Skip to content

False negative attr-defined in match case #20018

@vepain

Description

@vepain

Bug Report

Mypy does not identify an AttributeError in match case statement

To Reproduce

fn_attr_defined.py

"""Test attr-defined false negative Mypy."""

import itertools


def check(obj: object) -> None:
    match obj:
        case itertools.chain():
            print("Chain")
        case itertools.DoesNotExist():
            print("Here there must be a attr-defined mypy error")
        case _:
            print("Default")


if __name__ == "__main__":
    check(1)
python3 fn_attr_defined.py
Traceback (most recent call last):
  File "fn_attr_defined.py", line 17, in <module>
    check(1)
    ~~~~~^^^
  File "fn_attr_defined.py", line 10, in check
    case itertools.DoesNotExist():
         ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'itertools' has no attribute 'DoesNotExist'

Expected Behavior

Mypy should return a attr-defined error at line case itertools.DoesNotExist():

Actual Behavior

No errors returned by Mypy

Your Environment

  • Mypy version used: mypy 1.18.2 (compiled: yes)

  • Mypy command-line flags: mypy --no-color-output --no-error-summary --show-absolute-path --show-column-numbers --show-error-codes --no-pretty --config-file=mypy.ini --show-error-end fn_attr_defined.py

  • Mypy configuration options from mypy.ini (and other config files):

    [mypy]
    show_column_numbers = True
    check_untyped_defs = True
  • Python version used: Python 3.13.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions