Skip to content

Unexhaustive pattern match on dataclass field union #9429

Answered by erictraut
ducdetronquito asked this question in Q&A
Discussion options

You must be logged in to vote

Exhaustion detection uses type narrowing to determine whether you have handled all possible types. In this case, the subject type starts as User. The subsequent case statements do not narrow the type further. The type of the subject remains User after the first and the second. There is no notion in the Python type system of the type User where the "role" attribute refers to a "Classic" object.

If you want static analysis exhaustion to work, you'll need to change your code somewhat.

Code sample in pyright playground

# pyright: strict

from dataclasses import dataclass

@dataclass
class Classic:
    pass

@dataclass
class Admin:
    pass

@dataclass
class User[T: Classic | Admin]:
    name: s…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ducdetronquito
Comment options

Answer selected by ducdetronquito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants