Skip to content

Fix TypeIs for types with type params in Unions #17232

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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Special handling fallback_to_any types
  • Loading branch information
kreathon committed May 21, 2024
commit 4ee4771ae1f86460e05cb22642a71f4d4c98bc0e
7 changes: 5 additions & 2 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ def restrict_subtype_away(t: Type, s: Type) -> Type:
new_items = [
restrict_subtype_away(item, s)
for item in p_t.relevant_items()
if (isinstance(get_proper_type(item), AnyType) or not covers_type(item, s))
if not covers_type(item, s)
]
return UnionType.make_union(new_items)
elif covers_type(t, s):
Expand All @@ -1941,7 +1941,10 @@ def covers_type(item: Type, supertype: Type) -> bool:
item = get_proper_type(item)
supertype = get_proper_type(supertype)

if is_proper_subtype(item, supertype, ignore_promotions=True, erase_instances=True):
if isinstance(item, AnyType) or (isinstance(item, Instance) and item.type.fallback_to_any):
return False

if is_subtype(item, supertype, ignore_promotions=True):
return True
if isinstance(supertype, Instance):
if supertype.type.is_protocol:
Expand Down