Incorrect Any
class method return type inference with Generic TypeVars in separate modules
#18411
Labels
bug
mypy got something wrong
Bug Report
The setup:
I have a generic class (
Instantiator
) which contains a class method (instantiate()
) which is passed a desired type (MyClass
) and instantiates an instance of the generic type. I then have another function (returns_instance()
) outside of the class which callsinstantiate
and returns an instance of the passed desired type.What is happening:
When the definition for the generic type and the generic class lives in the same module as the calling function, there are no mypy errors. However, if the definition for the generic type and the generic class live in a different module from the calling function, mypy throws a
error: Returning Any from function declared to return "MyClass" [no-any-return]
error.To Reproduce
File 1:
a.py
File 2:
b.py
Expected Behavior
I expect that mypy would be able to correctly infer that the return type of the
instantiate_*
methods as it does forInstantiatorA
, but forInstantiatorB
it incorrectly reportsAny
as the return type.I would expect that maybe passing the desired return type as a subscript was needed such as in
returns_instance_b_with_generic
, and that actually does result in VSCode showing the correct return type, but mypy still does not.Actual Behavior
Errors are reported inline above.
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: