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

Commit 8e633de

Browse files
author
Sergey Vasilyev
committed
Clarify optional (nullable) fields: explicit is better than implicit
We are going to do strict type checking. The default values of fields that clearly contradict the declared types is an error for MyPy and all other type checkers and IDEs. Remove the implicit behaviour and make nullable fields explicitly declared as such.
1 parent cddf8a0 commit 8e633de

File tree

6 files changed

+45
-45
lines changed

6 files changed

+45
-45
lines changed

data_diff/databases/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ def set_timezone_to_utc(self) -> str:
815815
@dataclass
816816
class QueryResult:
817817
rows: list
818-
columns: list = None
818+
columns: Optional[list] = None
819819

820820
def __iter__(self):
821821
return iter(self.rows)

data_diff/info_tree.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
class SegmentInfo:
1111
tables: List[TableSegment]
1212

13-
diff: List[Union[Tuple[Any, ...], List[Any]]] = None
14-
diff_schema: Tuple[Tuple[str, type], ...] = None
15-
is_diff: bool = None
16-
diff_count: int = None
13+
diff: Optional[List[Union[Tuple[Any, ...], List[Any]]]] = None
14+
diff_schema: Optional[Tuple[Tuple[str, type], ...]] = None
15+
is_diff: Optional[bool] = None
16+
diff_count: Optional[int] = None
1717

1818
rowcounts: Dict[int, int] = field(default_factory=dict)
19-
max_rows: int = None
19+
max_rows: Optional[int] = None
2020

2121
def set_diff(self, diff: List[Union[Tuple[Any, ...], List[Any]]], schema: Optional[Tuple[Tuple[str, type]]] = None):
2222
self.diff_schema = schema

data_diff/joindiff_tables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class JoinDiffer(TableDiffer):
138138

139139
validate_unique_key: bool = True
140140
sample_exclusive_rows: bool = False
141-
materialize_to_table: DbPath = None
141+
materialize_to_table: Optional[DbPath] = None
142142
materialize_all_rows: bool = False
143143
table_write_limit: int = TABLE_WRITE_LIMIT
144144
skip_null_keys: bool = False

data_diff/queries/ast_classes.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def cast_to(self, to):
5555
@dataclass
5656
class Code(ExprNode, Root):
5757
code: str
58-
args: Dict[str, Expr] = None
58+
args: Optional[Dict[str, Expr]] = None
5959

6060

6161
def _expr_type(e: Expr) -> type:
@@ -216,7 +216,7 @@ def intersect(self, other: "ITable"):
216216
@dataclass
217217
class Concat(ExprNode):
218218
exprs: list
219-
sep: str = None
219+
sep: Optional[str] = None
220220

221221

222222
@dataclass
@@ -293,7 +293,7 @@ class WhenThen(ExprNode):
293293
@dataclass
294294
class CaseWhen(ExprNode):
295295
cases: Sequence[WhenThen]
296-
else_expr: Expr = None
296+
else_expr: Optional[Expr] = None
297297

298298
@property
299299
def type(self):
@@ -491,9 +491,9 @@ def schema(self) -> Schema:
491491
@dataclass
492492
class Join(ExprNode, ITable, Root):
493493
source_tables: Sequence[ITable]
494-
op: str = None
495-
on_exprs: Sequence[Expr] = None
496-
columns: Sequence[Expr] = None
494+
op: Optional[str] = None
495+
on_exprs: Optional[Sequence[Expr]] = None
496+
columns: Optional[Sequence[Expr]] = None
497497

498498
@property
499499
def schema(self) -> Schema:
@@ -534,9 +534,9 @@ def select(self, *exprs, **named_exprs) -> Union[Self, ITable]:
534534
@dataclass
535535
class GroupBy(ExprNode, ITable, Root):
536536
table: ITable
537-
keys: Sequence[Expr] = None # IKey?
538-
values: Sequence[Expr] = None
539-
having_exprs: Sequence[Expr] = None
537+
keys: Optional[Sequence[Expr]] = None # IKey?
538+
values: Optional[Sequence[Expr]] = None
539+
having_exprs: Optional[Sequence[Expr]] = None
540540

541541
def __post_init__(self):
542542
assert self.keys or self.values
@@ -580,15 +580,15 @@ def schema(self) -> Schema:
580580

