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
{{ message }}
Repository navigation
issubclass() assertions not narrowing types #10680
Running mypy on this with the --warn-return-any option produces the error "Returning Any from function declared to return "Type[int]"", even though the assert issubclass should convince mypy that x is of type Type[int] and not an Any.
Your Environment
Mypy version used: 0.902
Mypy command-line flags: --warn-return-any
Mypy configuration options from mypy.ini (and other config files): none
Just in case it's a useful test-case, I have a slight variation on this error, where it's clear from looking that we can deduce the narrowed return type in all conditional paths but where mypy complains that it thinks we could potentially be dealing with an 'Any' type -
deff(something: bool) ->dict:
my_dict=Noneifsomething:
exec('d = {}', globals(), locals())
my_dict=locals()['d']
assertisinstance(my_dict, dict)
# ... No good if we use this assert.else:
my_dict= {}
# assert isinstance(my_dict, dict)# ... but all good if we use this assertreturnmy_dict
I think I'm seeing this error as well. Abbreviated example:
_registry: dict[str, type[Config] |Action]
...
forkey, configin_registry.items():
ifconfigisNOOP: # NOOP is an Action object, and it's the only one that existscontinueassertissubclass(config, Config)
config.configure() # <- error: Item "Action" of "type[Config] | Action" has no attribute "configure" [union-attr]
(PyCharm's type checker is happy with this.)
If I add an additional assertion assert not isinstance(config, Action), then the error goes away, but I feel like that shouldn't be necessary: if config is indeed a subclass of Config, how can it also be an instance of Action? (Action inherits only from object.)
Edit: I found that assert not isinstance(config, Action) as the sole assertion works as well, but my question still stands - why isn't the issubclass check also enough to convince mypy?
Activity
kveretennicov commentedon Jun 9, 2022
We hit this issue in production code.
mypy Playground gist with repro and a workaround: https://mypy-play.net/?mypy=latest&python=3.10&gist=35deff0d7118af72d07a113a1f05c82b (mypy 0.961, Python 3.10)
GertyP commentedon Jul 3, 2023
Just in case it's a useful test-case, I have a slight variation on this error, where it's clear from looking that we can deduce the narrowed return type in all conditional paths but where mypy complains that it thinks we could potentially be dealing with an 'Any' type -
dave-v commentedon May 20, 2025
I think I'm seeing this error as well. Abbreviated example:
(PyCharm's type checker is happy with this.)
If I add an additional assertion
assert not isinstance(config, Action), then the error goes away, but I feel like that shouldn't be necessary: ifconfigis indeed a subclass ofConfig, how can it also be an instance ofAction? (Actioninherits only fromobject.)Edit: I found that
assert not isinstance(config, Action)as the sole assertion works as well, but my question still stands - why isn't theissubclasscheck also enough to convince mypy?Let issubclass narrow Any