Skip to content

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Copy link
Member

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 a str? (The problem predates this PR, but we may as well fix it while we're here.)

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):
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else:
if isinstance(runtime, type):
return mypy.types.TypeType(fallback)
elif isinstance(runtime, type):
return mypy.types.TypeType(fallback)
else:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to use nested if because all other elif cases work with literal values. I didn't want to created mixed concepts here.

And since we only work with fallback under else - I kept it that way.

Copy link
Member

@AlexWaygood AlexWaygood Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see -- maybe we should move this block to just below the if isinstance(runtime, tuple) block on lines 1373-1378, in that case? That seems like a more logical place for it to be, and it would mean we could have it as an isolated block of code instead of next to all the literal-value stuff.

return fallback

return mypy.types.LiteralType(value=value, fallback=fallback)
Expand Down
178 changes: 178 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,184 @@ def read_write_attr(self, val): self._val = val
error=None,
)

@collect_cases
def test_class_var(self) -> Iterator[Case]:
# Regular classes:
yield Case(stub="class Z: ...", runtime="class Z: ...", error=None)
yield Case(
stub="""
class A1:
a: type[Z]
""",
runtime="""
class A1:
a = Z
""",
error=None,
)
yield Case(
stub="""
class A2:
a: type[Z]
""",
runtime="""
class A2:
a = int
""",
error="A2.a",
)
yield Case(
stub="""
class A3:
a: Z
""",
runtime="""
class A3:
a = Z
""",
error="A3.a",
)
yield Case(
stub="""
class A4:
a: type[Z]
""",
runtime="""
class A4:
a = Z()
""",
error="A4.a",
)

@collect_cases
def test_class_var_meta(self) -> Iterator[Case]:
# Now, with metaclasses:
yield Case(
stub="""
class Meta(type): ...
class Y(metaclass=Meta): ...
""",
runtime="""
class Meta(type): ...
class Y(metaclass=Meta): ...
""",
error=None,
)
yield Case(
stub="""
class B1:
a: type[Y]
""",
runtime="""
class B1:
a = Y
""",
error=None,
)
yield Case(
stub="""
class B2:
a: type[Y]
""",
runtime="""
class B2:
a = int
""",
error="B2.a",
)
yield Case(
stub="""
class B3:
a: Meta
""",
runtime="""
class B3:
a = Y
""",
error=None,
)
yield Case(
stub="""
class B4:
a: Meta
""",
runtime="""
class B4:
a = int
""",
error="B4.a",
)
yield Case(
stub="""
class B5:
a: type[Y]
""",
runtime="""
class B5:
a = Y()
""",
error="B5.a",
)
yield Case(
stub="""
class B6:
a: Y
""",
runtime="""
class B6:
a = Y
""",
error="B6.a",
)
yield Case(
stub="""
class B7:
a: Meta
""",
runtime="""
class B7:
a = Y()
""",
error="B7.a",
)

@collect_cases
def test_class_var_abc(self) -> Iterator[Case]:
# Now, with ABC metaclasses:
yield Case(
stub="""
import abc
class Y(metaclass=abc.ABCMeta): ...
""",
runtime="""
import abc
class Y(metaclass=abc.ABCMeta): ...
""",
error=None,
)
yield Case(
stub="""
class B1:
a: type[Y]
""",
runtime="""
class B1:
a = Y
""",
error=None,
)
yield Case(
stub="""
class B2:
a: Y
""",
runtime="""
class B2:
a = Y
""",
error="B2.a",
)

@collect_cases
def test_type_alias(self) -> Iterator[Case]:
yield Case(
Expand Down