Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ruff rules #241

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import data_morph

sys.path.insert(0, str(Path().absolute()))
from post_build import determine_versions # noqa: E402
from post_build import determine_versions

project = 'Data Morph'
current_year = dt.date.today().year
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ lint.select = [
"I", # isort
"N", # pep8-naming
"PTH", # flake8-use-pathlib
"RUF", # ruff-specific rules
"SIM", # flake8-simplify
"TRY", # tryceratops
"UP", # pyupgrade
"W", # pycodestyle warning
]
lint.ignore = [
"ANN101", # missing type annotation for self (will be removed in future ruff version)
"ANN102", # missing type annotation for cls in classmethod (will be removed in future ruff version)
"E501", # line-too-long
"TRY003", # avoid specifying long messages outside the exception class (revisit later and consider making custom exceptions)
]
Expand Down
2 changes: 1 addition & 1 deletion src/data_morph/bounds/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __eq__(self, other: BoundingBox) -> bool:
def __repr__(self) -> str:
return '<BoundingBox>\n' f' x={self.x_bounds}' '\n' f' y={self.y_bounds}'

def adjust_bounds(self, x: Number = None, y: Number = None) -> None:
def adjust_bounds(self, x: Number | None = None, y: Number | None = None) -> None:
"""
Adjust bounding box range.

Expand Down
8 changes: 5 additions & 3 deletions src/data_morph/data/dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Class representing a dataset for morphing."""

from __future__ import annotations

from numbers import Number

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -39,13 +41,13 @@ class Dataset:
Utility for creating :class:`Dataset` objects from CSV files.
"""

_REQUIRED_COLUMNS = ['x', 'y']
_REQUIRED_COLUMNS = ('x', 'y')

def __init__(
self,
name: str,
df: pd.DataFrame,
scale: Number = None,
scale: Number | None = None,
) -> None:
self.df: pd.DataFrame = self._validate_data(df).pipe(self._scale_data, scale)
"""pandas.DataFrame: DataFrame containing columns x and y."""
Expand Down Expand Up @@ -181,7 +183,7 @@ def _validate_data(self, data: pd.DataFrame) -> pd.DataFrame:

@plot_with_custom_style
def plot(
self, ax: Axes = None, show_bounds: bool = True, title: str = 'default'
self, ax: Axes | None = None, show_bounds: bool = True, title: str = 'default'
) -> Axes:
"""
Plot the dataset and its bounds.
Expand Down
7 changes: 5 additions & 2 deletions src/data_morph/data/loader.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Load data for morphing."""

from __future__ import annotations

from importlib.resources import files
from itertools import zip_longest
from numbers import Number
from pathlib import Path
from typing import ClassVar

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -41,7 +44,7 @@ class DataLoader:
"""

_DATA_PATH: str = 'data/starter_shapes/'
_DATASETS: dict = {
_DATASETS: ClassVar[dict[str, str]] = {
'bunny': 'bunny.csv',
'cat': 'cat.csv',
'dino': 'dino.csv',
Expand All @@ -66,7 +69,7 @@ def __init__(self) -> None:
def load_dataset(
cls,
dataset: str,
scale: Number = None,
scale: Number | None = None,
) -> Dataset:
"""
Load dataset.
Expand Down
4 changes: 3 additions & 1 deletion src/data_morph/shapes/bases/line_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Base class for shapes that are composed of lines."""

from __future__ import annotations

from collections.abc import Iterable
from numbers import Number

Expand Down Expand Up @@ -97,7 +99,7 @@ def distance(self, x: Number, y: Number) -> float:
)

@plot_with_custom_style
def plot(self, ax: Axes = None) -> Axes:
def plot(self, ax: Axes | None = None) -> Axes:
"""
Plot the shape.

Expand Down
4 changes: 3 additions & 1 deletion src/data_morph/shapes/bases/point_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Base class for shapes that are composed of points."""

from __future__ import annotations

from collections.abc import Iterable
from numbers import Number

Expand Down Expand Up @@ -52,7 +54,7 @@ def distance(self, x: Number, y: Number) -> float:
)

