Skip to content

Function returning None gives Any even with func-returns-value error disabled #20346

@efroemling

Description

@efroemling

If I want to have test code to make sure my api functions return expected types (or None) it can be a bit tricky to check the None case due to the '"foo" does not return a value' error. That error in itself is totally understandable; I just have a question about being able to disable it for cases like this.

from typing import assert_type

def should_not_give_value() -> None:
    ...

testval = should_not_give_value()  # Errors because function returns None
assert_type(testval, None)         # Errors because testval is Any

# If I ask Mypy to be ok with stuff returning None, then the first line
# passes but there's still an error on the second.
testval2 = should_not_give_value()  # type: ignore[func-returns-value]
assert_type(testval2, None)         # Errors because testval2 is Any

I understand the Any in the first example because the previous line has errored, but when that particular error is disabled would it be possible/make sense for Mypy to evaluate the second line to None instead of Any?

(note that using mypy --disable-error-code=func-returns-value yields the same result as the inline ignore comment above)

Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=130c3996eb66a252cdcd520019a1b6c9

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions