Skip to content

Referencing unknown decorated functions inside a match statement spuriously trips exhaustive-match #19791

@elisw93

Description

@elisw93

Bug Report

The conditions for reproducing this bug:

  • You have at least one function that is decorated with a decorator which Mypy lacks typestubs for.
    • Decorated classes do not appear to trigger this bug.
  • You reference this function from at least one case inside an exhaustive match statement.
  • The function definition appears after the match statement.
    • Moving the decorated function above the match statement is an effective workaround for this bug.
  • You have enabled the exhaustive-match error code.

To Reproduce

Please reference this playground link: https://mypy-play.net/?mypy=latest&python=3.12&flags=ignore-missing-imports&enable-error-code=exhaustive-match&gist=a5cdad420b204b5b0bb2ce278a6513ab

import unknown_module
import enum


class MyEnum(enum.Enum):
    THIS = enum.auto()
    THAT = enum.auto()


def get_fn(e: MyEnum):
    match e:
        case MyEnum.THIS:
            fn = _this
        case MyEnum.THAT:
            fn = _that

    # Using mypy with `exhaustive-match`, we can ignore the `possibly-undefined`
    return fn  # type: ignore[possibly-undefined]


@unknown_module.decorate
def _this():
    pass


@unknown_module.decorate
def _that():
    pass

Expected Behavior

No exhaustive-match error should be emitted, because the match expression is exhaustive.

Actual Behavior

The following error is emitted:

Match statement has unhandled case for values of type "Any | Any"  [exhaustive-match]

The confusion created by this error may be compounded if you have ignore-missing-imports on, because the import-not-found error, which hints at the root cause, would be suppressed.

Your Environment

  • Mypy version used: 1.17.1
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
ignore_missing_imports = true
enable_error_code = exhaustive-match
  • Python version used: 3.11.11

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