diff --git a/mypy/semanal.py b/mypy/semanal.py
--- 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
--- 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]

AssertionError on version 0.960
**Bug Report**

We are using the [ddtrace](https://github.com/DataDog/dd-trace-py) Python package.
Consider the following file:
```python
# test.py
import ddtrace
```

Running `mypy test.py` passes without issues on version `0.950` but when run on version `0.960` the following is printed:

```python
Traceback (most recent call last):
  File ".../bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File ".../python3.10/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 96, in main
  File "mypy/main.py", line 173, in run_build
  File "mypy/build.py", line 154, in build
  File "mypy/build.py", line 230, in _build
  File "mypy/build.py", line 2729, in dispatch
  File "mypy/build.py", line 3087, in process_graph
  File "mypy/build.py", line 3179, in process_stale_scc
  File "mypy/semanal_main.py", line 88, in semantic_analysis_for_scc
  File "mypy/semanal_main.py", line 414, in apply_class_plugin_hooks
  File "mypy/semanal_main.py", line 434, in apply_hooks_to_class
  File "mypy/plugins/attrs.py", line 309, in attr_class_maker_callback
  File "mypy/plugins/attrs.py", line 372, in _analyze_class
  File "mypy/plugins/attrs.py", line 498, in _attributes_from_assignment
  File "mypy/plugins/attrs.py", line 594, in _attribute_from_attrib_maker
  File "mypy/semanal.py", line 5450, in anal_type
  File "mypy/types.py", line 765, in accept
  File "mypy/typeanal.py", line 172, in visit_unbound_type
  File "mypy/typeanal.py", line 263, in visit_unbound_type_nonoptional
  File "mypy/typeanal.py", line 1168, in anal_array
  File "mypy/typeanal.py", line 1177, in anal_type
  File "mypy/types.py", line 765, in accept
  File "mypy/typeanal.py", line 172, in visit_unbound_type
  File "mypy/typeanal.py", line 180, in visit_unbound_type_nonoptional
  File "mypy/semanal.py", line 4563, in lookup_qualified
  File "mypy/semanal.py", line 4458, in lookup
  File "mypy/semanal.py", line 4508, in is_active_symbol_in_class_body
AssertionError
```

I tried using the `--pdb` and `--tb` flags to get the traceback, or see which part of the library is causing this but was unable to do so.
I tried clearing the mypy cache, which didn't help as well.

Is this an issue with mypy, the library or both?

**Your Environment**

- Mypy version used: 0.960
- Python version used: 3.10
- Operating system and version: Ubuntu 22.04


Thanks for the report! I can reproduce this on the master branch, if I create a new virtual environment with just mypy's dependencies and `ddtrace` installed, and then run mypy on a file that only has `import ddtrace` inside it.

The crash bisects to 50a653e0714e303178c890854b9074fc030907f0 (PR #12772). Cc. @JukkaL.
I don't know if this could be related, but there seems to be issues with how `dataclasses` are handled too:

https://github.com/DataDog/riot/runs/6594827098?check_suite_focus=true

The logs show that the latest version of `mypy` has started complaining about attributes that are defined on the dataclass

https://github.com/DataDog/riot/blob/2f6ea2e7a128239c45d4771245f572095651436e/riot/riot.py#L177-L231
Thanks for reporting this! Getting this as well.
Here is a small reproducer for the attrs crash:
```py
import attr

@attr.s
class C:
    @attr.s
    class D:
        pass
    x = attr.ib(type=list[D])
```

As a workaround, moving the nested class to the module top level should fix the crash. I'm working on a fix.
mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsTypeEquals mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsSimple mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsSeriousNames mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsEqFalse mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsFrozen mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsAnnotated mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsDefaultErrors mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsOrderFalse mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsPython2Annotations mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsNotBooleans mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsNextGenFrozen mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsCmpEqOrderValues mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsInheritance mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsAutoAttribs mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsNextGenDetect mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsInitFalse mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsNewPackage mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsInitAttribFalse mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsNestedInClasses mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUntypedNoUntypedDefs mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsDataClass mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsInheritanceOverride mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsCmpTrue mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsWrongReturnValue mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsMultiGenericInheritance mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsTypeAlias mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsDefaultDecorator mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUsingBadConverter mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsGenericClassmethod mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsGeneric mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsValidatorDecorator mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUsingBadConverterReprocess mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUntypedGenericInheritance mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsLocalVariablesInClassMethod mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsForwardReference mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUsingUnsupportedConverter mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsGenericInheritance mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUnionForward mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUsingConverterAndSubclass mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsForwardReferenceInClass mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsGenericInheritance2 mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUsingConvert mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUsingConverterWithTypes mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsImporting mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUsingConverter mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsOtherMethods mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsGenericInheritance3 mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsCmpWithSubclasses mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsOtherOverloads mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUsingConvertAndConverter mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsComplexSuperclass mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsAutoMustBeAll mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsFactoryBadReturn mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsFrozenSubclass mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsMultiAssign mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsRepeatedName mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsDefaultAndInit mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsMultiAssign2 mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsCallableAttributes mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsNewStyleClassPy2 mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsPrivateInit mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsWithFactory mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsAutoAttribsPy2 mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsOptionalConverter mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsKwOnlyClass mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsFactoryAndDefault mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsKwOnlyClassWithMixedDefaults mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsOptionalConverterNewPackage mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsKwOnlyClassNoInit mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsKwOnlySubclass mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsNonKwOnlyAfterKwOnly mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsTypeVarNoCollision mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsKwOnlyWithDefault mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsDisallowUntypedWorksForward mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsKwOnlyPy2 mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsKwOnlyAttrib mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testDisallowUntypedWorksForwardBad mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsDefaultDecoratorDeferred mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testTypeInAttrUndefined mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testTypeInAttrForwardInRuntime mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testDefaultInAttrForward mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsValidatorDecoratorDeferred mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testTypeInAttrUndefinedFrozen mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testTypeInAttrDeferredStar mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsInheritanceNoAnnotation mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsClassHasAttributeWithAttributes mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsDefaultsMroOtherFile mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsBareClassHasAttributeWithAttributes mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsClassWithSlots mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsNGClassHasAttributeWithAttributes mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsWithMatchArgs mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsWithMatchArgsDefaultCase mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsWithMatchArgsOldVersion mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsWithMatchArgsOverrideExisting mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsMultipleInheritance mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsForwardReferenceInTypeVarBound mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsGenericInheritanceSpecialCase2 mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsGenericInheritanceSpecialCase1 mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testComplexTypeInAttrIb mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsUnsupportedConverterWithDisallowUntypedDefs
mypy/test/testcheck.py::TypeCheckSuite::check-attr.test::testAttrsNestedClass