Skip to content

Commit

Permalink
Enable more ruff rules (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro authored Aug 15, 2024
1 parent 8e25e76 commit 469c432
Show file tree
Hide file tree
Showing 27 changed files with 140 additions and 107 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ repos:
# - id: codespell
# additional_dependencies:
# - tomli
# - repo: https://github.com/pycqa/isort
# rev: 5.13.2
# hooks:
# - id: isort
# name: isort (python)

ci:
autofix_prs: false
45 changes: 32 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ fix = true
line-length = 100

[tool.ruff.lint]
select = [
# "B",
"E",
"F",
"FLY",
"I",
"ICN",
"NPY",
"PIE",
"PLC",
"PLE",
"PLR",
"PLW",
"RUF",
# "UP",
"W",
]
ignore = [
"E402", # Module level import not at top of file
"E501", # Line too long
Expand All @@ -87,22 +104,24 @@ ignore = [
"E731", # Do not assign a lambda expression, use a def
"E741", # Ambiguous variable name
"F405", # From star imports
# "PLE0604", # Invalid object in `__all__`, must contain only strings
# "PLE0605", # Invalid format for `__all__`
# "PLR091", # Too many arguments/branches/statements
# "PLR2004", # Magic value used in comparison
# "PLW2901", # `for` loop variable is overwritten
# "RUF005", # Consider {expr} instead of concatenation
# "RUF012", # Mutable class attributes should use `typing.ClassVar`
"PLE0604", # Invalid object in `__all__`, must contain only strings
"PLE0605", # Invalid format for `__all__`
"PLR091", # Too many arguments/branches/statements
"PLR2004", # Magic value used in comparison
"PLW2901", # `for` loop variable is overwritten
"RUF012", # Mutable class attributes should use `typing.ClassVar`
]
extend-unsafe-fixes = [
"F401", # Unused imports
"F841", # Unused variables
]

[tool.isort]
force_grid_wrap = 4
multi_line_output = 5
combine_as_imports = true
lines_between_types = 1
include_trailing_comma = true
[tool.ruff.lint.per-file-ignores]
"spatialpandas/tests/**" = [
"NPY002", # Replace legacy `np.random.rand` call with Generator
# "B904", # Within an `except` clause, raise exceptions with from err or None
]

[tool.ruff.lint.isort]
known-first-party = ["spatialpandas"]
combine-as-imports = true
14 changes: 7 additions & 7 deletions spatialpandas/dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
import uuid
from inspect import signature

import dask
import dask.dataframe as dd
import numpy as np
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
from retrying import retry
from packaging.version import Version

import dask
import dask.dataframe as dd
from dask import delayed
from dask.dataframe.core import get_parallel_type
from dask.dataframe.partitionquantiles import partition_quantiles
from dask.dataframe.extensions import make_array_nonempty
from dask.dataframe.partitionquantiles import partition_quantiles
from packaging.version import Version
from retrying import retry

try:
from dask.dataframe.dispatch import make_meta_dispatch
from dask.dataframe.backends import meta_nonempty
from dask.dataframe.dispatch import make_meta_dispatch
except ImportError:
from dask.dataframe.utils import make_meta as make_meta_dispatch, meta_nonempty

Expand Down
11 changes: 9 additions & 2 deletions spatialpandas/geometry/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,15 @@ def is_geometry_array(data):


def to_geometry_array(data, dtype=None):
from . import (LineArray, MultiLineArray, MultiPointArray,
MultiPolygonArray, PointArray, PolygonArray, RingArray)
from . import (
LineArray,
MultiLineArray,
MultiPointArray,
MultiPolygonArray,
PointArray,
PolygonArray,
RingArray,
)
if sg is not None:
shapely_to_spatialpandas = {
sg.Point: PointArray,
Expand Down
7 changes: 5 additions & 2 deletions spatialpandas/geometry/basefixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
from ..geometry.base import Geometry, GeometryArray, GeometryDtype
from ..geometry.baselist import _lexographic_lt
from ..utils import _asarray_maybe_ragged
from ._algorithms.bounds import (bounds_interleaved, total_bounds_interleaved,
total_bounds_interleaved_1d)
from ._algorithms.bounds import (
bounds_interleaved,
total_bounds_interleaved,
total_bounds_interleaved_1d,
)


@total_ordering
Expand Down
10 changes: 6 additions & 4 deletions spatialpandas/geometry/baselist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
from numba import jit, prange

from ..geometry.base import Geometry, GeometryArray
from ._algorithms.bounds import (bounds_interleaved, total_bounds_interleaved,
total_bounds_interleaved_1d)
from ._algorithms.bounds import (
bounds_interleaved,
total_bounds_interleaved,
total_bounds_interleaved_1d,
)


def _validate_nested_arrow_type(nesting_levels, pyarrow_type):
Expand All @@ -21,7 +24,6 @@ def _validate_nested_arrow_type(nesting_levels, pyarrow_type):
nesting_levels)
)
pyarrow_element_type = pyarrow_element_type.value_type
pyarrow_element_type = pyarrow_element_type
numpy_element_dtype = pyarrow_element_type.to_pandas_dtype()
if (numpy_element_dtype() is None
or numpy_element_dtype().dtype.kind not in ('i', 'u', 'f')):
Expand Down Expand Up @@ -69,7 +71,7 @@ def buffer_offsets(self):
for i in range(3, len(buffers) - 1, 2)
)