581581
@dataclass
582582
class Select(ExprNode, ITable, Root):
583-
table: Expr = None
584-
columns: Sequence[Expr] = None
585-
where_exprs: Sequence[Expr] = None
586-
order_by_exprs: Sequence[Expr] = None
587-
group_by_exprs: Sequence[Expr] = None
588-
having_exprs: Sequence[Expr] = None
589-
limit_expr: int = None
583+
table: Optional[Expr] = None
584+
columns: Optional[Sequence[Expr]] = None
585+
where_exprs: Optional[Sequence[Expr]] = None
586+
order_by_exprs: Optional[Sequence[Expr]] = None
587+
group_by_exprs: Optional[Sequence[Expr]] = None
588+
having_exprs: Optional[Sequence[Expr]] = None
589+
limit_expr: Optional[int] = None
590590
distinct: bool = False
591-
optimizer_hints: Sequence[Expr] = None
591+
optimizer_hints: Optional[Sequence[Expr]] = None
592592

593593
@property
594594
def schema(self) -> Schema:
@@ -636,8 +636,8 @@ def make(cls, table: ITable, distinct: bool = SKIP, optimizer_hints: str = SKIP,
636636
@dataclass
637637
class Cte(ExprNode, ITable):
638638
table: Expr
639-
name: str = None
640-
params: Sequence[str] = None
639+
name: Optional[str] = None
640+
params: Optional[Sequence[str]] = None
641641

642642
@property
643643
def source_table(self) -> "ITable":
@@ -667,7 +667,7 @@ def resolve_names(source_table, exprs):
667667
@dataclass(frozen=False, eq=False, order=False)
668668
class _ResolveColumn(ExprNode, LazyOps):
669669
resolve_name: str
670-
resolved: Expr = None
670+
resolved: Optional[Expr] = None
671671

672672
def resolve(self, expr: Expr):
673673
if self.resolved is not None:
@@ -750,9 +750,9 @@ def type(self) -> Optional[type]:
750750
class TimeTravel(ITable):
751751
table: TablePath
752752
before: bool = False
753-
timestamp: datetime = None
754-
offset: int = None
755-
statement: str = None
753+
timestamp: Optional[datetime] = None
754+
offset: Optional[int] = None
755+
statement: Optional[str] = None
756756

757757

758758
# DDL
@@ -767,9 +767,9 @@ def type(self) -> Optional[type]:
767767
@dataclass
768768
class CreateTable(Statement):
769769
path: TablePath
770-
source_table: Expr = None
770+
source_table: Optional[Expr] = None
771771
if_not_exists: bool = False
772-
primary_keys: List[str] = None
772+
primary_keys: Optional[List[str]] = None
773773

774774

775775
@dataclass
@@ -787,8 +787,8 @@ class TruncateTable(Statement):
787787
class InsertToTable(Statement):
788788
path: TablePath
789789
expr: Expr
790-
columns: List[str] = None
791-
returning_exprs: List[str] = None
790+
columns: Optional[List[str]] = None
791+
returning_exprs: Optional[List[str]] = None
792792

793793
def returning(self, *exprs) -> Self:
794794
"""Add a 'RETURNING' clause to the insert expression.

data_diff/queries/extras.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@dataclass
1111
class NormalizeAsString(ExprNode):
1212
expr: ExprNode
13-
expr_type: ColType = None
13+
expr_type: Optional[ColType] = None
1414

1515
@property
1616
def type(self) -> Optional[type]:
@@ -20,7 +20,7 @@ def type(self) -> Optional[type]:
2020
@dataclass
2121
class ApplyFuncAndNormalizeAsString(ExprNode):
2222
expr: ExprNode
23-
apply_func: Callable = None
23+
apply_func: Optional[Callable] = None
2424

2525

2626
@dataclass

data_diff/table_segment.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,18 @@ class TableSegment:
112112

113113
# Columns
114114
key_columns: Tuple[str, ...]
115-
update_column: str = None
115+
update_column: Optional[str] = None
116116
extra_columns: Tuple[str, ...] = ()
117117

118118
# Restrict the segment
119-
min_key: Vector = None
120-
max_key: Vector = None
121-
min_update: DbTime = None
122-
max_update: DbTime = None
123-
where: str = None
124-
125-
case_sensitive: bool = True
126-
_schema: Schema = None
119+
min_key: Optional[Vector] = None
120+
max_key: Optional[Vector] = None
121+
min_update: Optional[DbTime] = None
122+
max_update: Optional[DbTime] = None
123+
where: Optional[str] = None
124+
125+
case_sensitive: Optional[bool] = True
126+
_schema: Optional[Schema] = None
127127

128128
def __post_init__(self):
129129
if not self.update_column and (self.min_update or self.max_update):

0 commit comments

Comments
 (0)