Skip to content

Why is this dataclasses inferred to be invariant? #8733

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

You must be logged in to vote

I was wondering that myself, so I dug into it. The difference is that attr default to order=True whereas the stdlib dataclass does not. When order=True, methods are synthesized for __lt__, __gt__, etc. The signature that pyright generates for these comparison operations uses a parameter annotation of other: Foo[T]. This causes the variance of T to be invariant.

I think it's better for these synthesized methods to use Self rather than Foo[T] in this case. This preserves the covariance of T. I've made this change, and it will be included in the next release of pyright.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@mezuzza
Comment options

@mezuzza
Comment options

@erictraut
Comment options

Answer selected by mezuzza
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