Skip to content

Experiment with intersection on TypeVars #15712

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

Closed
Closed
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: 6 additions & 7 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,11 @@ def visit_erased_type(self, t: ErasedType) -> ProperType:
def visit_type_var(self, t: TypeVarType) -> ProperType:
if isinstance(self.s, TypeVarType) and self.s.id == t.id:
return self.s
# this may be alright on things other than just instances
elif isinstance(self.s, (Instance, NoneType, TupleType)):
return t.copy_modified(upper_bound=meet_types(t.upper_bound, self.s))
Comment on lines +685 to +687
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note to self: there's probably a "is concrete type" utility somewhere in mypy. Maybe in checking for subtypes of type.

elif isinstance(self.s, UnionType):
return meet_types(t, self.s)
else:
return self.default(self.s)

Expand Down Expand Up @@ -753,13 +758,7 @@ def visit_instance(self, t: Instance) -> ProperType:
if is_subtype(self.s.fallback, t):
return self.s
return self.default(self.s)
elif isinstance(self.s, TypeType):
return meet_types(t, self.s)
elif isinstance(self.s, TupleType):
return meet_types(t, self.s)
elif isinstance(self.s, LiteralType):
return meet_types(t, self.s)
elif isinstance(self.s, TypedDictType):
elif isinstance(self.s, (TypeType, TupleType, LiteralType, TypedDictType, TypeVarType)):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note to self: check every visit method to make sure typevars are getting passed through.

return meet_types(t, self.s)
return self.default(self.s)

Expand Down
4 changes: 3 additions & 1 deletion mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,9 @@ def test_simple_generics(self) -> None:
self.assert_meet(self.fx.ga, self.fx.nonet, self.fx.nonet)
self.assert_meet(self.fx.ga, self.fx.anyt, self.fx.ga)

for t in [self.fx.a, self.fx.t, self.tuple(), self.callable(self.fx.a, self.fx.b)]:
self.assert_meet(self.fx.t, self.fx.ga, self.fx.t)

for t in [self.fx.a, self.tuple(), self.callable(self.fx.a, self.fx.b)]:
self.assert_meet(t, self.fx.ga, self.fx.nonet)

def test_generics_with_multiple_args(self) -> None:
Expand Down