Skip to content

Commit

Permalink
Fix types of inherited attributes in generic dataclasses
Browse files Browse the repository at this point in the history
Fixes #12633.
  • Loading branch information
JukkaL committed Apr 22, 2022
1 parent 07ea0f6 commit ce01710
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 2 additions & 4 deletions mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ def deserialize(
def expand_typevar_from_subtype(self, sub_type: TypeInfo) -> None:
"""Expands type vars in the context of a subtype when an attribute is inherited
from a generic super type."""
if not isinstance(self.type, TypeVarType):
return

self.type = map_type_from_supertype(self.type, sub_type, self.info)
if self.type is not None:
self.type = map_type_from_supertype(self.type, sub_type, self.info)


class DataclassTransformer:
Expand Down
32 changes: 32 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -1568,3 +1568,35 @@ class B:
class Derived(A, B):
pass
[builtins fixtures/dataclasses.pyi]

[case testDataclassGenericInheritance2]
# flags: --python-version 3.7
from dataclasses import dataclass
from typing import Any, Callable, Generic, TypeVar, List

T = TypeVar("T")
S = TypeVar("S")

@dataclass
class Parent(Generic[T]):
f: Callable[[T], Any]

@dataclass
class Child(Parent[T]): ...

class A: ...
def func(obj: A) -> bool: ...

reveal_type(Child[A](func).f) # N: Revealed type is "def (__main__.A) -> Any"

@dataclass
class Parent2(Generic[T]):
a: List[T]

@dataclass
class Child2(Generic[T, S], Parent2[S]):
b: List[T]

reveal_type(Child2([A()], [1]).a) # N: Revealed type is "builtins.list[__main__.A]"
reveal_type(Child2[int, A]([A()], [1]).b) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/dataclasses.pyi]

0 comments on commit ce01710

Please sign in to comment.