Skip to content

Commit

Permalink
POST REVIEW: appease mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
sf-dcp committed Jan 14, 2025
1 parent 727426f commit f874c8c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
12 changes: 8 additions & 4 deletions dcpy/lifecycle/validate/pandera_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
signature,
) # used for checking expected attributes in a class signuture

from dcpy.models.dataset import Column, CheckAttributes
from dcpy.models.dataset import Column, CheckAttributes, Checks


def create_check(check: str | dict[str, CheckAttributes]) -> pa.Check:
Expand All @@ -15,7 +15,7 @@ def create_check(check: str | dict[str, CheckAttributes]) -> pa.Check:
Args:
check:
A string representing the name of the check or a dictionary with the
check name as the key and a dictionary of attributes as the value.
check name as the key and check attibutes as the value.
Returns:
pa.Check:
A Pandera `Check` object constructed with the specified parameters.
Expand Down Expand Up @@ -85,12 +85,16 @@ def create_check(check: str | dict[str, CheckAttributes]) -> pa.Check:

def create_checks(checks: list[str | dict[str, CheckAttributes]]) -> list[pa.Check]:
"""Create Pandera checks."""
checks = [create_check(check) for check in checks]
return checks
pandera_checks = [create_check(check) for check in checks]
return pandera_checks


def create_column_with_checks(column: Column) -> pa.Column:
"""Create Pandera column validator object."""
if isinstance(column.checks, Checks):
raise NotImplementedError(

Check warning on line 95 in dcpy/lifecycle/validate/pandera_utils.py

View check run for this annotation

Codecov / codecov/patch

dcpy/lifecycle/validate/pandera_utils.py#L95

Added line #L95 was not covered by tests
"Pandera checks are not implemented for old Column.checks format"
)
data_checks = create_checks(column.checks) if column.checks else None
return pa.Column(
# TODO: implement `dtype` param
Expand Down
33 changes: 18 additions & 15 deletions dcpy/test/lifecycle/validate/test_pandera_custom_checks.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import pytest
import pandera as pa
from shapely.geometry import Point, LineString, Polygon
import pandas as pd
import geopandas as gpd

from dcpy.models.dataset import Column
from dcpy.lifecycle.validate import pandera_utils


def test_is_geom_point_valid_points():
df = pd.DataFrame(
gdf = gpd.GeoDataFrame(
{
"geometry": [
Point(1, 1),
Point(2, 3),
]
"geometry": gpd.GeoSeries.from_wkt(
[
"POINT (1 1)",
"POINT (2 3)",
]
)
}
)
pandera_utils.run_data_checks(
df=df,
df=gdf,
columns=[Column(id="geometry", checks=["is_geom_point"])],
)


def test_is_geom_point_invalid_geoms():
df = pd.DataFrame(
gdf = gpd.GeoDataFrame(
{
"geometry": [
Point(1, 1),
LineString([(0, 0), (1, 1)]),
Polygon([(0, 0), (1, 1), (1, 0)]),
]
"geometry": gpd.GeoSeries.from_wkt(
[
"POINT (1 1)",
"LINESTRING (0 0, 1 1)",
"POLYGON ((0 0, 1 1, 1 0, 0 0))",
]
)
}
)
with pytest.raises(pa.errors.SchemaError):
pandera_utils.run_data_checks(
df=df,
df=gdf,
columns=[Column(id="geometry", checks=["is_geom_point"])],
)

0 comments on commit f874c8c

Please sign in to comment.