Skip to content

Commit a94e649

Browse files
TH3CHARLieilevkivskyi
authored andcommitted
Fix expr-has-type-any error following attribute access with disallow-any-expr flag (#7898)
Resolves #5842
1 parent 8252a0b commit a94e649

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3616,7 +3616,7 @@ def accept(self,
36163616
not always_allow_any and
36173617
not self.chk.is_stub and
36183618
self.chk.in_checked_function() and
3619-
has_any_type(typ)):
3619+
has_any_type(typ) and not self.chk.current_node_deferred):
36203620
self.msg.disallowed_any_type(typ, node)
36213621

36223622
if not self.chk.in_checked_function() or self.chk.current_node_deferred:

test-data/unit/check-classes.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6489,3 +6489,26 @@ reveal_type(Foo().y) # N: Revealed type is 'builtins.list[Any]'
64896489
class Foo:
64906490
def bad(): # E: Method must have at least one argument
64916491
self.x = 0 # E: Name 'self' is not defined
6492+
6493+
[case testTypeAfterAttributeAccessWithDisallowAnyExpr]
6494+
# flags: --disallow-any-expr
6495+
6496+
def access_before_declaration(self) -> None:
6497+
obj = Foo('bar')
6498+
obj.value
6499+
x = 1
6500+
6501+
reveal_type(x) # N: Revealed type is 'builtins.int'
6502+
x = x + 1
6503+
6504+
class Foo:
6505+
def __init__(self, value: str) -> None:
6506+
self.value = value
6507+
6508+
def access_after_declaration(self) -> None:
6509+
obj = Foo('bar')
6510+
obj.value
6511+
x = 1
6512+
6513+
reveal_type(x) # N: Revealed type is 'builtins.int'
6514+
x = x + 1

0 commit comments

Comments
 (0)