mypyc icon indicating copy to clipboard operation
mypyc copied to clipboard

Local variable "p" has inferred type None; add an annotation

Open hauntsaninja opened this issue 2 years ago • 3 comments

It's unclear to me mypyc thinks it needs an explicit annotation here

def bar() -> str | None:
    return "asdf"

def foo() -> None:
    p = bar()
    if p is None:
        print(f"{p}")

Came up in https://github.com/psf/black/pull/4015

hauntsaninja avatar Nov 01 '23 19:11 hauntsaninja

This seems like a feature in mypyc and a bug in mypy! This error was intentionally introduced in #7482 to force a compilation failure. The underlying issue in mypy is #5423.

advait-dixit avatar Nov 17 '24 23:11 advait-dixit

python/mypy#5423 was fixed but this is still happening. The best course of action may be to accept the None type.

JukkaL avatar Feb 03 '25 18:02 JukkaL

I think this error is pretty reasonable when it happens, seems fine to just focus on root causes in binder etc

hauntsaninja avatar Feb 03 '25 18:02 hauntsaninja