Skip to content

Any inferred when overloaded function called with None argument #3256

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
JukkaL opened this issue Apr 26, 2017 · 4 comments
Closed

Any inferred when overloaded function called with None argument #3256

JukkaL opened this issue Apr 26, 2017 · 4 comments
Labels

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 26, 2017

Mypy infers Any when calling a precisely typed overloaded function with a None argument (without strict optional checking). This is undesirable, since an Any is silently generated:

from typing import overload

@overload
def f(x: int) -> int:
    pass
@overload
def f(x: str) -> str:
    pass

def f(x): pass

reveal_type(f(None))  # Any

Not sure what's the right thing to do here -- we can either generate a message that says that the overload target is ambiguous, or infer Union[int, str] as the return type.

Note that explicitly including a None overload variant does not work as a workaround:

from typing import overload

@overload
def f(x: None) -> int:  # Overloaded function signatures 1 and 3 overlap with 
                        # incompatible return types
    pass
@overload
def f(x: int) -> int:
    pass
@overload
def f(x: str) -> str:
    pass

def f(x): pass

This is related to #1322.

@ilinum
Copy link
Collaborator

ilinum commented Jul 6, 2017

Without the --strict-optional flag, the functions in the example are essentially overlapping, so we should treat them as such (because any type could be None).

It seems to me that this issue is more or less a duplicate #1322.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Jul 6, 2017

This is a special case of #1322. Not sure if it's useful to track these as separate issues.

@ilinum
Copy link
Collaborator

ilinum commented Jul 6, 2017

I think it's useful to keep it open, just to make sure we don't forget to test this case.

@Michael0x2a
Copy link
Collaborator

I think this is working in a reasonable way on master now: mypy reports a 'No overload variant of "f" matches argument type "None"` in strict-optional mode and infers 'int' in no-strict-optional mode.

If we add a 'None' fallback, we infer int in both cases.

I guess the remaining question is whether or not it would be nice to add a command line flags that tells mypy to do something more stricter then just picking the first match, but I think we already have an issue for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants