From 0054046c8c147a5190769bde21eb08615c16038e Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 26 May 2022 14:43:40 +0100 Subject: [PATCH] Fix crash with nested attrs class (#12872) Fixes #12868. --- mypy/semanal.py | 4 +++- test-data/unit/check-attr.test | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index a49e7c23edf5..e00913a8cde4 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4505,7 +4505,9 @@ class C: """ # TODO: Forward reference to name imported in class body is not # caught. - assert self.statement # we are at class scope + if self.statement is None: + # Assume it's fine -- don't have enough context to check + return True return (node is None or self.is_textually_before_statement(node) or not self.is_defined_in_current_module(node.fullname) diff --git a/test-data/unit/check-attr.test b/test-data/unit/check-attr.test index fdb0da7e0fce..021be93bdd21 100644 --- a/test-data/unit/check-attr.test +++ b/test-data/unit/check-attr.test @@ -1734,3 +1734,18 @@ class C: # E: Unsupported converter, only named functions and types are currently supported ) [builtins fixtures/dict.pyi] + +[case testAttrsNestedClass] +from typing import List +import attr + +@attr.s +class C: + @attr.s + class D: + pass + x = attr.ib(type=List[D]) + +c = C(x=[C.D()]) +reveal_type(c.x) # N: Revealed type is "builtins.list[__main__.C.D]" +[builtins fixtures/list.pyi]