-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Fix crash when calling type[Any] from dict.get() #20586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix crash when calling type[Any] from dict.get() #20586
Conversation
6d12836 to
c0fcad8
Compare
test-data/unit/check-classes.test
Outdated
| # Regression test for https://github.com/python/mypy/issues/20585 | ||
| from typing import Any | ||
| op: type[Any] | ||
| reveal_type({0: object}.get(0, op)()) # N: Revealed type is "builtins.object" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expected Any, not object. Is there a way to reproduce the crash without relying on .get()? And did you verify this line crashed without the fix? Our fixture may be simpler than the actual typeshed stubs for dict.get.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added two unit tests to mypy/test/testtypes.py, and yes, I was able to reproduce that crash
This comment has been minimized.
This comment has been minimized.
c0fcad8 to
4224b93
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
A5rocks
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the object return but it sounds like that will need some special casing :/
Fixes #20585
When a callable has
type[Any]as its return type (e.g., fromdict.get(key, type[Any])), calling it would crash withAssertionError: isinstance(ret, Instance).The issue was in
is_type_obj()which returnedTruefortype[Any]becausetypeis a metaclass andAnyTypeisn'tUninhabitedType. However,type_object()then failed because it expects anInstance, notAnyType.The fix excludes
AnyTypefromis_type_obj()since we can't determine a concrete type object when the return type isAny.Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=4217675