Fix bogus name-defined error for cross-module as_manager overrides #3597
Isn't there already a flag in anal_type to suppress such error ?
I tested this with the two error-related flags the plugin-facing anal_type exposes (allow_unbound_tvars and report_invalid_types), each on its own and both together. My suggested test case fails identically in every combination.
I think that's because those flags never reach this error. It's a name lookup failure rather than an invalid type: TypeAnalyser.lookup_qualified calls api.lookup_qualified with suppress_errors=False hardcoded, so the Name "..." is not defined error is emitted by SemanticAnalyzer.name_not_defined before either flag is consulted. As far as I can tell, [report_invalid_types only gates the "Invalid type: try using Literal[...]" family of messages](https://github.com/python/mypy/blob/v2.3.0/mypy/typeanal.py#L1433-L1440), and allow_unbound_tvars applies to TypeVars that resolve but are not bound in scope.
I think filter_errors() is the closest tool for the job, but happy to switch if there's a better mechanism I've missed.
UnknownPlatypus
BenA-SA
Since #3460, add_as_manager_to_queryset_class resolves an inherited as_manager override's return annotation with semanal_api.anal_type().
That resolves names against the current module. When the override lives in another module that mypy has not analyzed yet (consumer scheduled before the library inside the same SCC), the annotation is still an UnboundType, and any name private to the defining module - typically a TypeVar - fails to resolve, producing a spurious 'Name "..." is not defined' error reported against the consumer's file with the library's line number.
Suppress resolution errors from that call and bail out without generating a manager, restoring the pre-6.0.7 behaviour for the cross-module case while keeping #3460's fix for same-module overrides.
Related issues
Fixes #3596
AI Policy