Skip to content

Mypy chokes on matching NamedTuple sub-objects in list[Any] member when using said objects: “Argument […] has incompatible type "K"; expected "K"” #15299

Closed
@Fiona-J-W

Description

@Fiona-J-W

Bug Report

When matching a member-list that is declared to contain Any (I did not check other types here) with [Type() as value] where Type is a NamedTuple, using that value in a seemingly correct way, will state that the type is incompatible with the needed type, despite them being the same (mypy prints the same name!).

To Reproduce

Playground Link

from typing import Any, NamedTuple

class T(NamedTuple):
    t: list[Any]

class K(NamedTuple):
    k: int

def f(t: T) -> None:
    d = set[K]()
    match t:
        case T([K() as k]):
            d.add(k)

Expected Behavior

I would expect this to be accepted (which is what happens if you run the code and also what pyright says), but in the unlikely event that this is really intentional, I would expect a better error-message than the one given.

Actual Behavior

demo.py:13: error: Argument 1 to "add" of "set" has incompatible type "K"; expected "K"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.3
  • Mypy command-line flags: None or --strict (behavior is the same)
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions