Skip to content

Commit c853c65

Browse files
committed
[CI] Add pre-commit hook pyupgrade to auto upgrade Python syntax
"A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language." https://github.com/asottile/pyupgrade
1 parent c6d7969 commit c853c65

13 files changed

+48
-56
lines changed

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ repos:
1010
hooks:
1111
- id: identity
1212
- id: check-hooks-apply
13+
- repo: https://github.com/asottile/pyupgrade
14+
rev: v3.18.0
15+
hooks:
16+
- id: pyupgrade
17+
args: [--py36-plus]
18+
exclude: ^python/sedona/sql/dataframe_api\.py$
1319
- repo: https://github.com/psf/black-pre-commit-mirror
1420
rev: 24.10.0
1521
hooks:

python/sedona/core/jvm/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from sedona.utils.decorators import classproperty
3030

31-
string_types = (type(b""), type(""))
31+
string_types = (bytes, str)
3232

3333

3434
def is_greater_or_equal_version(version_a: str, version_b: str) -> bool:
@@ -190,7 +190,7 @@ def get_spark_java_config(
190190
try:
191191
used_jar_files = java_spark_conf.get(value)
192192
except Py4JJavaError:
193-
error_message = "Didn't find the value of {} from SparkConf".format(value)
193+
error_message = f"Didn't find the value of {value} from SparkConf"
194194
logging.info(error_message)
195195

196196
return used_jar_files, error_message

python/sedona/maps/SedonaPyDeck.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def _create_default_fill_color_(cls, gdf, plot_col):
315315
:return: fill_color string for pydeck map
316316
"""
317317
plot_max = gdf[plot_col].max()
318-
return "[85, 183, 177, ({0} / {1}) * 255 + 15]".format(plot_col, plot_max)
318+
return f"[85, 183, 177, ({plot_col} / {plot_max}) * 255 + 15]"
319319

320320
@classmethod
321321
def _create_coord_column_(cls, gdf, geometry_col, add_points=False):

python/sedona/raster/meta.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def _do_change_pixel_anchor(self, from_anchor: PixelAnchor, to_anchor: PixelAnch
104104

105105
def __repr__(self):
106106
return (
107-
"[ {} {} {}\n".format(self.scale_x, self.skew_x, self.ip_x)
108-
+ " {} {} {}\n".format(self.skew_y, self.scale_y, self.ip_y)
107+
f"[ {self.scale_x} {self.skew_x} {self.ip_x}\n"
108+
+ f" {self.skew_y} {self.scale_y} {self.ip_y}\n"
109109
+ " 0 0 1 ]"
110110
)
111111

python/sedona/raster/raster_serde.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _deserialize(bio: BytesIO, raster_type: int) -> SedonaRaster:
6363
width, height, bands_meta, affine_trans, crs_wkt, awt_raster
6464
)
6565
else:
66-
raise ValueError("unsupported raster_type: {}".format(raster_type))
66+
raise ValueError(f"unsupported raster_type: {raster_type}")
6767

6868

6969
def _read_grid_envelope(bio: BytesIO) -> Tuple[int, int, int, int]:
@@ -183,7 +183,7 @@ def _read_data_buffer(bio: BytesIO) -> DataBuffer:
183183
elif data_type == DataBuffer.TYPE_DOUBLE:
184184
np_array = np.frombuffer(bio.read(8 * bank_size), dtype=np.float64)
185185
else:
186-
raise ValueError("unknown data_type {}".format(data_type))
186+
raise ValueError(f"unknown data_type {data_type}")
187187

188188
banks.append(np_array)
189189

python/sedona/utils/decorators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
T = TypeVar("T")
2121

2222

23-
class classproperty(object):
23+
class classproperty:
2424

2525
def __init__(self, f):
2626
self.f = f

python/sedona/utils/geometry_serde.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ def find_geos_c_dll():
4242
".dll"
4343
):
4444
return os.path.join(lib_dirpath, filename)
45-
raise RuntimeError(
46-
"geos_c DLL not found in {}\\[S|s]hapely.libs".format(packages_dir)
47-
)
45+
raise RuntimeError(f"geos_c DLL not found in {packages_dir}\\[S|s]hapely.libs")
4846

4947
if shapely.__version__.startswith("2."):
5048
if sys.platform != "win32":

python/sedona/utils/geometry_serde_general.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def type_of(geom) -> int:
8282
elif geom._ndim == 3:
8383
return CoordinateType.XYZ
8484
else:
85-
raise ValueError("Invalid coordinate dimension: {}".format(geom._ndim))
85+
raise ValueError(f"Invalid coordinate dimension: {geom._ndim}")
8686

8787
@staticmethod
8888
def bytes_per_coord(coord_type: int) -> int:
@@ -233,7 +233,7 @@ def deserialize(buffer: bytes) -> Optional[BaseGeometry]:
233233
elif geom_type == GeometryTypeID.GEOMETRYCOLLECTION:
234234
geom = deserialize_geometry_collection(geom_buffer)
235235
else:
236-
raise ValueError("Unsupported geometry type ID: {}".format(geom_type))
236+
raise ValueError(f"Unsupported geometry type ID: {geom_type}")
237237
return geom, geom_buffer.ints_offset
238238

239239

@@ -546,7 +546,7 @@ def serialize_shapely_1_empty_geom(geom: BaseGeometry) -> bytearray:
546546
geom_type = GeometryTypeID.MULTIPOLYGON
547547
total_size = 12
548548
else:
549-
raise ValueError("Invalid empty geometry collection object: {}".format(geom))
549+
raise ValueError(f"Invalid empty geometry collection object: {geom}")
550550
return create_buffer_for_geom(geom_type, CoordinateType.XY, total_size, 0)
551551

552552

python/sedona/utils/meta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def register(self, meth):
7979

8080
if parm.annotation is inspect.Parameter.empty:
8181
raise InvalidParametersException(
82-
"Argument {} must be annotated with a type".format(name)
82+
f"Argument {name} must be annotated with a type"
8383
)
8484
if parm.default is not inspect.Parameter.empty:
8585
self._methods[tuple(types)] = meth

python/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from sedona import version
2323

24-
with open("README.md", "r") as fh:
24+
with open("README.md") as fh:
2525
long_description = fh.read()
2626

2727
extension_args = {}

python/tests/core/test_avoiding_python_jvm_serde_df.py

+14-18
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,20 @@ def test_spatial_join_query_flat_to_df(self):
165165
right_geometries = self.__row_to_list(right_geometries_raw)
166166

167167
# Ignore the ordering of these
168-
assert set(geom[0] for geom in left_geometries) == set(
169-
[
170-
"POLYGON ((0 4, -3 3, -8 6, -6 8, -2 9, 0 4))",
171-
"POLYGON ((10 3, 10 6, 14 6, 14 3, 10 3))",
172-
"POLYGON ((2 2, 2 4, 3 5, 7 5, 9 3, 8 1, 4 1, 2 2))",
173-
"POLYGON ((-1 -1, -1 -3, -2 -5, -6 -8, -5 -2, -3 -2, -1 -1))",
174-
"POLYGON ((-1 -1, -1 -3, -2 -5, -6 -8, -5 -2, -3 -2, -1 -1))",
175-
]
176-
)
177-
assert set(geom[0] for geom in right_geometries) == set(
178-
[
179-
"POINT (-3 5)",
180-
"POINT (11 5)",
181-
"POINT (4 3)",
182-
"POINT (-1 -1)",
183-
"POINT (-4 -5)",
184-
]
185-
)
168+
assert {geom[0] for geom in left_geometries} == {
169+
"POLYGON ((0 4, -3 3, -8 6, -6 8, -2 9, 0 4))",
170+
"POLYGON ((10 3, 10 6, 14 6, 14 3, 10 3))",
171+
"POLYGON ((2 2, 2 4, 3 5, 7 5, 9 3, 8 1, 4 1, 2 2))",
172+
"POLYGON ((-1 -1, -1 -3, -2 -5, -6 -8, -5 -2, -3 -2, -1 -1))",
173+
"POLYGON ((-1 -1, -1 -3, -2 -5, -6 -8, -5 -2, -3 -2, -1 -1))",
174+
}
175+
assert {geom[0] for geom in right_geometries} == {
176+
"POINT (-3 5)",
177+
"POINT (11 5)",
178+
"POINT (4 3)",
179+
"POINT (-1 -1)",
180+
"POINT (-4 -5)",
181+
}
186182

187183
def test_range_query_flat_to_df(self):
188184
poi_point_rdd = WktReader.readToGeometryRDD(

python/tests/sql/test_function.py

+13-21
Original file line numberDiff line numberDiff line change
@@ -1911,14 +1911,10 @@ def test_st_collect_on_array_type(self):
19111911
)
19121912

19131913
# then result should be as expected
1914-
assert set(
1915-
[
1916-
el[0]
1917-
for el in geometry_df_collected.selectExpr(
1918-
"ST_AsText(collected)"
1919-
).collect()
1920-
]
1921-
) == {
1914+
assert {
1915+
el[0]
1916+
for el in geometry_df_collected.selectExpr("ST_AsText(collected)").collect()
1917+
} == {
19221918
"MULTILINESTRING ((1 2, 3 4), (3 4, 4 5))",
19231919
"MULTIPOINT ((1 2), (-2 3))",
19241920
"MULTIPOLYGON (((1 2, 1 4, 3 4, 3 2, 1 2)), ((0.5 0.5, 5 0, 5 5, 0 5, 0.5 0.5)))",
@@ -1944,14 +1940,10 @@ def test_st_collect_on_multiple_columns(self):
19441940
)
19451941

19461942
# then result should be calculated
1947-
assert set(
1948-
[
1949-
el[0]
1950-
for el in geometry_df_collected.selectExpr(
1951-
"ST_AsText(collected)"
1952-
).collect()
1953-
]
1954-
) == {
1943+
assert {
1944+
el[0]
1945+
for el in geometry_df_collected.selectExpr("ST_AsText(collected)").collect()
1946+
} == {
19551947
"MULTILINESTRING ((1 2, 3 4), (3 4, 4 5))",
19561948
"MULTIPOINT ((1 2), (-2 3))",
19571949
"MULTIPOLYGON (((1 2, 1 4, 3 4, 3 2, 1 2)), ((0.5 0.5, 5 0, 5 5, 0 5, 0.5 0.5)))",
@@ -1980,7 +1972,7 @@ def test_st_reverse(self):
19801972
}
19811973
for input_geom, expected_geom in test_cases.items():
19821974
reversed_geometry = self.spark.sql(
1983-
"select ST_AsText(ST_Reverse(ST_GeomFromText({})))".format(input_geom)
1975+
f"select ST_AsText(ST_Reverse(ST_GeomFromText({input_geom})))"
19841976
)
19851977
assert reversed_geometry.take(1)[0][0] == expected_geom
19861978

@@ -2078,7 +2070,7 @@ def test_st_force2d(self):
20782070

20792071
for input_geom, expected_geom in tests1.items():
20802072
geom_2d = self.spark.sql(
2081-
"select ST_AsText(ST_Force_2D(ST_GeomFromText({})))".format(input_geom)
2073+
f"select ST_AsText(ST_Force_2D(ST_GeomFromText({input_geom})))"
20822074
)
20832075
assert geom_2d.take(1)[0][0] == expected_geom
20842076

@@ -2102,7 +2094,7 @@ def test_st_buildarea(self):
21022094

21032095
for input_geom, expected_geom in tests.items():
21042096
areal_geom = self.spark.sql(
2105-
"select ST_AsText(ST_BuildArea(ST_GeomFromText({})))".format(input_geom)
2097+
f"select ST_AsText(ST_BuildArea(ST_GeomFromText({input_geom})))"
21062098
)
21072099
assert areal_geom.take(1)[0][0] == expected_geom
21082100

@@ -2162,7 +2154,7 @@ def test_st_s2_cell_ids(self):
21622154
]
21632155
for input_geom in test_cases:
21642156
cell_ids = self.spark.sql(
2165-
"select ST_S2CellIDs(ST_GeomFromText({}), 6)".format(input_geom)
2157+
f"select ST_S2CellIDs(ST_GeomFromText({input_geom}), 6)"
21662158
).take(1)[0][0]
21672159
assert isinstance(cell_ids, list)
21682160
assert isinstance(cell_ids[0], int)
@@ -2190,7 +2182,7 @@ def test_st_h3_cell_ids(self):
21902182
]
21912183
for input_geom in test_cases:
21922184
cell_ids = self.spark.sql(
2193-
"select ST_H3CellIDs(ST_GeomFromText({}), 6, true)".format(input_geom)
2185+
f"select ST_H3CellIDs(ST_GeomFromText({input_geom}), 6, true)"
21942186
).take(1)[0][0]
21952187
assert isinstance(cell_ids, list)
21962188
assert isinstance(cell_ids[0], int)

python/tests/stats/test_dbscan.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def get_actual_results(
9292

9393
clusters = {
9494
frozenset([y[0] for y in clusters_members if y[1] == x])
95-
for x in set([y[1] for y in clusters_members])
95+
for x in {y[1] for y in clusters_members}
9696
}
9797

9898
return clusters

0 commit comments

Comments
 (0)