@plot_with_custom_style
def plot(self, ax: Axes = None) -> Axes:
def plot(self, ax: Axes | None = None) -> Axes:
"""
Plot the shape.

Expand Down
2 changes: 1 addition & 1 deletion src/data_morph/shapes/bases/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _recursive_repr(self, attr: str | None = None) -> str:
)

@abstractmethod
def plot(self, ax: Axes = None) -> Axes:
def plot(self, ax: Axes | None = None) -> Axes:
"""
Plot the shape.

Expand Down
8 changes: 5 additions & 3 deletions src/data_morph/shapes/circles.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Shapes that are circular in nature."""

from __future__ import annotations

from numbers import Number

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -33,7 +35,7 @@ class Circle(Shape):
The radius of the circle.
"""

def __init__(self, dataset: Dataset, radius: Number = None) -> None:
def __init__(self, dataset: Dataset, radius: Number | None = None) -> None:
self.center: np.ndarray = dataset.df[['x', 'y']].mean().to_numpy()
"""numpy.ndarray: The (x, y) coordinates of the circle's center."""

Expand Down Expand Up @@ -63,7 +65,7 @@ def distance(self, x: Number, y: Number) -> float:
)

@plot_with_custom_style
def plot(self, ax: Axes = None) -> Axes:
def plot(self, ax: Axes | None = None) -> Axes:
"""
Plot the shape.

Expand Down Expand Up @@ -159,7 +161,7 @@ def distance(self, x: Number, y: Number) -> float:
)

@plot_with_custom_style
def plot(self, ax: Axes = None) -> Axes:
def plot(self, ax: Axes | None = None) -> Axes:
"""
Plot the shape.

Expand Down
3 changes: 2 additions & 1 deletion src/data_morph/shapes/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from itertools import zip_longest
from numbers import Number
from typing import ClassVar

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -56,7 +57,7 @@ class ShapeFactory:
The starting dataset to morph into other shapes.
"""

_SHAPE_MAPPING: dict = {
_SHAPE_MAPPING: ClassVar[dict[str, type[Shape]]] = {
'bullseye': Bullseye,
'circle': Circle,
'high_lines': HighLines,
Expand Down
2 changes: 1 addition & 1 deletion tests/data/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_validate_data_fix_column_casing(self, starter_shapes_dir):

df = pd.read_csv(starter_shapes_dir / 'dino.csv').rename(columns={'x': 'X'})
dataset = Dataset('dino', df)
assert not dataset.df[dataset._REQUIRED_COLUMNS].empty
assert not dataset.df[list(dataset._REQUIRED_COLUMNS)].empty

@pytest.mark.bounds
@pytest.mark.parametrize(
Expand Down
9 changes: 4 additions & 5 deletions tests/shapes/test_circles.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test circles module."""

import re
from collections.abc import Iterable
from numbers import Number

import numpy as np
Expand All @@ -16,7 +15,7 @@ class CirclesModuleTestBase:
"""Base for testing circle shapes."""

shape_name: str
distance_test_cases: Iterable[tuple[Iterable[Number], float]]
distance_test_cases: tuple[tuple[tuple[Number], float]]
repr_regex: str

@pytest.fixture(scope='class')
Expand All @@ -40,7 +39,7 @@ class TestBullseye(CirclesModuleTestBase):
"""Test the Bullseye class."""

