Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
nanne-aben authored and caneff committed Oct 17, 2023
1 parent af87590 commit 0837bac
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
29 changes: 7 additions & 22 deletions strictly_typed_pandas/typeguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,15 @@
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:
raise TypeError(msg)

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:
Expand All @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions tests/test_indexed_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -58,6 +60,7 @@ def test_overlapping_columns():
IndexedDataSet[IndexSchema, IndexSchema]()


@typechecked
def foo(df: IndexedDataSet[IndexSchema, DataSchema]) -> IndexedDataSet[IndexSchema, DataSchema]:
return df

Expand Down

0 comments on commit 0837bac

Please sign in to comment.