Python Type Hinting: Subclass not recognized.
Answered
PyCharm does not acknowledge that an instance of a subclass is also an instance of the superclass. Minimal example:
class A:
pass
class B(A):
pass
def function() -> A:
return B()
b: B = function()
Type Hinting will have a problem at `function()`, claiming that an instance of A is supplied and B is expected.
Production code is slightly more elaborate, with the exact subclass depending on the arguments to the equivalent of `function()`.
It would not be reasonable to expect the type-checker to figure out which subclass is returned, but when explicitly stated that I'm expecting an instance of B which is also an instance of A, I would expect the type check to be satisfied.
Please sign in to leave a comment.
Hi,
I filed a ticket to our issue tracker https://youtrack.jetbrains.com/issue/PY-46814, please follow it for further progress.