-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
Description
As originally described in #5788, I've run into a problem with --disallow-any-expr
reporting spurious errors.
Consider the following file any.py
:
def access_before_declaration(self) -> None:
obj = Object('foo')
obj.value
x = 1
reveal_type(x) # Revealed type is 'builtins.int'
x = x + 1 # Expression has type "Any"
class Object:
def __init__(self, value: str) -> None:
self.value = value
def access_after_declaration(self) -> None:
obj = Object('foo')
obj.value
x = 1
reveal_type(x) # Revealed type is 'builtins.int'
x = x + 1
Running mypy on it produces:
$ python3 -m mypy --version
mypy 0.641
$ python3 -m mypy --disallow-any-expr any.py
any.py:6: error: Revealed type is 'builtins.int'
any.py:7: error: Expression has type "Any"
any.py:18: error: Revealed type is 'builtins.int'
What we see is that x
is correctly identified as int
in access_after_declaration
, but incorrectly as Any
in access_before_declaration
. The error is only triggered in very specific circumstances:
- There is a class defined after the inspected line
- An attribute of an instance of that class is accessed before the inspected line