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

Discriminated/narrowed Union type of closure’s upvalues does not carry into closure #5852

Closed
amarshall opened this issue Oct 29, 2018 · 2 comments

Comments

@amarshall
Copy link

Probably best illustrated with an example:

from typing import TypeVar, Union

_T = TypeVar('_T')
_U = TypeVar('_U')

def f(x: Union[_T, int]) -> None:
    if isinstance(x, int):
        reveal_type(x)  # int
        def g1() -> None:
            reveal_type(x)  # Union[_T, int] (but expect int)
    else:
        reveal_type(x)  # _T
        def g2() -> None:
            reveal_type(x)  # Union[_T, int] (but expect _T)

The same also happens with Optional and if x is None, but I presume the type-checker doesn’t treat this as any different, anyway.

Using:

  • Python 3.7.0
  • mypy 0.641
  • mypy config:
    [mypy]
    check_untyped_defs = True
    disallow_any_decorated = True
    disallow_any_expr = True
    disallow_any_unimported = True
    disallow_incomplete_defs = True
    disallow_subclassing_any = True
    disallow_untyped_calls = True
    disallow_untyped_decorators = True
    disallow_untyped_defs = True
    warn_return_any = True
    warn_unused_ignores = True
@ilevkivskyi
Copy link
Member

Duplicate of #2608

@ilevkivskyi ilevkivskyi marked this as a duplicate of #2608 Oct 29, 2018
@amarshall
Copy link
Author

Thanks for finding the duplicate; I searched but couldn’t find it myself :)

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