From ce91dcfaa28e6a963ece092019fcd41c5115bee8 Mon Sep 17 00:00:00 2001 From: TH3CHARLie Date: Thu, 7 Nov 2019 11:17:20 +0800 Subject: [PATCH] fix expr-has-type-any error following attribute access with disallow-any-expr flag --- mypy/checkexpr.py | 2 +- test-data/unit/check-classes.test | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 27f462b7d00f..d45a27678e72 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -3613,7 +3613,7 @@ def accept(self, not always_allow_any and not self.chk.is_stub and self.chk.in_checked_function() and - has_any_type(typ)): + has_any_type(typ) and not self.chk.current_node_deferred): self.msg.disallowed_any_type(typ, node) if not self.chk.in_checked_function() or self.chk.current_node_deferred: diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 0764ee3e4903..78121bc70dad 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -6489,3 +6489,26 @@ reveal_type(Foo().y) # N: Revealed type is 'builtins.list[Any]' class Foo: def bad(): # E: Method must have at least one argument self.x = 0 # E: Name 'self' is not defined + +[case testTypeAfterAttributeAccessWithDisallowAnyExpr] +# flags: --disallow-any-expr + +def access_before_declaration(self) -> None: + obj = Foo('bar') + obj.value + x = 1 + + reveal_type(x) # N: Revealed type is 'builtins.int' + x = x + 1 + +class Foo: + def __init__(self, value: str) -> None: + self.value = value + +def access_after_declaration(self) -> None: + obj = Foo('bar') + obj.value + x = 1 + + reveal_type(x) # N: Revealed type is 'builtins.int' + x = x + 1