Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 opened this issue May 24, 2023 · 0 comments · Fixed by #18663
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-named-tuple

Comments

@Fiona-J-W
Copy link

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
@Fiona-J-W Fiona-J-W added the bug mypy got something wrong label May 24, 2023
@ilevkivskyi ilevkivskyi added topic-match-statement Python 3.10's match statement topic-named-tuple labels Feb 12, 2025
ilevkivskyi added a commit that referenced this issue Feb 12, 2025
Fixes #15299

The fix is straightforward, named tuples should be properly represented
as tuples with fallback, not as instances.
ericmarkmartin pushed a commit to ericmarkmartin/mypy that referenced this issue Feb 19, 2025
Fixes python#15299

The fix is straightforward, named tuples should be properly represented
as tuples with fallback, not as instances.
ericmarkmartin pushed a commit to ericmarkmartin/mypy that referenced this issue Feb 19, 2025
Fixes python#15299

The fix is straightforward, named tuples should be properly represented
as tuples with fallback, not as instances.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-named-tuple
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants