Repository: unionai-oss/pandera

Issue:
no exception raise after pipe on 3.11.9
**Describe the bug**
Under python **3.11.9**, using `pipe` function on Dataframe does not rise Exception

- [x] I have checked that this issue has not already been reported.
- [x] I have confirmed this bug exists on the latest version of pandera.
- [ ] (optional) I have confirmed this bug exists on the master branch of pandera.

**Note**: Please read [this guide](https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports) detailing how to provide the necessary information for us to reproduce your bug.

#### Code Sample, a copy-pastable example

```python
import pandera as pa
from pandera.typing import Series, DataFrame
import pandas as pd


class A(pa.DataFrameModel):
    x: Series[int] = pa.Field(
        check_name=True,
    )
    @pa.check("x")
    def check_x(cls, x: Series[int]) -> Series[bool]:
        print("running")
        raise Exception("error")

df = pd.DataFrame({"x": [1, 2, 3]})
# Does not raise exception
df.pipe(DataFrame[A])


```

#### Expected behavior
under python **3.11.8** it raises exception
```python
running
Traceback (most recent call last):
  File "/home/lukaskiss/siren/AWS/market-utils/test.py", line 16, in <module>
    df.pipe(DataFrame[A])
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib64/python3.11/site-packages/pandas/core/generic.py", line 6142, in pipe
    return common.pipe(self, func, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib64/python3.11/site-packages/pandas/core/common.py", line 497, in pipe
    return func(obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/typing.py", line 1277, in __call__
    result.__orig_class__ = self
    ^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/typing/common.py", line 179, in __setattr__
    self.__dict__ = schema_model.validate(self).__dict__
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/api/pandas/model.py", line 306, in validate
    cls.to_schema().validate(
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/api/pandas/container.py", line 375, in validate
    return self._validate(
           ^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/api/pandas/container.py", line 404, in _validate
    return self.get_backend(check_obj).validate(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/container.py", line 97, in validate
    error_handler = self.run_checks_and_handle_errors(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/container.py", line 172, in run_checks_and_handle_errors
    error_handler.collect_error(
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/error_handlers.py", line 38, in collect_error
    raise schema_error from original_exc
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/container.py", line 192, in run_schema_component_checks
    result = schema_component.validate(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/api/pandas/components.py", line 169, in validate
    return self.get_backend(check_obj).validate(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/components.py", line 119, in validate
    validate_column(check_obj, column_name)
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/components.py", line 89, in validate_column
    error_handler.collect_error(err.reason_code, err)
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/error_handlers.py", line 38, in collect_error
    raise schema_error from original_exc
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/components.py", line 68, in validate_column
    validated_check_obj = super(ColumnBackend, self).validate(
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/array.py", line 69, in validate
    error_handler = self.run_checks_and_handle_errors(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/array.py", line 150, in run_checks_and_handle_errors
    error_handler.collect_error(
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/error_handlers.py", line 38, in collect_error
    raise schema_error from original_exc
pandera.errors.SchemaError: Error while executing check function: Exception("error")
Traceback (most recent call last):
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/components.py", line 212, in run_checks
    self.run_check(
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/base.py", line 102, in run_check
    check_result: CheckResult = check(check_obj, *args)
                                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/api/checks.py", line 227, in __call__
    return backend(check_obj, column)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/checks.py", line 295, in __call__
    check_output = self.apply(check_obj)
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/multimethod/__init__.py", line 431, in __call__
    return self[sig](*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/backends/pandas/checks.py", line 143, in apply
    return self.check_fn(check_obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/.cache/pypoetry/virtualenvs/market-utils-mQwKEbgz-py3.11/lib/python3.11/site-packages/pandera/api/base/model_components.py", line 130, in _adapter
    return self.check_fn(model_cls, arg)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukaskiss/siren/AWS/market-utils/test.py", line 13, in check_x
    raise Exception("error")
Exception: error
```

#### Desktop (please complete the following information):

 - OS: [fedora 40]



Discussion:
Yeah, I've noticed 3.11.9 has a bunch of bugs... looking into this, best guess without knowing more is it may have something to do with typing module changes https://www.python.org/downloads/release/python-3119/

Resolving patch:
diff --git a/noxfile.py b/noxfile.py
--- a/noxfile.py
+++ b/noxfile.py
@@ -27,7 +27,7 @@
 )
 
 DEFAULT_PYTHON = "3.8"
-PYTHON_VERSIONS = ["3.8", "3.9", "3.10", "3.11.8"]
+PYTHON_VERSIONS = ["3.8", "3.9", "3.10", "3.11"]
 PANDAS_VERSIONS = ["1.5.3", "2.0.3", "2.2.0"]
 PYDANTIC_VERSIONS = ["1.10.11", "2.3.0"]
 
diff --git a/pandera/typing/common.py b/pandera/typing/common.py
--- a/pandera/typing/common.py
+++ b/pandera/typing/common.py
@@ -2,12 +2,21 @@
 # pylint:disable=abstract-method,too-many-ancestors,invalid-name
 
 import inspect
-from typing import TYPE_CHECKING, Any, Generic, Optional, Type, TypeVar, Union
+from typing import (  # type: ignore[attr-defined]
+    TYPE_CHECKING,
+    Any,
+    Generic,
+    Optional,
+    Type,
+    TypeVar,
+    Union,
+    _GenericAlias,
+)
 
 import pandas as pd
 import typing_inspect
 
-from pandera import dtypes
+from pandera import dtypes, errors
 from pandera.engines import numpy_engine, pandas_engine
 
 Bool = dtypes.Bool  #: ``"bool"`` numpy dtype
@@ -145,6 +154,39 @@
     T = DataFrameModel
 
 
+def __patched_generic_alias_call__(self, *args, **kwargs):
+    """
+    Patched implementation of _GenericAlias.__call__ so that validation errors
+    can be raised when instantiating an instance of pandera DataFrame generics,
+    e.g. DataFrame[A](data).
+    """
+    if not self._inst:
+        raise TypeError(
+            f"Type {self._name} cannot be instantiated; "
+            f"use {self.__origin__.__name__}() instead"
+        )
+    result = self.__origin__(*args, **kwargs)
+    try:
+        result.__orig_class__ = self
+    # Limit the patched behavior to subset of exception types
+    except (
+        TypeError,
+        errors.SchemaError,
+        errors.SchemaError,
+        errors.SchemaInitError,
+        errors.SchemaDefinitionError,
+    ):
+        raise
+    # In python 3.11.9, all exceptions when setting attributes when defining
+    # _GenericAlias subclasses are caught and ignored.
+    except Exception:  # pylint: disable=broad-except
+        pass
+    return result
+
+
+_GenericAlias.__call__ = __patched_generic_alias_call__
+
+
 class DataFrameBase(Generic[T]):
     # pylint: disable=too-few-public-methods
     """


Test patch: