Skip to content
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

join is not associative in the presence of unions #3339

Open
ilevkivskyi opened this issue May 7, 2017 · 3 comments
Open

join is not associative in the presence of unions #3339

ilevkivskyi opened this issue May 7, 2017 · 3 comments
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions topic-strict-optional

Comments

@ilevkivskyi
Copy link
Member

While working on #3322 I have found this problem:

lst1 = [1, 'a', None]
lst2 = [1, None, 'a']

reveal_type(lst1) # builtins.list[builtins.object*]
reveal_type(lst2) # builtins.list[Union[builtins.str, builtins.int, builtins.None]]

This turns out to be the cause of spurious --strict-optional errors I mentioned there.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong topic-strict-optional labels May 7, 2017
@JukkaL
Copy link
Collaborator

JukkaL commented May 8, 2017

It looks like joins should only produce unions if one of the operands is a union with more than one non-None item. Examples:

  • join(Union[int, None], str) would be Union[object, None] which would get simplified into object.
  • join(Union[int, bytes, None], str) would be Union[int, bytes, str, None].

@hauntsaninja
Copy link
Collaborator

Another example (which doesn't use None)

def check(l_int: list[int], l_str: list[str], l_union: list[str] | list[bytes]):
    reveal_type([l_int, l_str])
    reveal_type([l_int, l_str, l_union])
    reveal_type([l_int, l_union, l_str])

@hauntsaninja hauntsaninja changed the title join is not associative in strict optional mode join is not associative in the presence of unions May 21, 2023
@ikonst
Copy link
Contributor

ikonst commented May 22, 2023

Maybe worth clarifying (in the original post) what's the desired outcome? I assume builtins.list[Union[builtins.str, builtins.int, builtins.None]] but wanted to make sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions topic-strict-optional
Projects
None yet
Development

No branches or pull requests

5 participants