Skip to content

"Statement is unreachable" when doing match on type objects #18524

Description

@woodruffw

Hello mypy maintainers! I attempted to search for a dupe for this but couldn't find one, apologies if I missed it.

Bug Report

When running mypy with warn_unreachable = true (set in pyproject, but also on CLI), I get what appears to be a false positive for unreachable code on match statements where the matched object is a type and some of the arms are types.

For example (minimized/abstracted from my actual code):

importbuiltinsimporttypingimporttypesdeffrobulate(field_type: type) ->str:
matchfield_type:
casebuiltins.int:
ret="foo"casebuiltins.str:
ret="foo"casebuiltins.bytes:
ret="foo"casebuiltins.bool:
ret="foo"casetypes.NoneType:
ret="foo"case _:
return"bar"returnret

Produces:

main.py:10: error: Statement is unreachable [unreachable]
main.py:12: error: Statement is unreachable [unreachable]
main.py:16: error: Statement is unreachable [unreachable]
Found 3 errors in 1 file (checked 1 source file)

However, at runtime, I can confirm that these case arms are matched correctly.

To Reproduce

Playground reproducer: https://mypy-play.net/?mypy=latest&python=3.13&flags=warn-unreachable&gist=416b0d53cd61032acee0341e9b24de11

(Note that --warn-unreachable needs to be enabled for reproduction. I think the Playground link should preserve this, but just in case!)

Expected Behavior

I expected mypy to match on type objects correctly, as Python itself appears to do correctly at runtime.

Actual Behavior

From above:

main.py:10: error: Statement is unreachable [unreachable]
main.py:12: error: Statement is unreachable [unreachable]
main.py:16: error: Statement is unreachable [unreachable]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.14.1
  • Mypy command-line flags: --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions