Skip to content

Commit

Permalink
allow mypy scanning more code in tests
Browse files Browse the repository at this point in the history
This is where we check for type issues.
  • Loading branch information
benbovy committed Dec 6, 2024
1 parent 53bf81a commit 71bbbd0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 58 deletions.
12 changes: 6 additions & 6 deletions tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_distance_with_custom_radius() -> None:
assert actual == pytest.approx(np.pi / 2)


def test_area():
def test_area() -> None:
# scalar
geog = spherely.create_polygon([(0, 0), (90, 0), (0, 90), (0, 0)])
result = spherely.area(geog, radius=1)
Expand Down Expand Up @@ -191,11 +191,11 @@ def test_area():
"POLYGON EMPTY",
],
)
def test_area_empty(geog):
def test_area_empty(geog) -> None:
assert spherely.area(spherely.from_wkt(geog)) == 0


def test_length():
def test_length() -> None:
geog = spherely.create_linestring([(0, 0), (1, 0)])
result = spherely.length(geog, radius=1)
assert isinstance(result, float)
Expand All @@ -218,11 +218,11 @@ def test_length():
"POLYGON ((0 0, 0 1, 1 0, 0 0))",
],
)
def test_length_invalid(geog):
def test_length_invalid(geog) -> None:
assert spherely.length(spherely.from_wkt(geog)) == 0.0


def test_perimeter():
def test_perimeter() -> None:
geog = spherely.create_polygon([(0, 0), (0, 90), (90, 90), (90, 0), (0, 0)])
result = spherely.perimeter(geog, radius=1)
assert isinstance(result, float)
Expand All @@ -239,5 +239,5 @@ def test_perimeter():
@pytest.mark.parametrize(
"geog", ["POINT (0 0)", "POINT EMPTY", "LINESTRING (0 0, 1 0)", "POLYGON EMPTY"]
)
def test_perimeter_invalid(geog):
def test_perimeter_invalid(geog) -> None:
assert spherely.perimeter(spherely.from_wkt(geog)) == 0.0
20 changes: 10 additions & 10 deletions tests/test_boolean_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
),
],
)
def test_union(geog1, geog2, expected):
def test_union(geog1, geog2, expected) -> None:
result = spherely.union(spherely.from_wkt(geog1), spherely.from_wkt(geog2))
assert str(result) == expected

Expand All @@ -47,12 +47,12 @@ def test_union_polygon():
),
],
)
def test_intersection(geog1, geog2, expected):
def test_intersection(geog1, geog2, expected) -> None:
result = spherely.intersection(spherely.from_wkt(geog1), spherely.from_wkt(geog2))
assert str(result) == expected


def test_intersection_empty():
def test_intersection_empty() -> None:
result = spherely.intersection(poly1, spherely.from_wkt("POLYGON EMPTY"))
# assert spherely.is_empty(result)
assert str(result) == "GEOMETRYCOLLECTION EMPTY"
Expand All @@ -66,7 +66,7 @@ def test_intersection_empty():
assert str(result) == "GEOMETRYCOLLECTION EMPTY"


def test_intersection_lines():
def test_intersection_lines() -> None:
result = spherely.intersection(
spherely.from_wkt("LINESTRING (-45 0, 45 0)"),
spherely.from_wkt("LINESTRING (0 -10, 0 10)"),
Expand All @@ -75,7 +75,7 @@ def test_intersection_lines():
assert spherely.distance(result, spherely.from_wkt("POINT (0 0)")) == 0


def test_intersection_polygons():
def test_intersection_polygons() -> None:
result = spherely.intersection(poly1, poly2)
# TODO precision could be higher with snap level
precision = 2 if Version(spherely.__s2geography_version__) < Version("0.2.0") else 1
Expand All @@ -85,7 +85,7 @@ def test_intersection_polygons():
)


def test_intersection_polygon_model():
def test_intersection_polygon_model() -> None:
poly = spherely.from_wkt("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))")
point = spherely.from_wkt("POINT (0 0)")

