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

TypeError when calling _type_check on parameterized GenericModel in pydantic v1 #1786

Open
2 of 3 tasks
eharkins opened this issue Aug 14, 2024 · 1 comment
Open
2 of 3 tasks
Labels
bug Something isn't working

Comments

@eharkins
Copy link

Describe the bug

I run into the below typeerror when trying to use a generic type with pandera DataFrameModel:

(ihme_cc_liaison_service_py3.9) elias1-N9JTWKGPN2:ihme_cc_liaison_service elias1$ python -m bug_report.py
Traceback (most recent call last):
  File "/Users/elias1/miniconda3/envs/ihme_cc_liaison_service_py3.9/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Users/elias1/miniconda3/envs/ihme_cc_liaison_service_py3.9/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/Users/elias1/projects/ihme_cc_liaison_service/bug_report.py", line 19, in <module>
    def get_envelope() -> LiaisonAPIResponse[Envelope]:
  File "/Users/elias1/miniconda3/envs/ihme_cc_liaison_service_py3.9/lib/python3.9/site-packages/pydantic/generics.py", line 166, in __class_getitem__
    _prepare_model_fields(created_model, fields, instance_type_hints, typevars_map)
  File "/Users/elias1/miniconda3/envs/ihme_cc_liaison_service_py3.9/lib/python3.9/site-packages/pydantic/generics.py", line 396, in _prepare_model_fields
    concrete_type = replace_types(field_type_hint, typevars_map)
  File "/Users/elias1/miniconda3/envs/ihme_cc_liaison_service_py3.9/lib/python3.9/site-packages/pydantic/generics.py", line 298, in replace_types
    return origin_type[resolved_type_args]
  File "/Users/elias1/miniconda3/envs/ihme_cc_liaison_service_py3.9/lib/python3.9/site-packages/pandera/typing/pandas.py", line 92, in __class_getitem__
    _type_check(item, "Parameters to generic types must be types.")
  File "/Users/elias1/miniconda3/envs/ihme_cc_liaison_service_py3.9/lib/python3.9/typing.py", line 166, in _type_check
    raise TypeError(f"{msg} Got {arg!r:.100}.")
TypeError: Parameters to generic types must be types. Got (Envelope,).

Originally raised in pydantic (pydantic/pydantic#10116) but it seems like the issue is in pandera type checking something that is a tuple without accessing the type variable first. The answer there includes a possible solution: pydantic/pydantic#10116 (comment)

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandera.
  • (optional) I have confirmed this bug exists on the main branch of pandera.

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

from typing import Any, Generic, List, Optional, TypeVar

import pandera as pa
from pydantic.generics import GenericModel

DF = TypeVar("DF", bound=pa.DataFrameModel)


class LiaisonAPIResponse(GenericModel, Generic[DF]):
    """Response model returned by Liaison routes."""

    items: pa.typing.DataFrame[DF]

class Envelope(pa.DataFrameModel):
    """Model for dataframe returned by db_queries.get_envelope."""

    age_group_id: pa.typing.Series[int]

def get_envelope() -> LiaisonAPIResponse[Envelope]:
    return LiaisonAPIResponse(items=pd.DataFrame({"age_group_id": [1]}))

if __name__ == "__main__":
    get_envelope()

Expected behavior

Not raise in this case, since the tuple is added to the generic type by pydantic not by my code.

Desktop (please complete the following information):

  • OS: macOS 14.6
  • Browser: n/a
  • Version: 0.20.3
@eharkins eharkins added the bug Something isn't working label Aug 14, 2024
@lzoeckle
Copy link

Bumping this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants