Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pytype/abstract/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,19 @@ def __init__(
self._populate_decorator_metadata()
if "__dataclass_fields__" in self.metadata:
self.match_args = tuple(
attr.name for attr in self.metadata["__dataclass_fields__"]
attr.name
for attr in self.metadata["__dataclass_fields__"]
if not attr.kw_only
)
elif self.load_lazy_attribute("__match_args__"):
self.match_args = self._convert_str_tuple("__match_args__") or ()
else:
self.match_args = ()
for base in self.mro[1:]:
if isinstance(base, class_mixin.Class) and hasattr(base, "match_args"):
self.match_args = base.match_args
break
else:
self.match_args = ()

@classmethod
def make(
Expand Down
15 changes: 14 additions & 1 deletion pytype/overlays/classgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,21 @@ def make_init(self, node, cls, attrs, init_method_name="__init__"):
else:
pos_params.append(param)

# If the class has unknown bases or is dynamic, we can't know all possible
# fields, so accept arbitrary positional and keyword arguments.
has_unknown_fields = (
self.ctx.convert.unsolvable in cls.mro or cls.is_dynamic
)

return overlay_utils.make_method(
self.ctx, node, init_method_name, pos_params, 0, kwonly_params
self.ctx,
node,
init_method_name,
pos_params,
posonly_count=0,
kwonly_params=kwonly_params,
varargs=Param("args") if has_unknown_fields else None,
kwargs=Param("kwargs") if has_unknown_fields else None,
)

def call(self, node, func, args, alias_map=None):
Expand Down
7 changes: 6 additions & 1 deletion pytype/overlays/dataclass_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,18 @@ def _match_args_sequentially(self, node, args, alias_map, match_all_views):
fields = obj.cls.metadata["__dataclass_fields__"]
# 0 or more fields can be replaced, so we give every field a default.
default = self.ctx.new_unsolvable(node)
# If the class has unknown bases or is dynamic, we can't know all possible
# fields, so we accept arbitrary keyword arguments via kwargs_name.
has_unknown_fields = (
self.ctx.convert.unsolvable in obj.cls.mro or obj.cls.is_dynamic
)
replace = abstract.SimpleFunction.build(
name=self.name,
param_names=("obj",),
posonly_count=1,
varargs_name=None,
kwonly_params=tuple(f.name for f in fields),
kwargs_name=None,
kwargs_name="kwargs" if has_unknown_fields else None,
defaults={f.name: default for f in fields},
annotations={f.name: f.typ for f in fields},
ctx=self.ctx,
Expand Down
103 changes: 103 additions & 0 deletions pytype/tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,33 @@ def __init__(self, a: bool) -> None: ...
""",
)

def test_init_unknown_base(self):
self.CheckWithErrors("""
import dataclasses
from foo import Base # pytype: disable=import-error
@dataclasses.dataclass
class A(Base):
x: int
A(x=42)
A(x="wrong") # wrong-arg-types
A(x=42, y="from_base")
A(42, "from_base")
""")

def test_init_dynamic_base(self):
self.CheckWithErrors("""
import dataclasses
class Base:
_HAS_DYNAMIC_ATTRIBUTES = True
@dataclasses.dataclass
class A(Base):
x: int
A(x=42)
A(x="wrong") # wrong-arg-types
A(x=42, y="from_base")
A(42, "from_base")
""")

def test_field(self):
ty = self.Infer("""
from typing import List
Expand Down Expand Up @@ -968,6 +995,31 @@ class C:
errors, {"e": ["Expected", "str", "Actual", "int"]}
)

def test_replace_unknown_base(self):
self.CheckWithErrors("""
import dataclasses
from foo import Base # pytype: disable=import-error
@dataclasses.dataclass
class A(Base):
x: int
a = A(x=42)
dataclasses.replace(a, x="wrong") # wrong-arg-types
dataclasses.replace(a, y="from_base")
""")

def test_replace_dynamic_base(self):
self.CheckWithErrors("""
import dataclasses
class Base:
_HAS_DYNAMIC_ATTRIBUTES = True
@dataclasses.dataclass
class A(Base):
x: int
a = A(x=42)
dataclasses.replace(a, x="wrong") # wrong-arg-types
dataclasses.replace(a, y="from_base")
""")


class TestPyiDataclass(test_base.BaseTest):
"""Tests for @dataclasses in pyi files."""
Expand Down Expand Up @@ -1718,6 +1770,57 @@ def f(x, y):
print("not matched")
""")

def test_inheritance(self):
with self.DepTree([(
"foo.pyi",
"""
import dataclasses
@dataclasses.dataclass
class Point:
x: float
y: float

class OtherPoint(Point):
...
""",
)]):
self.Check("""
import foo
def f(x, y):
p = foo.OtherPoint(x, y)
match p:
case foo.OtherPoint(x, y):
print(f"({x}, {y})")
case _:
print("not matched")
""")

def test_kw_only(self):
with self.DepTree([(
"foo.pyi",
"""
import dataclasses
@dataclasses.dataclass
class Point:
x: float
_: dataclasses.KW_ONLY
y: float

class PointWithKwOnly(Point):
...
""",
)]):
self.CheckWithErrors("""
import foo
def f(x, y):
p = foo.PointWithKwOnly(x, y=y)
match p:
case foo.PointWithKwOnly(x, y): # match-error
print(f"({x}, {y})")
case _:
print("not matched")
""")


if __name__ == "__main__":
test_base.main()
Loading