In this post, we will see how to resolve PyCharm incorrectly assumes object is instance of BaseClass after calling issubclass()
Question:
I am running this test with Python 3.10.9 and PyCharm 2022.2.1 (Community Edition). This is an issue with PyCharm, not Python itself.In my example code, I use a generator method that takes in a class type as an argument, and constructs the class. I want to make sure the class type that is passed is a subclass of
BaseClass
, so I use issubclass()
on the class_type
argument:issubclass(class_type, BaseClass)
, PyCharm now thinks that the constructed object is a BaseClass
object. The constructor call has a warning because it is looking at the wrong constructor, and the new_subclass.bar
call in Main has a warning because it doesn’t see bar
in BaseClass
, even though it is a SubClass
object as verified by the Python output.Does anybody know why this would be happening? It seems like a PyCharm error to me, I have no idea why simply calling
issubclass
would make PyCharm assume the object is the BaseClass. Without the call to issubclass
, PyCharm has no issues understanding that it is an instance of SubClass
.Thanks in advance for any help.
Best Answer:
I suspect it’s not really a bug, but a side effect of not providing any type hints. PyCharm starts with the assumption thatclass_type
as the type Any
, meaning any value could be assigned to the name and that value can be used with any operation, regardless of type.So what does PyCharm do with the
assert
statement? It tries to apply type narrowing, assigning class_type
a more specific type than Any
when the assertion is true. The most general type that makes the assertion True
is BaseClass
, so in the code that follows the assertion, it assumes that class_type
has the type BaseClass
, not Any
. (If the assertion failed, it doesn’t need to assume anything, because none of the following code would execute.)Once the type has been narrowed to
BaseClass
, the rest of the warnings are self-explanatory. BaseClass
isn’t provide the argument BaseClass.__init__
expects, and direct instances of BaseClass
don’t have bar
attributes.At runtime, everything is fine because there are no assumptions about the type of
class_type
; it is SubClass
, and so the code executes as expected.That said, I don’t have PyCharm installed to test.
mypy
does nothing, which leads me to suspect that PyCharm is being overly aggressive in performing type narrowing compared to mypy
. What happens if you provide explicit type hints, for example,assert
statement, but since the type hint already implies that T
could be bound to BaseClass
or a subclass of BaseClass
, no type narrowing should be necessary or applied.If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com