From 05323dc6b35295f24536aab4ce9b664577f2483f Mon Sep 17 00:00:00 2001 From: Davor Dundovic <33790330+ddundo@users.noreply.github.com> Date: Tue, 10 Sep 2024 09:25:21 +0200 Subject: [PATCH] Enable B and C linting rules (#123) Closes #72. --- movement/math.py | 2 +- movement/monge_ampere.py | 7 ++++--- movement/mover.py | 5 +++-- movement/spring.py | 10 ++++------ movement/tangling.py | 2 +- pyproject.toml | 4 ++-- test/monitors.py | 2 +- test/test_forced_movement.py | 3 ++- 8 files changed, 18 insertions(+), 17 deletions(-) diff --git a/movement/math.py b/movement/math.py index 796c927..34b724e 100644 --- a/movement/math.py +++ b/movement/math.py @@ -33,7 +33,7 @@ def equation_of_hyperplane(*points): try: hyperplane = Hyperplane(*(Point(points[i]) for i in indices[:dim])) - def equation(*xyz): + def equation(*xyz, hyperplane=hyperplane): return hyperplane.distance(Point(xyz)) return equation diff --git a/movement/monge_ampere.py b/movement/monge_ampere.py index c765b22..9ea3ada 100644 --- a/movement/monge_ampere.py +++ b/movement/monge_ampere.py @@ -85,8 +85,8 @@ def MongeAmpereMover(mesh, monitor_function, method="relaxation", **kwargs): } try: return implemented_methods[method](mesh, monitor_function, **kwargs) - except KeyError: - raise ValueError(f"Method '{method}' not recognised.") + except KeyError as e: + raise ValueError(f"Method '{method}' not recognised.") from e def tangential(v, n): @@ -156,7 +156,8 @@ def __init__(self, mesh, monitor_function, **kwargs): if len(self._all_boundary_segments) == 0: warn( "Provided mesh has no boundary segments with Physical ID tags. If the " - "boundaries aren't fully periodic then this will likely cause errors." + "boundaries aren't fully periodic then this will likely cause errors.", + stacklevel=1, ) elif ( len(self.fixed_boundary_segments) == 1 diff --git a/movement/mover.py b/movement/mover.py index 0ff499c..2e1267a 100644 --- a/movement/mover.py +++ b/movement/mover.py @@ -46,7 +46,8 @@ def __init__( if not raise_convergence_errors: warn( f"{type(self)}.move called with raise_convergence_errors=False." - " Beware: this option can produce poor quality meshes!" + " Beware: this option can produce poor quality meshes!", + stacklevel=1, ) self.raise_convergence_errors = raise_convergence_errors self.dim = self.mesh.topological_dimension() @@ -65,7 +66,7 @@ def __init__( self._local_coordinates_vec = dm_coords.createLocalVec() self._update_plex_coordinates() except ValueError: - warn("Cannot update DMPlex coordinates for periodic meshes.") + warn("Cannot update DMPlex coordinates for periodic meshes.", stacklevel=1) self._local_coordinates_vec = None self.dx = firedrake.dx(domain=self.mesh, degree=quadrature_degree) diff --git a/movement/spring.py b/movement/spring.py index 6bfb252..92a597a 100644 --- a/movement/spring.py +++ b/movement/spring.py @@ -186,10 +186,9 @@ def assemble_stiffness_matrix(self, boundary_conditions=None): :returns: the stiffness matrix with boundary conditions applied :rtype: :class:`numpy.ndarray` """ - if not boundary_conditions: - boundary_conditions = firedrake.DirichletBC( - self.coord_space, 0, "on_boundary" - ) + boundary_conditions = boundary_conditions or firedrake.DirichletBC( + self.coord_space, 0, "on_boundary" + ) if isinstance(boundary_conditions, firedrake.DirichletBC): boundary_conditions = [boundary_conditions] assert isinstance(boundary_conditions, Iterable) @@ -208,8 +207,7 @@ def assemble_stiffness_matrix(self, boundary_conditions=None): tags = boundary_condition.sub_domain if tags == "on_boundary": tags = bnd.unique_markers - if not isinstance(tags, Iterable): - tags = [tags] + tags = [tags] if not isinstance(tags, Iterable) else tags if not set(tags).issubset(set(bnd.unique_markers)): raise ValueError(f"{tags} contains invalid boundary tags.") subsets = np.array([bnd.subset(tag).indices for tag in tags]).flatten() diff --git a/movement/tangling.py b/movement/tangling.py index 638cb8b..90f840b 100644 --- a/movement/tangling.py +++ b/movement/tangling.py @@ -64,5 +64,5 @@ def check(self): msg = f"Mesh has {num_tangled} tangled element{plural}." if self.raise_error: raise ValueError(msg) - warnings.warn(msg) + warnings.warn(msg, stacklevel=1) return num_tangled diff --git a/pyproject.toml b/pyproject.toml index 0d7b416..5fc0d58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,8 +38,8 @@ line-length = 88 [tool.ruff.lint] select = [ -# "B", # flake8-bugbear TODO: enable this (#72) -# "C", # mccabe complexity TODO: enable this (#72) + "B", # flake8-bugbear + "C", # mccabe complexity "E", "W", # Pycodestyle "F", # Pyflakes "I", # isort diff --git a/test/monitors.py b/test/monitors.py index 9b584dd..6f0ebd5 100644 --- a/test/monitors.py +++ b/test/monitors.py @@ -10,6 +10,6 @@ def ring_monitor(mesh): beta = Constant(200.0) # width gamma = Constant(0.15) # radius dim = mesh.geometric_dimension() - xyz = SpatialCoordinate(mesh) - as_vector([0.5]*dim) + xyz = SpatialCoordinate(mesh) - as_vector([0.5] * dim) r = dot(xyz, xyz) return Constant(1.0) + alpha / cosh(beta * (r - gamma)) ** 2 diff --git a/test/test_forced_movement.py b/test/test_forced_movement.py index 6926eba..34d74b9 100644 --- a/test/test_forced_movement.py +++ b/test/test_forced_movement.py @@ -21,9 +21,10 @@ def move( mesh, fixed_boundary_tags=None, moving_boundary_tags=None, - vector=[1, 0], + vector=None, **kwargs, ): + vector = vector or [1, 0] mover = self.mover(mesh) bcs = [] if fixed_boundary_tags: