unionai-oss / pandera

A light-weight, flexible, and expressive statistical data testing library
https://www.union.ai/pandera
MIT License
3.37k stars 310 forks source link

check_types decorator cause error with typing.Literal #509

Closed kacky24 closed 3 years ago

kacky24 commented 3 years ago

Describe the bug A clear and concise description of what the bug is.

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

Code Sample, a copy-pastable example

# Your code here
import typing
from typing import Literal

import pandas as pd

import pandera as pa
from pandera.typing import DataFrame, Series

class OutputSchema(pa.SchemaModel):
    id: Series[str]
    name: Series[str]

class Hoge(object):
    @pa.check_types
    def generate(
        self,
        id_: Literal["2", "3"]
    ) -> DataFrame[OutputSchema]:
        df = pd.DataFrame(
            {
                "id": ["1", "2", "3", "4", "5"],
                "name": ["A", "B", "C", "D", "E"]
            }
        )
        df = df[df["id"] == id_]
        return df

if __name__ == "__main__":
    hoge = Hoge()
    df = hoge.generate("2")

Expected behavior

A clear and concise description of what you expected to happen.

This is the error message.

Traceback (most recent call last):
  File "~~~/main.py", line 15, in <module>
    class Hoge(object):
  File "~~~/main.py", line 17, in Hoge
    def generate(
  File "~~~/.venv/lib/python3.9/site-packages/pandera/decorators.py", line 466, in check_types
    if not annotation_info.is_generic_df:
  File "~~~/.venv/lib/python3.9/site-packages/pandera/typing.py", line 116, in is_generic_df
    return self.origin is not None and issubclass(self.origin, DataFrame)
TypeError: issubclass() arg 1 must be a class

https://github.com/pandera-dev/pandera/blob/a0813b7d85c5564fa98554196d459477ca2d272f/pandera/typing.py#L118

annotation_info.origin is generated in annotation_info._parse_annotation(). And when decorated func has args with typing.Literal, annotation_info.origin seems to be typing.Literal. This is not class, so the error occurs at typing.py#L116. https://github.com/pandera-dev/pandera/blob/a0813b7d85c5564fa98554196d459477ca2d272f/pandera/typing.py#L116

Additional context

Add any other context about the problem here.

cosmicBboy commented 3 years ago

thanks for pointing this out @kacky24! https://github.com/pandera-dev/pandera/pull/510 should fix it

kacky24 commented 3 years ago

Thank you!

cosmicBboy commented 3 years ago

@kacky24 this'll be available in the next bugfix release 0.6.5 in the mean time you can do a dev install