Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 4091e49

Browse files
author
Sergey Vasilyev
committed
Convert class-level constants to properties for attrs compatibility
`attrs` cannot use multiple inheritance when both parents introduce their attributes (as documented). Only one side can inherit the attributes, other bases must be pure interfaces/protocols. Reimplement the `ExprNode.type` via properties to exclude it from the sight of `attrs`.
1 parent 9bd8357 commit 4091e49

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

data_diff/abcs/database_types.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
@dataclass
1717
class ColType:
18-
supported = True
18+
@property
19+
def supported(self) -> bool:
20+
return True
1921

2022

2123
@dataclass
@@ -132,7 +134,9 @@ def make_value(self, value):
132134

133135
@dataclass
134136
class Text(StringType):
135-
supported = False
137+
@property
138+
def supported(self) -> bool:
139+
return False
136140

137141

138142
# In majority of DBMSes, it is called JSON/JSONB. Only in Snowflake, it is OBJECT.
@@ -169,4 +173,6 @@ def __post_init__(self):
169173
class UnknownColType(ColType):
170174
text: str
171175

172-
supported = False
176+
@property
177+
def supported(self) -> bool:
178+
return False

data_diff/queries/ast_classes.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ class Root:
2828
class ExprNode(Compilable):
2929
"Base class for query expression nodes"
3030

31-
type: Any = None
31+
@property
32+
def type(self) -> Optional[type]:
33+
return None
3234

3335
def _dfs_values(self):
3436
yield self
@@ -216,7 +218,10 @@ class Concat(ExprNode):
216218
class Count(ExprNode):
217219
expr: Expr = None
218220
distinct: bool = False
219-
type = int
221+
222+
@property
223+
def type(self) -> Optional[type]:
224+
return int
220225

221226

222227
class LazyOps:
@@ -337,7 +342,10 @@ def then(self, then: Expr) -> CaseWhen:
337342
class IsDistinctFrom(ExprNode, LazyOps):
338343
a: Expr
339344
b: Expr
340-
type = bool
345+
346+
@property
347+
def type(self) -> Optional[type]:
348+
return bool
341349

342350

343351
@dataclass(eq=False, order=False)
@@ -361,7 +369,9 @@ class UnaryOp(ExprNode, LazyOps):
361369

362370

363371
class BinBoolOp(BinOp):
364-
type = bool
372+
@property
373+
def type(self) -> Optional[type]:
374+
return bool
365375

366376

367377
@dataclass(eq=False, order=False)
@@ -700,7 +710,10 @@ def __getitem__(self, name):
700710
class In(ExprNode):
701711
expr: Expr
702712
list: Sequence[Expr]
703-
type = bool
713+
714+
@property
715+
def type(self) -> Optional[type]:
716+
return bool
704717

705718

706719
@dataclass
@@ -711,7 +724,9 @@ class Cast(ExprNode):
711724

712725
@dataclass
713726
class Random(ExprNode, LazyOps):
714-
type = float
727+
@property
728+
def type(self) -> Optional[type]:
729+
return float
715730

716731

717732
@dataclass
@@ -722,11 +737,16 @@ class ConstantTable(ExprNode):
722737
@dataclass
723738
class Explain(ExprNode, Root):
724739
select: Select
725-
type = str
740+
741+
@property
742+
def type(self) -> Optional[type]:
743+
return str
726744

727745

728746
class CurrentTimestamp(ExprNode):
729-
type = datetime
747+
@property
748+
def type(self) -> Optional[type]:
749+
return datetime
730750

731751

732752
@dataclass
@@ -742,7 +762,9 @@ class TimeTravel(ITable):
742762

743763

744764
class Statement(Compilable, Root):
745-
type = None
765+
@property
766+
def type(self) -> Optional[type]:
767+
return None
746768

747769

748770
@dataclass

data_diff/queries/extras.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"Useful AST classes that don't quite fall within the scope of regular SQL"
2-
from typing import Callable, Sequence
2+
from typing import Callable, Optional, Sequence
33
from runtype import dataclass
44

55
from data_diff.abcs.database_types import ColType
@@ -11,7 +11,10 @@
1111
class NormalizeAsString(ExprNode):
1212
expr: ExprNode
1313
expr_type: ColType = None
14-
type = str
14+
15+
@property
16+
def type(self) -> Optional[type]:
17+
return str
1518

1619

1720
@dataclass

0 commit comments

Comments
 (0)