Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Inner functions lose type context #6236

Closed
psteele-wayfair opened this issue Jan 21, 2019 · 1 comment
Closed

Inner functions lose type context #6236

psteele-wayfair opened this issue Jan 21, 2019 · 1 comment

Comments

@psteele-wayfair
Copy link

MyPy can lose type information in an inner function deduced in an outer function.

Consider the following sample code:

from typing import Callable, Optional


def f(x: Optional[int]) -> Callable[[], int]:
    if x is None:
        x = 0
    reveal_type(x)

    def g() -> int:
        reveal_type(x)
        return x + 1

    return g

I would expect that MyPy accept this code without error, and for both reveal_type calls to return builtins.int. Running python -m mypy minimal.py we get

minimal.py:7: error: Revealed type is 'builtins.int'
minimal.py:10: error: Revealed type is 'Union[builtins.int, None]'
minimal.py:11: error: Unsupported operand types for + ("None" and "int")
minimal.py:11: note: Left operand is of type "Optional[int]"

The first reveal_type shows MyPy has correctly narrowed Optional[int] to int. However, inside the inner function we see that MyPy forgets this narrowing.

This was run using Python 3.6.0 and MyPy 0.660. The error persisted when running MyPy from the current master branch, currently hash f03ad7094e7a9bd174011e2ae3bc0c7204c95e22.

@ilevkivskyi
Copy link
Member

Duplicate of #2608

@ilevkivskyi ilevkivskyi marked this as a duplicate of #2608 Jan 21, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants