Skip to content

unreachable code isn't type checked #11649

Closed

Description

@DetachHead
foo: str

if isinstance(foo, str):
    ...
else:
    asdf: list[str] = "" # no error

https://mypy-play.net/?mypy=master&python=3.10&gist=98cc704dbc9936fa1c3801984cd631fd

my use case is an assert_unreachable function:

def assert_unreachable(msg: str):
    raise AssertionError(msg)

foo: str

if isinstance(foo, str):
    ...
else:
    # no error even though the msg argument was not provided
    assert_unreachable() #type:ignore[unreachable]

Activity

  1. hauntsaninja commented on Dec 1, 2021

    @hauntsaninja
    Collaborator

    Yeah, this is intentional. https://mypy.readthedocs.io/en/stable/common_issues.html#unreachable-code
    I think in practice basically all warnings mypy would give if it didn't do this would end up being false positives.

  2. KotlinIsland commented on Dec 1, 2021

    @KotlinIsland
    Contributor

    @hauntsaninja Those docs don't really explain why "this would end up being false positives", only showing very obvious true positives being ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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