Skip to content

Commit

Permalink
Switch linting to ruff (#203)
Browse files Browse the repository at this point in the history
* use ruff instead of flake8 and others

* fine tune ruff to be more similar to what we used so far

* enable E721

* ruff (pydocstyle) fixes for previously ignored rules

* Fixed typo

* update changelog and credits

* fixes for B904

* apply B028

* change variable name from e to exception

* update pre-commit ruff args so that it exits with 0 on successful fix

* Updated doc/development.rst

* Fixed ruff order for manual runs

---------

Co-authored-by: janbridley <[email protected]>
  • Loading branch information
DomFijan and janbridley authored Oct 11, 2023
1 parent 8cc9183 commit 6d72539
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 109 deletions.
42 changes: 5 additions & 37 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,20 @@ repos:
- id: check-executables-have-shebangs
- id: check-json
- id: check-yaml
- repo: https://github.com/asottile/pyupgrade
rev: 'v3.8.0'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.291
hooks:
- id: pyupgrade
args:
- --py38-plus
exclude: '(?:extern/.*)'
- repo: https://github.com/PyCQA/isort
rev: '5.12.0'
hooks:
- id: isort
exclude: '(?:extern/.*)'
- id: ruff
args: [ --fix ]
types_or: [python, pyi, jupyter]
- repo: https://github.com/psf/black
rev: '23.3.0'
hooks:
- id: black
exclude: '(?:extern/.*)'
- repo: https://github.com/PyCQA/pydocstyle
rev: '6.3.0'
hooks:
- id: pydocstyle
exclude: '(?:extern/.*|tests/.*|setup.py|conf.py)'
additional_dependencies: [
'tomli'
]
- repo: https://github.com/PyCQA/flake8
rev: '6.0.0'
hooks:
- id: flake8
additional_dependencies: [
'flake8-bugbear==22.1.11',
'flake8-rst-docstrings==0.2.5',
'pep8==1.7.1',
'pep8-naming==0.12.1',
'flake8-comprehensions==3.8.0',
]
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.7.0
hooks:
- id: nbqa-pyupgrade
args:
- --nbqa-mutate
- --py36-plus
- id: nbqa-isort
args:
- --nbqa-mutate
- id: nbqa-black
args:
- --nbqa-mutate
1 change: 1 addition & 0 deletions ChangeLog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Added
Changed
~~~~~~~

- Pre-commit now uses ruff instead of flake8, pydocstyle, pyupgrade and isort.
- CI now uses GitHub Actions.
- Docs ported to furo theme.

Expand Down
1 change: 1 addition & 0 deletions Credits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Domagoj Fijan

* Rewrote point in polygon check to use NumPy vectorized operations.
* Rewrote point in polyhedron check to use NumPy vectorized operations.
* Pre-commit now uses ruff instead of flake8, pydocstyle, pyupgrade and isort.
* Ported CI to github actions.
* Ported docs to Furo.

Expand Down
6 changes: 4 additions & 2 deletions coxeter/families/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def get_shape(cls, n):
n (int):
The number of vertices (greater than or equal to 3).
Returns:
Returns
-------
:class:`~.ConvexPolygon`: The corresponding regular polygon.
"""
return ConvexPolygon(cls.make_vertices(n))
Expand All @@ -48,7 +49,8 @@ def make_vertices(cls, n):
n (int):
An integer greater than or equal to 3.
Returns:
Returns
-------
:math:`(n, 3)` :class:`numpy.ndarray` of float: The vertices of the polygon.
"""
if n < 3:
Expand Down
21 changes: 14 additions & 7 deletions coxeter/families/plane_shape_families.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class TruncationPlaneShapeFamily(ShapeFamily):
def get_planes(cls):
"""Get the set of planes used to truncate the shape.
Returns:
Returns
-------
(:math:`N_{planes}`, 3) :class:`numpy.ndarray` of float:
The planes defining this family
"""
Expand All @@ -58,7 +59,8 @@ def get_plane_types(cls):
* type 1 corresponds to the parameter b.
* type 2 corresponds to the parameter c.
Returns:
Returns
-------
(:math:`N_{planes}`, ) :class:`numpy.ndarray` of int:
The plane types.
"""
Expand All @@ -73,7 +75,8 @@ def make_vertices(cls, a, b, c):
b (float): The b parameter.
c (float): The c parameter.
Returns:
Returns
-------
(:math:`N_{vertices}`, 3) :class:`numpy.ndarray` of float:
The vertices of the shape generated by the provided parameters.
"""
Expand Down Expand Up @@ -174,7 +177,8 @@ def get_shape(cls, a, c):
c (float):
The parameter :math:`c \in [1, 3]`.
Returns:
Returns
-------
:class:`~coxeter.shapes.ConvexPolyhedron`:
The desired shape.
"""
Expand Down Expand Up @@ -246,7 +250,8 @@ def get_shape(cls, a, c):
c (float):
The parameter :math:`c \in [2, 3]`.
Returns:
Returns
-------
:class:`~coxeter.shapes.ConvexPolyhedron`:
The desired shape.
"""
Expand Down Expand Up @@ -426,7 +431,8 @@ def get_shape(cls, a, c):
c (float):
The parameter :math:`c \in [S^2, 3]`.
Returns:
Returns
-------
:class:`~coxeter.shapes.ConvexPolyhedron`:
The desired shape.
"""
Expand Down Expand Up @@ -463,7 +469,8 @@ def get_shape(cls, truncation):
truncation (float):
The parameter :math:`truncation \in [0, 1]`.
Returns:
Returns
-------
:class:`~coxeter.shapes.ConvexPolyhedron`:
The desired truncated tetrahedron.
"""
Expand Down
9 changes: 6 additions & 3 deletions coxeter/families/tabulated_shape_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def from_mapping(cls, mapping, classname=None, docstring=None):
docstring (str, optional):
The docstring to apply to the class.
Returns:
Returns
-------
A subclass of this one associated with the the provided data.
"""

Expand Down Expand Up @@ -79,7 +80,8 @@ def from_json_file(cls, filename, *args, **kwargs):
\*\*kwargs:
Passed on to :meth:`~.from_mapping`.
Returns:
Returns
-------
A subclass of this one associated with the the provided data.
"""
with open(filename) as f:
Expand Down Expand Up @@ -108,7 +110,8 @@ def get_shape(cls, name):
name (str):
The key of the desired shape in the data dict.
Returns:
Returns
-------
:class:`~coxeter.shapes.Shape`: The requested shape.
"""
return from_gsd_type_shapes(cls.data[name])
5 changes: 3 additions & 2 deletions coxeter/shape_getters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2021 The Regents of the University of Michigan
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.
"""This module defines various convenience functions for generating shapes.
"""Module for defining various convenience functions for shape generation.
The methods here provide routes for generating instances of
:class:`~coxeter.shapes.Shape` based on certain pre-specified mappings.
Expand Down Expand Up @@ -42,7 +42,8 @@ def from_gsd_type_shapes(params, dimensions=3): # noqa: C901
instead of a :class:`~.shapes.Sphere` or
:class:`~.shapes.Ellipsoid` (Default value: 3).
Returns:
Returns
-------
:class:`~coxeter.shapes.Shape`:
The desired shape.
"""
Expand Down
16 changes: 10 additions & 6 deletions coxeter/shapes/base_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def is_inside(self, points):
points (:math:`(N, 3)` :class:`numpy.ndarray`):
The points to test.
Returns:
Returns
-------
:math:`(N, )` :class:`numpy.ndarray`:
Boolean array indicating which points are contained in the shape.
"""
Expand Down Expand Up @@ -117,7 +118,8 @@ def distance_to_surface(self, angles):
Angles between :math:`0` and :math:`2 \pi` over which to
calculate the distances. :math:`d` is the number of dimensions.
Returns:
Returns
-------
:math:`(N,)` :class:`numpy.ndarray`:
An array of distances from the center of the shape to its surface
at each of the given angles.
Expand Down Expand Up @@ -164,11 +166,13 @@ def to_plato_scene(self, backend="matplotlib", scene=None, scene_kwargs=None):
(Default value: None). Only used if ``scene`` is not provided
or None.
Returns:
Returns
-------
:class:`plato.draw.Scene`:
A scene containing this shape.
Raises:
Raises
------
NotImplementedError:
If no plato primitive corresponds to this coxeter shape class.
AttributeError:
Expand All @@ -180,10 +184,10 @@ def to_plato_scene(self, backend="matplotlib", scene=None, scene_kwargs=None):
import importlib

backend = importlib.import_module(f"plato.draw.{backend}")
except ImportError:
except ImportError as exception:
raise ImportError(
f"Backend plato.draw.{backend} could not be imported."
)
) from exception
if scene_kwargs is None:
scene_kwargs = {}
prim = self._plato_primitive(backend)
Expand Down
3 changes: 2 additions & 1 deletion coxeter/shapes/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def is_inside(self, points):
points (:math:`(N, 3)` :class:`numpy.ndarray`):
The points to test.
Returns:
Returns
-------
:math:`(N, )` :class:`numpy.ndarray`:
Boolean array indicating which points are contained in the
circle.
Expand Down
4 changes: 3 additions & 1 deletion coxeter/shapes/convex_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def _is_convex(vertices, normal):
normal (:math:`(3, )` :class:`numpy.ndarray`):
The normal to the vertices.
Returns:
Returns
-------
bool: ``True`` if ``vertices`` define a convex polygon.
"""
Expand Down Expand Up @@ -168,6 +169,7 @@ def incircle_from_center(self):
"The incircle_from_center property is deprecated, use "
"maximal_centered_bounded_circle instead.",
DeprecationWarning,
stacklevel=2,
)
return self.maximal_centered_bounded_circle

Expand Down
5 changes: 4 additions & 1 deletion coxeter/shapes/convex_polyhedron.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def is_inside(self, points):
points (:math:`(N, 3)` :class:`numpy.ndarray`):
The points to test.
Returns:
Returns
-------
:math:`(N, )` :class:`numpy.ndarray`:
Boolean array indicating which points are contained in the
polyhedron.
Expand All @@ -157,6 +158,7 @@ def insphere_from_center(self):
"The insphere_from_center property is deprecated, use "
"maximal_centered_bounded_sphere instead",
DeprecationWarning,
stacklevel=2,
)
return self.maximal_centered_bounded_sphere

Expand All @@ -172,6 +174,7 @@ def circumsphere_from_center(self):
"The circumsphere_from_center property is deprecated, use "
"minimal_centered_bounding_sphere instead",
DeprecationWarning,
stacklevel=2,
)
return self.minimal_centered_bounding_sphere

Expand Down
3 changes: 2 additions & 1 deletion coxeter/shapes/convex_spheropolyhedron.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ def is_inside(self, points):
points (:math:`(N, 3)` :class:`numpy.ndarray`):
The points to test.
Returns:
Returns
-------
:math:`(N, )` :class:`numpy.ndarray`:
Boolean array indicating which points are contained in the
spheropolyhedron.
Expand Down
3 changes: 2 additions & 1 deletion coxeter/shapes/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ def is_inside(self, points):
points (:math:`(N, 3)` :class:`numpy.ndarray`):
The points to test.
Returns:
Returns
-------
:math:`(N, )` :class:`numpy.ndarray`:
Boolean array indicating which points are contained in the
ellipsoid.
Expand Down
3 changes: 2 additions & 1 deletion coxeter/shapes/ellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ def is_inside(self, points):
points (:math:`(N, 3)` :class:`numpy.ndarray`):
The points to test.
Returns:
Returns
-------
:math:`(N, )` :class:`numpy.ndarray`:
Boolean array indicating which points are contained in the
ellipsoid.
Expand Down
12 changes: 8 additions & 4 deletions coxeter/shapes/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def _align_points_by_normal(normal, points):
points (:math:`(N, 3)` :class:`numpy.ndarray`):
The points that will be rotated and returned.
Returns:
Returns
-------
:math:`(N, 3)` :class:`numpy.ndarray`: The rotated points.
"""
# Since we are considering just a single vector, to avoid getting a pure
Expand Down Expand Up @@ -465,6 +466,7 @@ def bounding_circle(self):
"The bounding_circle property is deprecated, use "
"minimal_bounding_circle instead",
DeprecationWarning,
stacklevel=2,
)

return self.minimal_bounding_circle
Expand Down Expand Up @@ -516,7 +518,8 @@ def circumcircle(self):
of linear equations defined by this constraint, and the circumcircle
only exists if the resulting solution has no residual.
Raises:
Raises
------
RuntimeError: If no circumcircle exists for this polygon.
"""
# The circumsphere is defined by center C and radius r. For vertex i
Expand Down Expand Up @@ -664,7 +667,7 @@ def compute_form_factor_amplitude(self, q, density=1.0): # noqa: D102
return form_factor

def is_inside(self, points):
r"""Simple point-in-polygon algorithm based on winding number.
r"""Implement a simple point-in-polygon algorithm based on winding number.
The code in this function is based on implementation in :cite:`Dickinson2019`
which is licensed under the BSD-3 license.
Expand Down Expand Up @@ -703,7 +706,8 @@ def is_inside(self, points):
points (:math:`(N, 3)` or :math:`(N, 2)` :class:`numpy.ndarray`):
The points to test.
Returns:
Returns
-------
:math:`(N, )` :class:`numpy.ndarray`:
Boolean array indicating which points are contained in the
polyhedron.
Expand Down
Loading

0 comments on commit 6d72539

Please sign in to comment.