Skip to content

new generics should be inferred as covariant instead of invariant on frozen dataclasses #17692

Closed as duplicate

Description

@DetachHead
# mypy: enable-incomplete-feature=NewGenericSyntax
from dataclasses import dataclass

@dataclass(frozen=True)
class Foo[T]:
    value: T


foo = Foo[int](value=1)
bar: Foo[object] = foo # error: Incompatible types in assignment (expression has type "Foo[int]", variable has type "Foo[object]")  [assignment]

playground

since value is immutable, T is only used in an output position therefore the generic should be inferred as covariant

Activity

  1. changed the title [-]new generics shouyld be inferred as covariant instead of invariant on frozen dataclasses[/-] [+]new generics should be inferred as covariant instead of invariant on frozen dataclasses[/+] on Aug 19, 2024
  2. smheidrich commented on Aug 20, 2025

    @smheidrich
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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