You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mypy is able to narrow the type of a variable using issubclass, but in your example, the expression passed as the first argument to issubclass is not a variable but a call expression (type(obj)). I wouldn't expect that to work.
If you assign the result of this expression to a variable, mypy does narrow its type as expected.
And that's because obj hasn't been narrowed down, but a completely different variable (even though obj_type is a function of obj but mypy doesn't remember that) has been narrowed down?
So the only way of getting the result I want is using the isinstance checks?
Yeah, performing a runtime protocol check (regardless of whether it uses isinstance or issubclass) is not something I'd recommend doing in code that is performance critical.
Just adding one more example of this issue, involving a type variable bound to type. With Mypy --strict, the code snippet below raises the following error:
Mypy(return-value): Incompatible return value type (got "type[object]", expected "ClassT | None")
Activity
erictraut commentedon Aug 14, 2021
Mypy is able to narrow the type of a variable using
issubclass, but in your example, the expression passed as the first argument toissubclassis not a variable but a call expression (type(obj)). I wouldn't expect that to work.If you assign the result of this expression to a variable, mypy does narrow its type as expected.
Illviljan commentedon Aug 14, 2021
This example fails as well:
And that's because
objhasn't been narrowed down, but a completely different variable (even thoughobj_typeis a function ofobjbut mypy doesn't remember that) has been narrowed down?So the only way of getting the result I want is using the
isinstancechecks?erictraut commentedon Aug 14, 2021
You can get the result you want using an
isinstancecheck, but perhaps there was some other reason you want to useissubclasshere?Another option is to use a user-defined type guard. Unfortunately, this appears to hit a bug in mypy's TypeGuard support. This works fine in pyright.
Illviljan commentedon Aug 14, 2021
TypeGuard seems interesting! I have not looked into that one much. I'm mostly trying to improve performance and
isinstancechecks are rather slow.If you want to get really fast you can check for one of the required attributes MutableMapping has
But this is (admittedly) not that mypy friendly, but will probably do the job just fine.
erictraut commentedon Aug 14, 2021
Yeah, performing a runtime protocol check (regardless of whether it uses
isinstanceorissubclass) is not something I'd recommend doing in code that is performance critical.sg495 commentedon Dec 3, 2023
Just adding one more example of this issue, involving a type variable bound to
type. WithMypy --strict, the code snippet below raises the following error:However, the problem disappears if
typeis replaced byType[Any]:This was tested with
mypy 1.7.0 (compiled: yes)andPython 3.12.0.hauntsaninja commentedon Feb 10, 2026
sg495 case was fixed in #19183
For OP's case, they should probably just use
isinstance, but it is a thing we could potentially add narrowing for in the future