Skip to content

Wrong types when pattern matching int with http.HTTPStatus #18866

Open
@injust

Description

@injust

Bug Report

When using structural pattern matching to compare an int vs an http.HTTPStatus instance (and vice versa), mypy gets the type wrong or thinks the code is unreachable.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.13&flags=strict%2Cwarn-unreachable&gist=843b35aa16cac4c7357b94902cc9057b

from http import HTTPStatus
from typing import reveal_type

code = 200

match code:
    case HTTPStatus():
        pass
    case HTTPStatus.OK:
        reveal_type(code)
    case _:
        pass


status = HTTPStatus.OK

match status:
    case 200:
        reveal_type(status)
    case _:
        pass

Expected Behavior

The revealed types should be int and Literal[HTTPStatus.OK].

Actual Behavior

main.py:10: note: Revealed type is "Literal[http.HTTPStatus.OK]"
main.py:19: note: Revealed type is "Never"
Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: --strict --warn-unreachable
  • 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