Expand All @@ -107,12 +107,12 @@ def test_intersection_polygon_model():
),
],
)
def test_difference(geog1, geog2, expected):
def test_difference(geog1, geog2, expected) -> None:
result = spherely.difference(spherely.from_wkt(geog1), spherely.from_wkt(geog2))
assert spherely.equals(result, spherely.from_wkt(expected))


def test_difference_polygons():
def test_difference_polygons() -> None:
result = spherely.difference(poly1, poly2)
expected_near = spherely.area(poly1) - spherely.area(
spherely.from_wkt("POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))")
Expand All @@ -133,14 +133,14 @@ def test_difference_polygons():
),
],
)
def test_symmetric_difference(geog1, geog2, expected):
def test_symmetric_difference(geog1, geog2, expected) -> None:
result = spherely.symmetric_difference(
spherely.from_wkt(geog1), spherely.from_wkt(geog2)
)
assert spherely.equals(result, spherely.from_wkt(expected))


def test_symmetric_difference_polygons():
def test_symmetric_difference_polygons() -> None:
result = spherely.symmetric_difference(poly1, poly2)
expected_near = 2 * (
spherely.area(poly1)
Expand Down
32 changes: 16 additions & 16 deletions tests/test_geoarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ga = pytest.importorskip("geoarrow.pyarrow")


def test_from_geoarrow_wkt():
def test_from_geoarrow_wkt() -> None:

arr = ga.as_wkt(["POINT (1 1)", "POINT(2 2)", "POINT(3 3)"])

Expand All @@ -27,7 +27,7 @@ def test_from_geoarrow_wkt():
assert spherely.equals(result, expected).all()


def test_from_geoarrow_wkb():
def test_from_geoarrow_wkb() -> None:

arr = ga.as_wkt(["POINT (1 1)", "POINT(2 2)", "POINT(3 3)"])
arr_wkb = ga.as_wkb(arr)
Expand All @@ -43,7 +43,7 @@ def test_from_geoarrow_wkb():
assert spherely.equals(result, expected).all()


def test_from_geoarrow_native():
def test_from_geoarrow_native() -> None:

arr = ga.as_wkt(["POINT (1 1)", "POINT(2 2)", "POINT(3 3)"])
arr_point = ga.as_geoarrow(arr)
Expand All @@ -60,7 +60,7 @@ def test_from_geoarrow_native():
)


def test_from_geoarrow_oriented():
def test_from_geoarrow_oriented() -> None:
# by default re-orients the inner ring
arr = ga.as_geoarrow([polygon_with_bad_hole_wkt])

Expand All @@ -75,7 +75,7 @@ def test_from_geoarrow_oriented():
spherely.from_geoarrow(arr, oriented=True)


def test_from_wkt_planar():
def test_from_wkt_planar() -> None:
arr = ga.as_geoarrow(["LINESTRING (-64 45, 0 45)"])
result = spherely.from_geoarrow(arr)
assert spherely.distance(result, spherely.create_point(-30.1, 45)) > 10000
Expand All @@ -87,7 +87,7 @@ def test_from_wkt_planar():
assert spherely.distance(result, spherely.create_point(-30.1, 45)) < 10


def test_from_geoarrow_projection():
def test_from_geoarrow_projection() -> None:
arr = ga.as_wkt(["POINT (1 0)", "POINT(0 1)"])

result = spherely.from_geoarrow(
Expand All @@ -99,26 +99,26 @@ def test_from_geoarrow_projection():
assert (spherely.to_wkt(result) == spherely.to_wkt(expected)).all()


def test_from_geoarrow_no_extension_type():
def test_from_geoarrow_no_extension_type() -> None:
arr = pa.array(["POINT (1 1)", "POINT(2 2)", "POINT(3 3)"])

with pytest.raises(ValueError, match="Expected extension type"):
spherely.from_geoarrow(arr)


def test_from_geoarrow_invalid_encoding():
def test_from_geoarrow_invalid_encoding() -> None:
arr = pa.array(["POINT (1 1)", "POINT(2 2)", "POINT(3 3)"])

with pytest.raises(ValueError, match="'geometry_encoding' should be one"):
spherely.from_geoarrow(arr, geometry_encoding="point")


def test_from_geoarrow_no_arrow_object():
def test_from_geoarrow_no_arrow_object() -> None:
with pytest.raises(ValueError, match="input should be an Arrow-compatible array"):
spherely.from_geoarrow(np.array(["POINT (1 1)"], dtype=object))


def test_to_geoarrow():
def test_to_geoarrow() -> None:
arr = spherely.points([1, 2, 3], [1, 2, 3])
res = spherely.to_geoarrow(
arr, output_schema=ga.point().with_coord_type(ga.CoordType.INTERLEAVED)
Expand All @@ -132,14 +132,14 @@ def test_to_geoarrow():
np.testing.assert_allclose(coords, expected)


def test_to_geoarrow_wkt():
def test_to_geoarrow_wkt() -> None:
arr = spherely.points([1, 2, 3], [1, 2, 3])
result = pa.array(spherely.to_geoarrow(arr, output_schema=ga.wkt()))
expected = pa.array(["POINT (1 1)", "POINT (2 2)", "POINT (3 3)"])
assert result.storage.equals(expected)


def test_to_geoarrow_wkb():
def test_to_geoarrow_wkb() -> None:
arr = spherely.points([1, 2, 3], [1, 2, 3])
result = pa.array(spherely.to_geoarrow(arr, output_schema=ga.wkb()))
# the conversion from lon/lat values to S2 points and back gives some floating
Expand All @@ -155,7 +155,7 @@ def test_to_geoarrow_wkb():
assert result.equals(expected)


def test_wkt_roundtrip():
def test_wkt_roundtrip() -> None:
wkt = [
"POINT (30 10)",
"LINESTRING (30 10, 10 30, 40 40)",
Expand All @@ -173,14 +173,14 @@ def test_wkt_roundtrip():
np.testing.assert_array_equal(result, wkt)


def test_to_geoarrow_no_output_encoding():
def test_to_geoarrow_no_output_encoding() -> None:
arr = spherely.points([1, 2, 3], [1, 2, 3])

with pytest.raises(ValueError, match="'output_schema' should be specified"):
spherely.to_geoarrow(arr)


def test_to_geoarrow_invalid_output_schema():
def test_to_geoarrow_invalid_output_schema() -> None:
arr = spherely.points([1, 2, 3], [1, 2, 3])
with pytest.raises(
ValueError, match="'output_schema' should be an Arrow-compatible schema"
Expand All @@ -191,7 +191,7 @@ def test_to_geoarrow_invalid_output_schema():
spherely.to_geoarrow(arr, output_schema=pa.schema([("test", pa.int64())]))


def test_to_geoarrow_projected():
def test_to_geoarrow_projected() -> None:
arr = spherely.points([1, 2, 3], [1, 2, 3])
point_schema = ga.point().with_coord_type(ga.CoordType.INTERLEAVED)
result = pa.array(
Expand Down
26 changes: 13 additions & 13 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import spherely


def test_from_wkt():
def test_from_wkt() -> None:
result = spherely.from_wkt(["POINT (1 1)", "POINT(2 2)", "POINT(3 3)"])
expected = spherely.points([1, 2, 3], [1, 2, 3])
# object equality does not yet work
Expand All @@ -27,13 +27,13 @@ def test_from_wkt():
assert spherely.equals(result, expected).all()


def test_from_wkt_invalid():
def test_from_wkt_invalid() -> None:
# TODO can we provide better error type?
with pytest.raises(RuntimeError):
spherely.from_wkt(["POINT (1)"])


def test_from_wkt_wrong_type():
def test_from_wkt_wrong_type() -> None:
with pytest.raises(TypeError, match="expected bytes, int found"):
spherely.from_wkt([1])

Expand All @@ -49,7 +49,7 @@ def test_from_wkt_wrong_type():
)


def test_from_wkt_oriented():
def test_from_wkt_oriented() -> None:
# by default re-orients the inner ring
result = spherely.from_wkt(polygon_with_bad_hole_wkt)
assert (
Expand All @@ -62,7 +62,7 @@ def test_from_wkt_oriented():
spherely.from_wkt(polygon_with_bad_hole_wkt, oriented=True)


def test_from_wkt_planar():
def test_from_wkt_planar() -> None:
result = spherely.from_wkt("LINESTRING (-64 45, 0 45)")
assert spherely.distance(result, spherely.create_point(-30.1, 45)) > 10000

Expand All @@ -75,14 +75,14 @@ def test_from_wkt_planar():
assert spherely.distance(result, spherely.create_point(-30.1, 45)) < 10


def test_to_wkt():
def test_to_wkt() -> None:
arr = spherely.points([1.1, 2, 3], [1.1, 2, 3])
result = spherely.to_wkt(arr)
expected = np.array(["POINT (1.1 1.1)", "POINT (2 2)", "POINT (3 3)"], dtype=object)
np.testing.assert_array_equal(result, expected)


def test_to_wkt_precision():
def test_to_wkt_precision() -> None:
arr = spherely.points([0.12345], [0.56789])
result = spherely.to_wkt(arr)
assert result[0] == "POINT (0.12345 0.56789)"
Expand All @@ -101,7 +101,7 @@ def test_to_wkt_precision():
) # noqa: E501


def test_from_wkb_point_empty():
def test_from_wkb_point_empty() -> None:
result = spherely.from_wkb([POINT11_WKB, POINT_NAN_WKB, MULTIPOINT_NAN_WKB])
# empty MultiPoint is converted to empty Point
expected = spherely.from_wkt(["POINT (1 1)", "POINT EMPTY", "POINT EMPTY"])
Expand All @@ -111,7 +111,7 @@ def test_from_wkb_point_empty():
assert str(result) == "GEOMETRYCOLLECTION (POINT EMPTY)"


def test_from_wkb_invalid():
def test_from_wkb_invalid() -> None:
with pytest.raises(RuntimeError, match="Expected endian byte"):
spherely.from_wkb(b"")

Expand All @@ -124,7 +124,7 @@ def test_from_wkb_invalid():
assert str(result) == "POLYGON ((108.7761 -10.2852, 108.7761 -10.2852))"


def test_from_wkb_invalid_type():
def test_from_wkb_invalid_type() -> None:
with pytest.raises(TypeError, match="expected bytes, str found"):
spherely.from_wkb("POINT (1 1)")

Expand Down Expand Up @@ -158,7 +158,7 @@ def test_from_wkb_invalid_type():
),
],
)
def test_wkb_roundtrip(geog):
def test_wkb_roundtrip(geog) -> None:
wkb = spherely.to_wkb(geog)
result = spherely.from_wkb(wkb)
# roundtrip through Geography unit vector is not exact, so equals can fail
Expand All @@ -167,7 +167,7 @@ def test_wkb_roundtrip(geog):
assert str(result) == str(geog)


def test_from_wkb_oriented():
def test_from_wkb_oriented() -> None:
# WKB for POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0)) -> non-CCW box
wkb = bytes.fromhex(
"010300000001000000050000000000000000000000000000000000000000000000000000000000000000002440000000000000244000000000000024400000000000002440000000000000000000000000000000000000000000000000"
Expand All @@ -183,7 +183,7 @@ def test_from_wkb_oriented():
assert not spherely.within(spherely.create_point(5, 5), result)


def test_from_wkb_planar():
def test_from_wkb_planar() -> None:
wkb = spherely.to_wkb(spherely.from_wkt("LINESTRING (-64 45, 0 45)"))

result = spherely.from_wkb(wkb)
Expand Down
Loading

0 comments on commit 71bbbd0

Please sign in to comment.