return (offsets1,) + remaining_offsets
return (offsets1, *remaining_offsets)

@property
def flat_values(self):
Expand Down
3 changes: 1 addition & 2 deletions spatialpandas/geometry/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from ..geometry._algorithms.intersection import lines_intersect_bounds
from ..geometry._algorithms.measures import compute_line_length
from ..geometry.base import GeometryDtype
from ..geometry.baselist import (GeometryList, GeometryListArray,
_geometry_map_nested1)
from ..geometry.baselist import GeometryList, GeometryListArray, _geometry_map_nested1


@register_extension_dtype
Expand Down
6 changes: 2 additions & 4 deletions spatialpandas/geometry/multiline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
from dask.dataframe.extensions import make_array_nonempty
from pandas.core.dtypes.dtypes import register_extension_dtype

from ..geometry._algorithms.intersection import (lines_intersect_bounds,
multilines_intersect_bounds)
from ..geometry._algorithms.intersection import lines_intersect_bounds, multilines_intersect_bounds
from ..geometry._algorithms.measures import compute_line_length
from ..geometry.base import GeometryDtype
from ..geometry.baselist import (GeometryList, GeometryListArray,
_geometry_map_nested2)
from ..geometry.baselist import GeometryList, GeometryListArray, _geometry_map_nested2


@register_extension_dtype
Expand Down
3 changes: 2 additions & 1 deletion spatialpandas/geometry/multipolygon.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings

import numpy as np
import pyarrow as pa
import warnings
from dask.dataframe.extensions import make_array_nonempty
from pandas.core.dtypes.dtypes import register_extension_dtype

Expand Down
3 changes: 1 addition & 2 deletions spatialpandas/geometry/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from dask.dataframe.extensions import make_array_nonempty
from pandas.core.dtypes.dtypes import register_extension_dtype

from ..geometry._algorithms.intersection import (point_intersects_polygon,
segment_intersects_point)
from ..geometry._algorithms.intersection import point_intersects_polygon, segment_intersects_point
from ..geometry.base import GeometryDtype
from ..geometry.basefixed import GeometryFixed, GeometryFixedArray
from ..utils import ngpjit
Expand Down
2 changes: 1 addition & 1 deletion spatialpandas/geoseries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pandas as pd

from .geometry import GeometryDtype, Geometry
from .geometry import Geometry, GeometryDtype


class _MaybeGeoSeries(pd.Series):
Expand Down
20 changes: 11 additions & 9 deletions spatialpandas/io/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
from functools import reduce
from glob import has_magic
from numbers import Number
from packaging.version import Version
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union

