Skip to content

mypy forgets that value is not None inside function definition #15178

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
schuelermine opened this issue May 3, 2023 · 2 comments
Closed

mypy forgets that value is not None inside function definition #15178

schuelermine opened this issue May 3, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@schuelermine
Copy link

Bug Report

mypy thinks that the type of a variable is Optional[A] when it has been previously determined to not be None when the variable is used in a function definition

To Reproduce

from typing import TypeVar, ParamSpec, Concatenate
from collections.abc import Callable

A = TypeVar("A")
B = ParamSpec("B")
C = TypeVar("C")


def guard_none(a: A | None, f: Callable[Concatenate[A, B], C]) -> Callable[B, C]:
    if a is None:
        raise TypeError()
    else:
        
        def g(*args: B.args, **kwargs: B.kwargs) -> C:
            return f(a, *args, **kwargs)
        
        return g

Expected Behavior

I expect no error.

Actual Behavior

mypy produces this error:

main.py:15: error: Argument 1 has incompatible type "Optional[A]"; expected "A"  [arg-type]

Your Environment

mypy playground

  • Mypy version used: 1.2.0
  • Mypy command-line flags: --strict
  • Python version used: 3.11
@schuelermine schuelermine added the bug mypy got something wrong label May 3, 2023
@schuelermine
Copy link
Author

apparently this is a duplicate

@JelleZijlstra
Copy link
Member

Of #2608

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

No branches or pull requests

2 participants