pandera icon indicating copy to clipboard operation
pandera copied to clipboard

Accept NewType for @pa.check_types

Open pprados opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. I want to use the Python typing, with mypy or pytype. I want to declare a Schema and a New Type.

With My_type = pa.typing.DataFrame[My_schema], the @pa.check_types check the parameters. With My_type = NewType("My_type", pa.typing.DataFrame[My_schema]), the @pa.check_types never check the parameters.

Describe the solution you'd like A clear and concise description of what you want to happen.

import pandas as pd
import pandera as pa
from typing import NewType
from pandera.typing import Index, DataFrame, Series

class My_schema(pa.SchemaModel):
    id: Index[int]
    data: Series[int]
    class Config:
        strict = True
        ordered = True
        
#My_type = pa.typing.DataFrame[My_schema]  # @pa.check_types check the parameters
My_type = NewType("My_type", pa.typing.DataFrame[My_schema]) # @pa.check_types never check the parameters

@pa.check_types
def f(data: My_type) -> My_type:
    #data['data'] = 2
    return data

f(pd.DataFrame({"xdata": [1, 2]}))  # Error ?

The @check_types may accept NewType.

pprados avatar Jul 18 '22 12:07 pprados

Hi @pprados just so I understand the use case, why are you using NewType instead of using pa.typing.DataFrame[My_schema] directly (or a type alias MyType = pa.typing.DataFrame[My_schema]?

cosmicBboy avatar Jul 20 '22 14:07 cosmicBboy