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

mypy fails to narrow a type when isinstance() is wrapped inside a function #8322

Closed
kprzybyla opened this issue Jan 23, 2020 · 1 comment
Closed

Comments

@kprzybyla
Copy link
Contributor

I stumbled upon this couple of times so I guess it's fairly reasonable to report this. Currently mypy understand isinstance() and will properly resolve types when isinstance() is used inside if statement:

from typing import Union

Complex = Union[int, str]

x: Complex

if isinstance(x, int):
    reveal_type(x)
example.py:8: note: Revealed type is 'builtins.int'

However, if you move isinstance() call to function that basically wraps it, mypy fails to narrow a type of variable.

from typing import Union

Complex = Union[int, str]

x: Complex

def is_integer(value: Complex) -> bool:
    return isinstance(value, int)

if is_integer(x):
    reveal_type(x)
example.py:11: note: Revealed type is 'Union[builtins.int, builtins.str]'

This is somewhat annoying if you have a complex isinstance() call that you don't want to copy in your code over and over again but simply hide it under a descriptive function name.

This might be somehow related to #4573.

Versions

mypy 0.761
Python 3.7.4
@ethanhs
Copy link
Collaborator

ethanhs commented Jan 23, 2020

Duplicate of #5206

@ethanhs ethanhs marked this as a duplicate of #5206 Jan 23, 2020
@ethanhs ethanhs closed this as completed Jan 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants