Skip to content

Commit a91189b

Browse files
authored
Add _GenericAlias.__call__ patch (#1561)
* Fixes #1559, add _GenericAlias.__call__ patch Signed-off-by: cosmicBboy <[email protected]> * raise TypeErrors Signed-off-by: cosmicBboy <[email protected]> --------- Signed-off-by: cosmicBboy <[email protected]>
1 parent c42efcf commit a91189b

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

.github/workflows/ci-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
strategy:
3333
fail-fast: true
3434
matrix:
35-
python-version: ["3.8", "3.9", "3.10", "3.11.8"] # python 3.11.9 causes issues with unit tests
35+
python-version: ["3.8", "3.9", "3.10", "3.11"]
3636
defaults:
3737
run:
3838
shell: bash -l {0}
@@ -101,7 +101,7 @@ jobs:
101101
fail-fast: true
102102
matrix:
103103
os: ["ubuntu-latest", "macos-latest", "windows-latest"]
104-
python-version: ["3.8", "3.9", "3.10", "3.11.8"] # python 3.11.9 causes issues with unit tests
104+
python-version: ["3.8", "3.9", "3.10", "3.11"]
105105
pandas-version: ["1.5.3", "2.0.3", "2.2.0"]
106106
pydantic-version: ["1.10.11", "2.3.0"]
107107
include:

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
)
2828

2929
DEFAULT_PYTHON = "3.8"
30-
PYTHON_VERSIONS = ["3.8", "3.9", "3.10", "3.11.8"]
30+
PYTHON_VERSIONS = ["3.8", "3.9", "3.10", "3.11"]
3131
PANDAS_VERSIONS = ["1.5.3", "2.0.3", "2.2.0"]
3232
PYDANTIC_VERSIONS = ["1.10.11", "2.3.0"]
3333

pandera/typing/common.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
# pylint:disable=abstract-method,too-many-ancestors,invalid-name
33

44
import inspect
5-
from typing import TYPE_CHECKING, Any, Generic, Optional, Type, TypeVar, Union
5+
from typing import ( # type: ignore[attr-defined]
6+
TYPE_CHECKING,
7+
Any,
8+
Generic,
9+
Optional,
10+
Type,
11+
TypeVar,
12+
Union,
13+
_GenericAlias,
14+
)
615

716
import pandas as pd
817
import typing_inspect
918

10-
from pandera import dtypes
19+
from pandera import dtypes, errors
1120
from pandera.engines import numpy_engine, pandas_engine
1221

1322
Bool = dtypes.Bool #: ``"bool"`` numpy dtype
@@ -145,6 +154,39 @@
145154
T = DataFrameModel
146155

147156

157+
def __patched_generic_alias_call__(self, *args, **kwargs):
158+
"""
159+
Patched implementation of _GenericAlias.__call__ so that validation errors
160+
can be raised when instantiating an instance of pandera DataFrame generics,
161+
e.g. DataFrame[A](data).
162+
"""
163+
if not self._inst:
164+
raise TypeError(
165+
f"Type {self._name} cannot be instantiated; "
166+
f"use {self.__origin__.__name__}() instead"
167+
)
168+
result = self.__origin__(*args, **kwargs)
169+
try:
170+
result.__orig_class__ = self
171+
# Limit the patched behavior to subset of exception types
172+
except (
173+
TypeError,
174+
errors.SchemaError,
175+
errors.SchemaError,
176+
errors.SchemaInitError,
177+
errors.SchemaDefinitionError,
178+
):
179+
raise
180+
# In python 3.11.9, all exceptions when setting attributes when defining
181+
# _GenericAlias subclasses are caught and ignored.
182+
except Exception: # pylint: disable=broad-except
183+
pass
184+
return result
185+
186+
187+
_GenericAlias.__call__ = __patched_generic_alias_call__
188+
189+
148190
class DataFrameBase(Generic[T]):
149191
# pylint: disable=too-few-public-methods
150192
"""

0 commit comments

Comments
 (0)