Skip to content

Spurious complaint about return value in deferred function #4488

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 Jan 18, 2018 · 1 comment
Closed

Spurious complaint about return value in deferred function #4488

JukkaL opened this issue Jan 18, 2018 · 1 comment
Labels
bug mypy got something wrong topic-strict-optional

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 18, 2018

Mypy incorrectly complains about the return statement in this program (with --strict-optional):

def f() -> str:
    y  # Causes f to be deferred
    x = None
    if int():
        x = ''
    if x is None:
        x = ''
    return x  # Incompatible return value type (got "Optional[Any]", expected "str")

y = int()

The expected behavior is clean output.

Note that f is deferred. The problem goes away when f is not deferred.

@JukkaL JukkaL added bug mypy got something wrong topic-strict-optional labels Jan 18, 2018
@JukkaL
Copy link
Collaborator Author

JukkaL commented Jan 18, 2018

Slightly tweaked example illustrates another problem:

def f() -> str:
    y
    x = None
    if int():
        x = ''
    if x is None:
        x = ''
        x[0]  # Value of type "None" is not indexable   <-- Unexpected
    return x  # Incompatible return value type (got "Optional[Any]", expected "str")

y = int()

JukkaL added a commit that referenced this issue Jan 18, 2018
Previously a partial type could be narrowed down to a bogus
`Optional[Any]` type. Selectively switch narrowing down off
in deferred nodes to work around the issue.

Fixes #4488.
JukkaL added a commit that referenced this issue Jan 19, 2018
Previously a partial type could be narrowed down to a bogus
`Optional[Any]` type. Selectively switch narrowing down off
in deferred nodes to work around the issue.

Fixes #4488.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-strict-optional
Projects
None yet
Development

No branches or pull requests

1 participant