Skip to content

attrs match type narrowing failure #13804

@Tinche

Description

@Tinche

Bug Report

Hello Mypy friends!

While playing around with yet another proposal for sum types (typing-sig message forthcoming), I think I found a small issue with attrs and the match statement.

To Reproduce

from typing import Generic, TypeVar

from attrs import frozen
from typing_extensions import assert_never

T = TypeVar("T")


@frozen
class Some(Generic[T]):
    val: T

Expected and Actual Behavior

Now we define a function that handles it.

def takes_opt(opt: Some[int]) -> None:
    match opt:
        case Some(val):
            reveal_type(opt)  # Correct, Some[int]
            reveal_type(opt.val)  # Correct, int
            reveal_type(val)  # Incorrect, Any
            pass
        case _:
            assert_never(opt)

So I'm going to guess it's an issue with how we generate attrs __match_args__, maybe for generic types. I might try solving this myself, or it would be awesome if someone more knowledgeable could solve it first.

If I use dataclasses it works properly.

Your Environment

  • Mypy version used: mypy 0.982 (compiled: yes)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.10.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions