FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Dedup overlapping union items in error messages by mokashang · Pull Request #22196 · python/mypy · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .test  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 12 additions & 3 deletions mypy/messages.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3521,11 +3521,20 @@ def ignore_last_known_values(t: UnionType) -> Type:
last_known_values are kept during union simplification, but may cause
weird formatting for e.g. tuples of literals.
"""
# Flatten nested unions reached through type aliases so an item like
# ``AnotherAlias`` (where ``AnotherAlias = str | None``) does not
# bypass deduplication just because it is wrapped in a TypeAliasType.
# Keep recursive aliases intact so we don't try to expand them forever.
items = flatten_nested_unions(t.items, handle_recursive=False)
union_items: list[Type] = []
seen_instances = set()
for item in t.items:
if isinstance(item, ProperType) and isinstance(item, Instance):
erased = item.copy_modified(last_known_value=None)
for item in items:
if isinstance(item, TypeAliasType) and item.is_recursive:
union_items.append(item)
continue
proper = get_proper_type(item)
if isinstance(proper, Instance):
erased = proper.copy_modified(last_known_value=None)
if erased in seen_instances:
continue
seen_instances.add(erased)
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-python312.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2282,6 +2282,19 @@ if isinstance(x, tuple):
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]

[case testPEP695TypeAliasUnionErrorDeduplicatesOverlappingItems]
# Regression test for https://github.com/python/mypy/issues/22194
# Overlapping aliases should not render as e.g. "str | str | None".
type TextInput = str
type ImageInput = str | None
type SingleInput = TextInput | ImageInput

def f(x: SingleInput) -> None: ...

f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str | None"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]

[case testPEP695MultipleTypeVarTuples]
from typing import Generic
from typing_extensions import TypeVarTuple, Unpack
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-type-aliases.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1449,3 +1449,24 @@ Alias1: TypeAlias = Union[C, int]
class SomeClass:
pass
[builtins fixtures/tuple.pyi]

[case testUnionTypeAliasErrorDeduplicatesOverlappingItems]
# Regression test for https://github.com/python/mypy/issues/22194
# Overlapping aliases should not render as e.g. "str | str | None".
from typing_extensions import TypeAlias

TextInput: TypeAlias = str
ImageInput: TypeAlias = str | None
SingleInput: TypeAlias = TextInput | ImageInput

def f(x: SingleInput) -> None: ...

f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str | None"

Opt: TypeAlias = int | None
U: TypeAlias = int | Opt

def g(x: U) -> None: ...

g("x") # E: Argument 1 to "g" has incompatible type "str"; expected "int | None"
[builtins fixtures/tuple.pyi]
Loading

Back | FazBrowse Home | New Git URL