Skip to content

Type narrowing broken with dict items #18440

Open
coderabbit-test/mypy
#4
@esceral

Description

@esceral

Bug Report

When using a dict item, the type does not get narrowed.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=77cccd913f6c38a3536c4306c1439cf3

Expected Behavior

Both functions should typecheck.

Actual Behavior

main.py:6: error: Item "int" of "int | str" has no attribute "upper"  [union-attr]
main.py:16: error: Argument 1 to "assert_never" has incompatible type "int | str"; expected "Never"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

A workaround with an intermediate variable typechecks without issues.

Your Environment

  • Mypy version used: 1.13.0 (initially; also tested with master, 1.14.1, 1.12.0, 1.0.0)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statementtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions