diff --git a/pandera/api/dataframe/container.py b/pandera/api/dataframe/container.py index 322017d7d..af59623f9 100644 --- a/pandera/api/dataframe/container.py +++ b/pandera/api/dataframe/container.py @@ -262,7 +262,7 @@ def dtypes(self) -> dict[str, DataType]: ) return {n: c.dtype for n, c in self.columns.items() if not c.regex} - def get_metadata(self) -> dict | None: + def get_metadata(self) -> dict[Any, Any]: """Provide metadata for columns and schema level""" res: dict[Any, Any] = {"columns": {}} for k in self.columns.keys(): diff --git a/pandera/api/dataframe/model.py b/pandera/api/dataframe/model.py index b31979dbc..a3cfc1eeb 100644 --- a/pandera/api/dataframe/model.py +++ b/pandera/api/dataframe/model.py @@ -630,7 +630,7 @@ def _extract_df_parsers( return [parser_info.to_parser(cls) for parser_info in parser_infos] @classmethod - def get_metadata(cls) -> dict | None: + def get_metadata(cls) -> dict[Any, Any]: """Provide metadata for columns and schema level""" res: dict[Any, Any] = {"columns": {}} columns = cls._collect_fields() diff --git a/tests/mypy/pandas_modules/pandera_metadata.py b/tests/mypy/pandas_modules/pandera_metadata.py new file mode 100644 index 000000000..762891f9c --- /dev/null +++ b/tests/mypy/pandas_modules/pandera_metadata.py @@ -0,0 +1,15 @@ +# pylint: skip-file +from typing import Any + +import pandera.pandas as pa + + +class Foo(pa.DataFrameModel): + x: int + + +model_metadata: dict[Any, Any] = Foo.get_metadata() +model_value = model_metadata[str(Foo)] + +schema = Foo.to_schema() +schema_metadata: dict[Any, Any] = schema.get_metadata() diff --git a/tests/mypy/test_pandas_static_type_checking.py b/tests/mypy/test_pandas_static_type_checking.py index d474961f9..ca9fb2b9a 100644 --- a/tests/mypy/test_pandas_static_type_checking.py +++ b/tests/mypy/test_pandas_static_type_checking.py @@ -180,6 +180,7 @@ def test_pandera_runtime_errors(fn) -> None: ["pandera_types.py", "plugin_mypy.ini", []], ["pandas_concat.py", "no_plugin.ini", []], ["pandas_concat.py", "plugin_mypy.ini", []], + ["pandera_metadata.py", "no_plugin.ini", []], ["pandas_time.py", "no_plugin.ini", PANDAS_TIME_ERRORS], ["pandas_time.py", "plugin_mypy.ini", PANDAS_TIME_ERRORS], ["python_slice.py", "no_plugin.ini", PYTHON_SLICE_ERRORS], @@ -292,6 +293,7 @@ def test_pandas_stubs_false_positives( "pandera_inheritance", "pandera_types", "pandas_concat", + "pandera_metadata", "pandas_time", "python_slice", "pandas_index",