shape_name = 'bullseye'
distance_test_cases = [[(20, 50), 3.660254], [(10, 25), 9.08004]]
distance_test_cases = (((20, 50), 3.660254), ((10, 25), 9.08004))
repr_regex = (
r'^<Bullseye>\n'
r' circles=\n'
Expand All @@ -61,7 +60,7 @@ class TestCircle(CirclesModuleTestBase):
"""Test the Circle class."""

shape_name = 'circle'
distance_test_cases = [[(20, 50), 10.490381], [(10, 25), 15.910168]]
distance_test_cases = (((20, 50), 10.490381), ((10, 25), 15.910168))
repr_regex = '^' + CIRCLE_REPR + '$'

def test_is_circle(self, shape):
Expand All @@ -79,7 +78,7 @@ class TestRings(CirclesModuleTestBase):
"""Test the Rings class."""

shape_name = 'rings'
distance_test_cases = [[(20, 50), 3.16987], [(10, 25), 9.08004]]
distance_test_cases = (((20, 50), 3.16987), ((10, 25), 9.08004))
repr_regex = (
r'^<Rings>\n'
r' circles=\n'
Expand Down
31 changes: 15 additions & 16 deletions tests/shapes/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

from collections.abc import Iterable
from numbers import Number

import numpy as np
Expand All @@ -15,9 +14,9 @@ class LinesModuleTestBase:
"""Base for testing line-based shapes."""

shape_name: str
distance_test_cases: Iterable[tuple[Iterable[Number], float]]
distance_test_cases: tuple[tuple[tuple[Number], float]]
expected_line_count: int
expected_slopes: Iterable[Number] | Number
expected_slopes: tuple[Number] | Number

@pytest.fixture(scope='class')
def shape(self, shape_factory):
Expand Down Expand Up @@ -67,7 +66,7 @@ class TestHighLines(ParallelLinesModuleTestBase):
"""Test the HighLines class."""

shape_name = 'high_lines'
distance_test_cases = [[(20, 50), 6.0], [(30, 60), 4.0]]
distance_test_cases = (((20, 50), 6.0), ((30, 60), 4.0))
expected_line_count = 2
expected_slopes = 0

Expand All @@ -76,7 +75,7 @@ class TestHorizontalLines(ParallelLinesModuleTestBase):
"""Test the HorizontalLines class."""

shape_name = 'h_lines'
distance_test_cases = [[(20, 50), 0.0], [(30, 60), 2.5]]
distance_test_cases = (((20, 50), 0.0), ((30, 60), 2.5))
expected_line_count = 5
expected_slopes = 0

Expand All @@ -85,7 +84,7 @@ class TestSlantDownLines(ParallelLinesModuleTestBase):
"""Test the SlantDownLines class."""

shape_name = 'slant_down'
distance_test_cases = [[(20, 50), 1.664101], [(30, 60), 0.554700]]
distance_test_cases = (((20, 50), 1.664101), ((30, 60), 0.554700))
expected_line_count = 5
expected_slopes = -1.5

Expand All @@ -94,7 +93,7 @@ class TestSlantUpLines(ParallelLinesModuleTestBase):
"""Test the SlantUpLines class."""

shape_name = 'slant_up'
distance_test_cases = [[(20, 50), 1.664101], [(30, 60), 1.109400]]
distance_test_cases = (((20, 50), 1.664101), ((30, 60), 1.109400))
expected_line_count = 5
expected_slopes = 1.5

Expand All @@ -103,7 +102,7 @@ class TestVerticalLines(ParallelLinesModuleTestBase):
"""Test the VerticalLines class."""

shape_name = 'v_lines'
distance_test_cases = [[(35, 60), 5.0], [(30, 60), 0.0]]
distance_test_cases = (((35, 60), 5.0), ((30, 60), 0.0))
expected_line_count = 5
expected_slopes = np.inf

Expand All @@ -112,7 +111,7 @@ class TestWideLines(ParallelLinesModuleTestBase):
"""Test the WideLines class."""

shape_name = 'wide_lines'
distance_test_cases = [[(26, 50), 0], [(30, 60), 4.0]]
distance_test_cases = (((26, 50), 0), ((30, 60), 4.0))
expected_line_count = 2
expected_slopes = np.inf

Expand All @@ -121,14 +120,14 @@ class TestXLines(LinesModuleTestBase):
"""Test the XLines class."""

shape_name = 'x'
distance_test_cases = [
[(8, 83), 0], # edge of X line
[(20, 65), 0], # middle of X (intersection point)
[(19, 64), 0.277350], # off the X
[(10, 20), 27.073973], # off the X
]
distance_test_cases = (
((8, 83), 0), # edge of X line
((20, 65), 0), # middle of X (intersection point)
((19, 64), 0.277350), # off the X
((10, 20), 27.073973), # off the X
)
expected_line_count = 2
expected_slopes = [-1.5, 1.5]
expected_slopes = (-1.5, 1.5)

def test_lines_form_an_x(self, shape):
"""Test that the lines form an X."""
Expand Down
Loading