Skip to content

Unions of collectively valid argument types not accepted by @overload groups #3556

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
rkr-at-dbx opened this issue Jun 16, 2017 · 5 comments
Closed
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-overloads topic-union-types

Comments

@rkr-at-dbx
Copy link

This fails:

import re
from typing import Union, Pattern

def f(pattern):
    # type: (Union[str, Pattern[str]]) -> None
    re.search(pattern, "")

example.py:6: error: Argument 1 to "search" has incompatible type "Union[str, Pattern[str]]"; expected "unicode"

However, this passes without comment:

def g(pattern):
    # type: (Union[str, Pattern[str]]) -> None
    if isinstance(pattern, str):
        re.search(pattern, "")
    else:
        re.search(pattern, "")
@ilevkivskyi
Copy link
Member

This is a more general manifestation of #3295 , see comments under the issue.

@JukkaL I think we should support some "union math" in such simple cases, as I proposed in #3295 (comment)

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 16, 2017

This looks like a duplicate of #1943 (though there's only a quick mention of overloads in that issue)? Yes, it would be nice to have this fixed.

@ilevkivskyi
Copy link
Member

I think there are two slightly different problems:
#1943 is more about Union[str, bytes] should be accepted where AnyStr is expected:

def f(x: AnyStr) -> AnyStr:
    ...

a: Union[str, bytes]
reveal_type(f(a)) # Revealed type should be Union[str, bytes]

This issue and #3295 are more about something like this should also be accepted:

@overload
def f(x: str) -> str: ...
@overload
def f(x: bytes) -> int: ...
def f(x):
    ... # implementation

a: Union[str, bytes]
reveal_type(f(s)) # this should give Union[str, int]

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 16, 2017

#1943 also mentions the issue with overloads though it doesn't provide an example. Both use cases are quite similar, but we can keep both of these issues open.

@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 19, 2018
@Michael0x2a
Copy link
Collaborator

Closing -- the original example provided up above appears to behave as expected on master (probably due to f61c2ba).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-overloads topic-union-types
Projects
None yet
Development

No branches or pull requests

4 participants