10000 Fix join of Any against a union type by JukkaL · Pull Request #12068 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix join of Any against a union type #12068

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

Merged
merged 2 commits into from
Jan 25, 2022
Merged
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
6 changes: 3 additions & 3 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ def join_types(s: Type, t: Type, instance_joiner: Optional[InstanceJoiner] = Non
s = mypy.typeops.true_or_false(s)
t = mypy.typeops.true_or_false(t)

if isinstance(s, UnionType) and not isinstance(t, UnionType):
s, t = t, s

if isinstance(s, AnyType):
return s

if isinstance(s, ErasedType):
return t

if isinstance(s, UnionType) and not isinstance(t, UnionType):
s, t = t, s

if isinstance(s, NoneType) and not isinstance(t, NoneType):
s, t = t, s

Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -1881,4 +1881,10 @@ class C(IntEnum):
def f1(c: C) -> None:
x = {'x': c.value}
reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str*, builtins.int]"

def f2(c: C, a: Any) -> None:
x = {'x': c.value, 'y': a}
Copy link
Member

Choose a reason for hiding this comment

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

Will y = {'y': a, 'x': c.value} be helpful here as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good idea, I'll add it here.

reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str*, Any]"
y = {'y': a, 'x': c.value}
reveal_type(y) # N: Revealed type is "builtins.dict[builtins.str*, Any]"
[builtins fixtures/dict.pyi]
0