-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Option to disallow all expressions of type Any(--disallow-any=expr) #3519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
93d4c19
6517ae4
ee76320
f059dfd
ca06a5a
edef0b1
b5c33d7
a07ad12
ebc51a8
ff16858
d6d9ecb
775c5ec
a8731e9
b0193fb
fd166b1
a2dbe35
52b9206
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -515,3 +515,78 @@ x, y = 1, 2 # type: Unchecked, Unchecked | |
[out] | ||
main:4: error: Type of variable becomes "Any" due to an unfollowed import | ||
main:6: error: A type on this line becomes "Any" due to an unfollowed import | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be nice to have some tests for the basic functionality. These tests do a good job of exercising the more interesting edge cases, but it's worth testing putting an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me know if you have any other test ideas. |
||
[case testDisallowAnyExprSimple] | ||
# flags: --disallow-any=expr | ||
from typing import Any | ||
def f(s): | ||
yield s | ||
|
||
x = f(0) # E: Expression has type "Any" | ||
for x in f(0): # E: Expression has type "Any" | ||
g(x) # E: Expression has type "Any" | ||
|
||
def g(x) -> Any: | ||
yield x # E: Expression has type "Any" | ||
|
||
l = [1, 2, 3] | ||
l[f(0)] # E: Expression has type "Any" | ||
f(l) | ||
f(f(0)) # E: Expression has type "Any" | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testDisallowAnyExprUnannotatedFunction] | ||
# flags: --disallow-any=expr | ||
def g(s): | ||
return s | ||
|
||
g(0) | ||
w: int = g(1) | ||
|
||
[case testDisallowAnyExprExplicitAnyParam] | ||
# flags: --disallow-any=expr | ||
from typing import Any, List | ||
def f(s: Any) -> None: | ||
pass | ||
|
||
def g(s: List[Any]) -> None: | ||
pass | ||
|
||
f(0) | ||
|
||
# type of list below is inferred with expected type of List[Any], so that becomes it's type | ||
# instead of List[str] | ||
g(['']) # E: Expression type contains "Any" (has type List[Any]) | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testDisallowAnyExprAllowsAnyInCast] | ||
# flags: --disallow-any=expr | ||
from typing import Any, cast | ||
class Foo: | ||
g: Any = 2 | ||
|
||
z = cast(int, Foo().g) | ||
m = cast(Any, Foo().g) # E: Expression has type "Any" | ||
k = Foo.g # E: Expression has type "Any" | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testDisallowAnyExprAllowsAnyInVariableAssignmentWithExplicitTypeAnnotation] | ||
# flags: --disallow-any=expr | ||
from typing import Any | ||
class Foo: | ||
g: Any = 2 | ||
|
||
z: int = Foo().g | ||
x = Foo().g # type: int | ||
m: Any = Foo().g # E: Expression has type "Any" | ||
n = Foo().g # type: Any # E: Expression has type "Any" | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testDisallowAnyExprGeneric] | ||
# flags: --disallow-any=expr | ||
from typing import List | ||
|
||
l: List = [] | ||
l.append(1) # E: Expression type contains "Any" (has type List[Any]) | ||
k = l[0] # E: Expression type contains "Any" (has type List[Any]) # E: Expression has type "Any" | ||
[builtins fixtures/list.pyi] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
always_allow_any=True
here so we can give a more specific error message incheck_arg
? If so, could you add a comment about that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is.
I think we are not giving any errors for calls to function that accept
Any
anymore.Should I still add a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's still worth a quick comment, yeah.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After talking about this in person, we decided that adding info about calling untyped functions to documentation would be enough.