import fsspec
import pandas as pd
from dask import delayed
from dask.dataframe import from_delayed, from_pandas
from dask.dataframe import read_parquet as dd_read_parquet
from dask.dataframe import to_parquet as dd_to_parquet # noqa
from dask.dataframe import (
from_delayed,
from_pandas,
read_parquet as dd_read_parquet,
to_parquet as dd_to_parquet,
)
from dask.utils import natural_sort_key
from packaging.version import Version
from pandas.io.parquet import to_parquet as pd_to_parquet
from pyarrow.parquet import ParquetDataset, ParquetFile, read_metadata

Expand Down Expand Up @@ -90,10 +93,9 @@ def to_parquet(

if PANDAS_GE_12:
to_parquet_args.update({"storage_options": storage_options})
else:
if filesystem is None:
filesystem = validate_coerce_filesystem(path, filesystem, storage_options)
to_parquet_args.update({"filesystem": filesystem})
elif filesystem is None:
filesystem = validate_coerce_filesystem(path, filesystem, storage_options)
to_parquet_args.update({"filesystem": filesystem})

pd_to_parquet(**to_parquet_args)

Expand Down Expand Up @@ -441,7 +443,7 @@ def _perform_read_parquet_dask(
div_maxes = partitions_df.div_maxes

if load_divisions:
divisions = div_mins + [div_maxes[-1]]
divisions = [*div_mins, div_maxes[-1]]
if divisions != sorted(divisions):
raise ValueError(
"Cannot load divisions because the discovered divisions are unsorted.\n"
Expand Down
1 change: 0 additions & 1 deletion spatialpandas/spatialindex/hilbert_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from ..utils import ngjit



@ngjit
def _int_2_binary(v, width):
"""Return a binary byte array representation of `v` zero padded to `width`
Expand Down
7 changes: 4 additions & 3 deletions spatialpandas/tests/_create_testdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
# The files can be created with particular version of pyarrow and dask, and
# checked that they can be read with different versions of them.

import os
import warnings

import dask.dataframe as dd
from hypothesis.errors import NonInteractiveExampleWarning
import os
from pyarrow import __version__ as pyarrow_version

from spatialpandas import GeoDataFrame
from spatialpandas.io import to_parquet
from spatialpandas.tests.geometry.strategies import st_multiline_array
import warnings


directory = "test_data"

Expand Down
27 changes: 14 additions & 13 deletions spatialpandas/tests/geometry/algorithms/test_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@
from hypothesis import example, given
from shapely import geometry as sg

from ..strategies import (
coord,
hyp_settings,
st_bounds,
st_line_array,
st_multiline_array,
st_multipoint_array,
st_multipolygon_array,
st_point_array,
st_points,
st_polygon,
st_polygon_array,
)
from spatialpandas.geometry import (
LineArray,
MultiLineArray,
Expand All @@ -31,6 +18,20 @@
segments_intersect,
)

from ..strategies import (
coord,
hyp_settings,
st_bounds,
st_line_array,
st_multiline_array,
st_multipoint_array,
st_multipolygon_array,
st_point_array,
st_points,
st_polygon,
st_polygon_array,
)


@given(coord, coord, coord, coord, coord, coord)
@example(0, 0, 1, 1, 0.25, 0.25) # Point on line
Expand Down
3 changes: 2 additions & 1 deletion spatialpandas/tests/geometry/algorithms/test_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import pytest
from hypothesis import given

from ..strategies import hyp_settings, st_multipolygon_array, st_polygon_array
from spatialpandas.geometry import MultiPolygonArray, PolygonArray

from ..strategies import hyp_settings, st_multipolygon_array, st_polygon_array


@pytest.mark.slow
@given(st_polygon_array())
Expand Down
3 changes: 2 additions & 1 deletion spatialpandas/tests/geometry/algorithms/test_orientation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from hypothesis import given
from shapely import geometry as sg

from ..strategies import coord, hyp_settings
from spatialpandas.geometry._algorithms.orientation import triangle_orientation

from ..strategies import coord, hyp_settings


@given(coord, coord, coord, coord, coord, coord)
@hyp_settings
Expand Down
Loading

0 comments on commit 469c432

Please sign in to comment.