From 0837bac748f01b769f7c9c5c03e1fa4dc80cc6fe Mon Sep 17 00:00:00 2001 From: nanne-aben <47976799+nanne-aben@users.noreply.github.com> Date: Wed, 11 Oct 2023 08:51:01 +0200 Subject: [PATCH] update --- .github/workflows/build.yml | 2 +- strictly_typed_pandas/typeguard.py | 29 +++++++---------------------- tests/test_dataset.py | 3 +++ tests/test_indexed_dataset.py | 3 +++ 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 99a75ea..7981a4f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -29,7 +29,7 @@ jobs: mypy . - name: Test with pytest run: | - coverage run -m pytest --typeguard-packages=strictly_typed_pandas,tests + coverage run -m pytest coverage report -m - name: Run notebooks run: | diff --git a/strictly_typed_pandas/typeguard.py b/strictly_typed_pandas/typeguard.py index 58ceb2b..640577c 100644 --- a/strictly_typed_pandas/typeguard.py +++ b/strictly_typed_pandas/typeguard.py @@ -7,10 +7,7 @@ def check_dataset(value: Any, origin_type: Any, args: Tuple[Any, ...], memo: typeguard.TypeCheckMemo) -> None: schema_expected = args[0] if not isinstance(value, DataSet): - msg = "Type must be a DataSet[{schema_expected}]; got {class_observed} instead".format( - schema_expected=typeguard.qualified_name(schema_expected), - class_observed=typeguard.qualified_name(value) - ) + msg = f"Type must be a DataSet[{schema_expected}]; got {value} instead" if memo.config.typecheck_fail_callback: memo.config.typecheck_fail_callback(typeguard.TypeCheckError(msg), memo) else: @@ -18,10 +15,7 @@ def check_dataset(value: Any, origin_type: Any, args: Tuple[Any, ...], memo: ty schema_observed = value.__orig_class__.__args__[0] if schema_observed != schema_expected: - msg = "Type must be a DataSet[{schema_expected}]; got DataSet[{schema_observed}] instead".format( - schema_expected=typeguard.qualified_name(schema_expected), - schema_observed=typeguard.qualified_name(schema_observed) - ) + msg = f"Type must be a DataSet[{schema_expected}]; got DataSet[{schema_observed}] instead" if memo.config.typecheck_fail_callback: memo.config.typecheck_fail_callback(typeguard.TypeCheckError(msg), memo) else: @@ -33,13 +27,9 @@ def check_indexed_dataset(value: Any, origin_type: Any, args: Tuple[Any, ...], schema_data_expected = args[1] if not isinstance(value, IndexedDataSet): msg = ( - "Type must be a IndexedDataSet[{schema_index_expected},{schema_data_expected}];" + - "got {class_observed} instead" - ).format( - schema_index_expected=typeguard.qualified_name(schema_index_expected), - schema_data_expected=typeguard.qualified_name(schema_data_expected), - class_observed=typeguard.qualified_name(value) - ) + f"Type must be a IndexedDataSet[{schema_index_expected},{schema_data_expected}]; " + + f"got {value} instead" + ) if memo.config.typecheck_fail_callback: memo.config.typecheck_fail_callback(typeguard.TypeCheckError(msg), memo) @@ -50,13 +40,8 @@ def check_indexed_dataset(value: Any, origin_type: Any, args: Tuple[Any, ...], schema_data_observed = value.__orig_class__.__args__[1] if schema_index_observed != schema_index_expected or schema_data_observed != schema_data_expected: msg = ( - "Type must be a IndexedDataSet[{schema_index_expected},{schema_data_expected}];" + - "got IndexedDataSet[{schema_index_observed},{schema_data_observed}] instead" - ).format( - schema_index_expected=typeguard.qualified_name(schema_index_expected), - schema_data_expected=typeguard.qualified_name(schema_data_expected), - schema_index_observed=typeguard.qualified_name(schema_index_observed), - schema_data_observed=typeguard.qualified_name(schema_data_observed) + f"Type must be a IndexedDataSet[{schema_index_expected},{schema_data_expected}];" + + f"got IndexedDataSet[{schema_index_observed},{schema_data_observed}] instead" ) if memo.config.typecheck_fail_callback: memo.config.typecheck_fail_callback(typeguard.TypeCheckError(msg), memo) diff --git a/tests/test_dataset.py b/tests/test_dataset.py index 542cfc4..a318ad1 100644 --- a/tests/test_dataset.py +++ b/tests/test_dataset.py @@ -2,6 +2,8 @@ import pandas as pd import numpy as np # type: ignore +from typeguard import typechecked + from strictly_typed_pandas import DataSet from strictly_typed_pandas.pandas_types import StringDtype @@ -93,6 +95,7 @@ def test_dataset_to_dataframe() -> None: assert isinstance(df.to_frame(), pd.DataFrame) +@typechecked def foo(df: DataSet[Schema]) -> DataSet[Schema]: return df diff --git a/tests/test_indexed_dataset.py b/tests/test_indexed_dataset.py index 971c115..98f352b 100644 --- a/tests/test_indexed_dataset.py +++ b/tests/test_indexed_dataset.py @@ -2,6 +2,8 @@ import pandas as pd import numpy as np # type: ignore +from typeguard import typechecked + from strictly_typed_pandas import IndexedDataSet from strictly_typed_pandas.pandas_types import StringDtype @@ -58,6 +60,7 @@ def test_overlapping_columns(): IndexedDataSet[IndexSchema, IndexSchema]() +@typechecked def foo(df: IndexedDataSet[IndexSchema, DataSchema]) -> IndexedDataSet[IndexSchema, DataSchema]: return df