-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
stubtest: Fix TypeType
subtyping problem
#13367
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1353,11 +1353,16 @@ def anytype() -> mypy.types.AnyType: | |||||||||||||
) | ||||||||||||||
|
||||||||||||||
# Try and look up a stub for the runtime object | ||||||||||||||
stub = get_stub(type(runtime).__module__) | ||||||||||||||
if isinstance(runtime, type): | ||||||||||||||
runtime_type = runtime # This might be a class | ||||||||||||||
else: | ||||||||||||||
runtime_type = type(runtime) # Or an instance | ||||||||||||||
|
||||||||||||||
stub = get_stub(runtime_type.__module__) | ||||||||||||||
if stub is None: | ||||||||||||||
return None | ||||||||||||||
type_name = type(runtime).__name__ | ||||||||||||||
if type_name not in stub.names: | ||||||||||||||
type_name = getattr(runtime_type, "__name__", None) | ||||||||||||||
if not isinstance(type_name, str) or type_name not in stub.names: | ||||||||||||||
return None | ||||||||||||||
type_info = stub.names[type_name].node | ||||||||||||||
if isinstance(type_info, nodes.Var): | ||||||||||||||
|
@@ -1382,6 +1387,8 @@ def anytype() -> mypy.types.AnyType: | |||||||||||||
elif isinstance(runtime, (bool, int, str)): | ||||||||||||||
value = runtime | ||||||||||||||
else: | ||||||||||||||
if isinstance(runtime, type): | ||||||||||||||
return mypy.types.TypeType(fallback) | ||||||||||||||
Comment on lines
1389
to
+1391
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to use nested And since we only work with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see -- maybe we should move this block to just below the |
||||||||||||||
return fallback | ||||||||||||||
|
||||||||||||||
return mypy.types.LiteralType(value=value, fallback=fallback) | ||||||||||||||
|
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.
It might be worth being a bit more defensive here as well. What if it's a class that doesn't have a
__module__
attribute, or that has a__module__
attribute set to something that isn't astr
? (The problem predates this PR, but we may as well fix it while we're here.)