Skip to content

Commit

Permalink
Use generic types for config objects.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Matt Gilson committed Aug 30, 2023
1 parent 850dcf8 commit 9e38577
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions pandera/api/pandas/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import warnings
from pathlib import Path
from typing import Any, Dict, List, Optional, Union, cast, overload
from typing import Any, Dict, List, Optional, Sequence, Union, cast, overload

import pandas as pd

Expand Down Expand Up @@ -47,7 +47,7 @@ def __init__(
strict: StrictType = False,
name: Optional[str] = None,
ordered: bool = False,
unique: Optional[Union[str, List[str]]] = None,
unique: Optional[Union[str, Sequence[str]]] = None,
report_duplicates: UniqueSettings = "all",
unique_column_names: bool = False,
add_missing_columns: bool = False,
Expand Down Expand Up @@ -188,12 +188,12 @@ def coerce(self, value: bool) -> None:
self._coerce = value

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

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

Expand Down
8 changes: 4 additions & 4 deletions pandera/api/pandas/model_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Class-based dataframe model API configuration for pandas."""

from typing import Any, Callable, Dict, List, Optional, Union
from typing import Any, Callable, Mapping, Optional, Sequence, Union

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

#: make sure certain column combinations are unique
unique: Optional[Union[str, List[str]]] = None
unique: Optional[Union[str, Sequence[str]]] = None

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

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

#: a dictionary object to store key-value data at schema level
metadata: Optional[dict] = None
8 changes: 4 additions & 4 deletions pandera/api/pyspark/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import warnings
from pathlib import Path
from typing import Any, Dict, List, Optional, Union, cast, overload
from typing import Any, Dict, List, Optional, Sequence, Union, cast, overload

Check warning on line 9 in pandera/api/pyspark/container.py

View check run for this annotation

Codecov / codecov/patch

pandera/api/pyspark/container.py#L9

Added line #L9 was not covered by tests

from pyspark.sql import DataFrame

Expand Down Expand Up @@ -40,7 +40,7 @@ def __init__(
strict: StrictType = False,
name: Optional[str] = None,
ordered: bool = False,
unique: Optional[Union[str, List[str]]] = None,
unique: Optional[Union[str, Sequence[str]]] = None,
report_duplicates: UniqueSettings = "all",
unique_column_names: bool = False,
title: Optional[str] = None,
Expand Down Expand Up @@ -169,12 +169,12 @@ def coerce(self, value: bool) -> None:
self._coerce = value

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

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

Expand Down
8 changes: 4 additions & 4 deletions pandera/api/pyspark/model_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Class-based dataframe model API configuration for pyspark."""

from typing import Any, Callable, Dict, List, Optional, Union
from typing import Any, Callable, Mapping, Optional, Sequence, Union

Check warning on line 3 in pandera/api/pyspark/model_config.py

View check run for this annotation

Codecov / codecov/patch

pandera/api/pyspark/model_config.py#L3

Added line #L3 was not covered by tests

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

#: make sure certain column combinations are unique
unique: Optional[Union[str, List[str]]] = None
unique: Optional[Union[str, Sequence[str]]] = None

Check warning on line 26 in pandera/api/pyspark/model_config.py

View check run for this annotation

Codecov / codecov/patch

pandera/api/pyspark/model_config.py#L26

Added line #L26 was not covered by tests

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

Check warning on line 47 in pandera/api/pyspark/model_config.py

View check run for this annotation

Codecov / codecov/patch

pandera/api/pyspark/model_config.py#L47

Added line #L47 was not covered by tests

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

Check warning on line 62 in pandera/api/pyspark/model_config.py

View check run for this annotation

Codecov / codecov/patch

pandera/api/pyspark/model_config.py#L62

Added line #L62 was not covered by tests

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

0 comments on commit 9e38577

Please sign in to comment.