Skip to content

Commit 9e38577

Browse files
author
Matt Gilson
committed
Use generic types for config objects.
This is made to be consistent with typeshed best practices: > avoid invariant collection types (list, dict) in argument positions, in favor of covariant types like Mapping or Sequence Signed-off-by: Matt Gilson <[email protected]>
1 parent 850dcf8 commit 9e38577

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

pandera/api/pandas/container.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import warnings
88
from pathlib import Path
9-
from typing import Any, Dict, List, Optional, Union, cast, overload
9+
from typing import Any, Dict, List, Optional, Sequence, Union, cast, overload
1010

1111
import pandas as pd
1212

@@ -47,7 +47,7 @@ def __init__(
4747
strict: StrictType = False,
4848
name: Optional[str] = None,
4949
ordered: bool = False,
50-
unique: Optional[Union[str, List[str]]] = None,
50+
unique: Optional[Union[str, Sequence[str]]] = None,
5151
report_duplicates: UniqueSettings = "all",
5252
unique_column_names: bool = False,
5353
add_missing_columns: bool = False,
@@ -188,12 +188,12 @@ def coerce(self, value: bool) -> None:
188188
self._coerce = value
189189

190190
@property
191-
def unique(self):
191+
def unique(self) -> Optional[Sequence[str]]:
192192
"""List of columns that should be jointly unique."""
193193
return self._unique
194194

195195
@unique.setter
196-
def unique(self, value: Optional[Union[str, List[str]]]) -> None:
196+
def unique(self, value: Optional[Union[str, Sequence[str]]]) -> None:
197197
"""Set unique attribute."""
198198
self._unique = [value] if isinstance(value, str) else value
199199

pandera/api/pandas/model_config.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Class-based dataframe model API configuration for pandas."""
22

3-
from typing import Any, Callable, Dict, List, Optional, Union
3+
from typing import Any, Callable, Mapping, Optional, Sequence, Union
44

55
from pandera.api.base.model_config import BaseModelConfig
66
from pandera.api.pandas.types import PandasDtypeInputTypes, StrictType
@@ -24,7 +24,7 @@ class BaseConfig(BaseModelConfig): # pylint:disable=R0903
2424
drop_invalid_rows: bool = False #: drop invalid rows on validation
2525

2626
#: make sure certain column combinations are unique
27-
unique: Optional[Union[str, List[str]]] = None
27+
unique: Optional[Union[str, Sequence[str]]] = None
2828

2929
#: make sure all specified columns are in the validated dataframe -
3030
#: if ``"filter"``, removes columns not specified in the schema
@@ -61,7 +61,7 @@ class BaseConfig(BaseModelConfig): # pylint:disable=R0903
6161
#: converts the object of type ``from_format`` to a pandera-validate-able
6262
#: data structure. The reader function is implemented in the pandera.typing
6363
#: generic types via the ``from_format`` and ``to_format`` methods.
64-
from_format_kwargs: Optional[Dict[str, Any]] = None
64+
from_format_kwargs: Optional[Mapping[str, Any]] = None
6565

6666
#: data format to serialize into after validation. This option only applies
6767
#: to schemas used in the context of the pandera type constructor
@@ -76,7 +76,7 @@ class BaseConfig(BaseModelConfig): # pylint:disable=R0903
7676
#: converts the pandera-validate-able object to type ``to_format``.
7777
#: The writer function is implemented in the pandera.typing
7878
#: generic types via the ``from_format`` and ``to_format`` methods.
79-
to_format_kwargs: Optional[Dict[str, Any]] = None
79+
to_format_kwargs: Optional[Mapping[str, Any]] = None
8080

8181
#: a dictionary object to store key-value data at schema level
8282
metadata: Optional[dict] = None

pandera/api/pyspark/container.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import warnings
88
from pathlib import Path
9-
from typing import Any, Dict, List, Optional, Union, cast, overload
9+
from typing import Any, Dict, List, Optional, Sequence, Union, cast, overload
1010

1111
from pyspark.sql import DataFrame
1212

@@ -40,7 +40,7 @@ def __init__(
4040
strict: StrictType = False,
4141
name: Optional[str] = None,
4242
ordered: bool = False,
43-
unique: Optional[Union[str, List[str]]] = None,
43+
unique: Optional[Union[str, Sequence[str]]] = None,
4444
report_duplicates: UniqueSettings = "all",
4545
unique_column_names: bool = False,
4646
title: Optional[str] = None,
@@ -169,12 +169,12 @@ def coerce(self, value: bool) -> None:
169169
self._coerce = value
170170

171171
@property
172-
def unique(self):
172+
def unique(self) -> Optional[Sequence[str]]:
173173
"""List of columns that should be jointly unique."""
174174
return self._unique
175175

176176
@unique.setter
177-
def unique(self, value: Optional[Union[str, List[str]]]) -> None:
177+
def unique(self, value: Optional[Union[str, Sequence[str]]]) -> None:
178178
"""Set unique attribute."""
179179
self._unique = [value] if isinstance(value, str) else value
180180

pandera/api/pyspark/model_config.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Class-based dataframe model API configuration for pyspark."""
22

3-
from typing import Any, Callable, Dict, List, Optional, Union
3+
from typing import Any, Callable, Mapping, Optional, Sequence, Union
44

55
from pandera.api.base.model_config import BaseModelConfig
66
from pandera.api.pyspark.types import PySparkDtypeInputTypes, StrictType
@@ -23,7 +23,7 @@ class BaseConfig(BaseModelConfig): # pylint:disable=R0903
2323
coerce: bool = False #: coerce types of all schema components
2424

2525
#: make sure certain column combinations are unique
26-
unique: Optional[Union[str, List[str]]] = None
26+
unique: Optional[Union[str, Sequence[str]]] = None
2727

2828
#: make sure all specified columns are in the validated dataframe -
2929
#: if ``"filter"``, removes columns not specified in the schema
@@ -44,7 +44,7 @@ class BaseConfig(BaseModelConfig): # pylint:disable=R0903
4444
#: converts the object of type ``from_format`` to a pandera-validate-able
4545
#: data structure. The reader function is implemented in the pandera.typing
4646
#: generic types via the ``from_format`` and ``to_format`` methods.
47-
from_format_kwargs: Optional[Dict[str, Any]] = None
47+
from_format_kwargs: Optional[Mapping[str, Any]] = None
4848

4949
#: data format to serialize into after validation. This option only applies
5050
#: to schemas used in the context of the pandera type constructor
@@ -59,7 +59,7 @@ class BaseConfig(BaseModelConfig): # pylint:disable=R0903
5959
#: converts the pandera-validate-able object to type ``to_format``.
6060
#: The writer function is implemented in the pandera.typing
6161
#: generic types via the ``from_format`` and ``to_format`` methods.
62-
to_format_kwargs: Optional[Dict[str, Any]] = None
62+
to_format_kwargs: Optional[Mapping[str, Any]] = None
6363

6464
#: a dictionary object to store key-value data at schema level
6565
metadata: Optional[dict] = None

0 commit comments

Comments
 (0)