Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add List, Dict, Tuple and NamedTuple to the GenericDType bound #1556

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions pandera/typing/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Common typing functionality."""

# pylint:disable=abstract-method,too-many-ancestors,invalid-name

import copy
Expand All @@ -11,6 +12,10 @@
Type,
TypeVar,
Union,
List,
Dict,
Tuple,
NamedTuple,
_GenericAlias,
)

Expand Down Expand Up @@ -71,6 +76,10 @@ class Geometry: # type: ignore [no-redef]
int,
str,
float,
List[Any],
Dict[Any, Any],
Tuple[Any],
NamedTuple,
pd.core.dtypes.base.ExtensionDtype,
Bool,
Date,
Expand Down
32 changes: 31 additions & 1 deletion tests/core/test_typing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test typing annotations for the model api."""
# pylint:disable=missing-class-docstring,too-few-public-methods
import re
from typing import Any, Dict, Optional, Type
from typing import Any, Dict, List, NamedTuple, Optional, Tuple, Type

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -487,3 +487,33 @@ def test_init_pandas_dataframe_errors(invalid_data):
"""Test errors from initializing a pandas.typing.DataFrame with Schema."""
with pytest.raises(pa.errors.SchemaError):
DataFrame[InitSchema](invalid_data)


class ExampleNamedTuple(NamedTuple):
a: int
b: float


class SchemaComplexPythonCollectionTypes(pa.DataFrameModel):
list: List[pa.typing.Int32]
dict: Dict[str, pa.typing.Int32]
tuple2: Tuple[pa.typing.Int32, pa.typing.Int32]
named_tuple: ExampleNamedTuple


def test_complex_python_collection_types():
"""Test complex python collection types."""
assert isinstance(
DataFrame[SchemaComplexPythonCollectionTypes](
{
"list": [[1, 2], [3, 4, 5]],
"dict": [{"a": 1, "b": 2}, {"c": 1, "d": 2}],
"tuple2": [[6, 7], [8, 9]],
"named_tuple": [
ExampleNamedTuple(1, 2),
ExampleNamedTuple(3, 4),
],
}
),
DataFrame,
)
Loading