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

Lighter switch to ruff #2998

Merged
merged 31 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4806473
Basic configuration from other branch.
jhale Jan 18, 2024
f55135e
Ignore X | Y, our supported Python to old
jhale Jan 18, 2024
81e8a7f
Cannot use isort package from ruff.
jhale Jan 18, 2024
cebb334
Use traditional isort + ruff --fix .
jhale Jan 18, 2024
c3427f5
Unsafe fixes.
jhale Jan 18, 2024
b5b7191
Add back in isort.
jhale Jan 18, 2024
0b67404
Add ruff.toml
jhale Jan 18, 2024
fb1f579
Fix.
jhale Jan 18, 2024
54834fe
Ignore RUF012
jhale Jan 18, 2024
a32efe5
Fix circleci
jhale Jan 18, 2024
231d144
Remove deprecated numpy random call.
jhale Jan 18, 2024
d80318d
Add back isort.
jhale Jan 18, 2024
30a033d
Move Python minimum to 3.9 (discuss).
jhale Jan 18, 2024
83996a2
Merge branch 'main' into jhale/ruff-light
jhale Jan 18, 2024
3b33340
More fixes.
jhale Jan 18, 2024
a6287e5
Merge branch 'jhale/ruff-light' of github.com:FEniCS/dolfinx into jha…
jhale Jan 18, 2024
f6f4501
Merge remote-tracking branch 'origin/main' into jhale/ruff-light
jhale Jan 21, 2024
081d670
ruff fix.
jhale Jan 21, 2024
f7f6896
Fix isort configuration and re-isort.
jhale Jan 21, 2024
70be3d7
Fixes.
jhale Jan 21, 2024
30e2f45
Python 3.10
jhale Jan 22, 2024
0cb91ab
Merge remote-tracking branch 'origin/main' into jhale/ruff-light
jhale Jan 22, 2024
57f0a62
Not necessary.
jhale Jan 22, 2024
235515f
Add back in some removals.
jhale Jan 22, 2024
cce1384
Merge remote-tracking branch 'origin/main' into jhale/ruff-light
jhale Jan 22, 2024
5d03160
isort.
jhale Jan 22, 2024
383d406
Rocky and Intel OneAPI on Python 3.9
jhale Jan 22, 2024
82eaf51
Seed random number generator.
jhale Jan 22, 2024
41d8ace
Remove | stuff.
jhale Jan 22, 2024
93b2da0
Fix circleci.
jhale Jan 22, 2024
237e1f5
Merge remote-tracking branch 'origin/main' into jhale/ruff-light
jhale Jan 22, 2024
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
19 changes: 5 additions & 14 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,15 @@ jobs:
python3 -m pip install git+https://github.com/FEniCS/basix.git@${{ github.event.inputs.basix_ref }}
python3 -m pip install git+https://github.com/FEniCS/ffcx.git@${{ github.event.inputs.ffcx_ref }}

- name: Flake8 checks
run: |
cd cpp/
python3 -m flake8 cmake
python3 -m flake8 demo
python3 -m flake8 test
cd ..
cd python/
python3 -m flake8 dolfinx
python3 -m flake8 demo
python3 -m flake8 test
- name: isort checks (non-blocking)
continue-on-error: true
- name: ruff and isort C++ .py file checks
run: |
cd cpp/
ruff check .
python3 -m isort --check .
cd ..
- name: ruff and isort Python interface checks
run: |
cd python/
ruff check .
python3 -m isort --check .
- name: mypy checks
run: |
Expand Down
1 change: 1 addition & 0 deletions cpp/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extend = "../python/pyproject.toml"
3 changes: 2 additions & 1 deletion python/demo/demo_lagrange_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# We begin this demo by importing the required modules.

import typing

from mpi4py import MPI

# +
Expand All @@ -26,7 +27,7 @@

import basix
import basix.ufl
import ufl # type: ignore
import ufl # type: ignore
from dolfinx import default_scalar_type, fem, mesh
from dolfinx.fem.petsc import LinearProblem
from ufl import ds, dx, grad, inner
Expand Down
3 changes: 2 additions & 1 deletion python/demo/demo_static-condensation.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
from dolfinx.io import XDMFFile
from dolfinx.jit import ffcx_jit
from dolfinx.mesh import locate_entities_boundary, meshtags
from ffcx.codegeneration.utils import numba_ufcx_kernel_signature as ufcx_signature
from ffcx.codegeneration.utils import \
numba_ufcx_kernel_signature as ufcx_signature

if default_real_type == np.float32:
print("float32 not yet supported for this demo.")
Expand Down
4 changes: 2 additions & 2 deletions python/demo/demo_tnt-elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from mpi4py import MPI

# +
import matplotlib
import matplotlib as mpl
import matplotlib.pylab as plt
import numpy as np

Expand All @@ -34,7 +34,7 @@
from ufl import (SpatialCoordinate, TestFunction, TrialFunction, cos, div, dx,
grad, inner, sin)

matplotlib.use('agg')
mpl.use('agg')
# -

# ## Defining a degree 1 TNT element
Expand Down
3 changes: 2 additions & 1 deletion python/dolfinx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

from dolfinx import common
from dolfinx import cpp as _cpp
from dolfinx import fem, geometry, graph, io, jit, la, log, mesh, nls, plot, utils
from dolfinx import (fem, geometry, graph, io, jit, la, log, mesh, nls, plot,
utils)
# Initialise logging
from dolfinx.common import (TimingType, git_commit_hash, has_debug, has_kahip,
has_parmetis, list_timings, timing)
Expand Down
12 changes: 6 additions & 6 deletions python/dolfinx/fem/assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _assemble_vector_array(b: np.ndarray, L: Form, constants=None, coeffs=None):


@functools.singledispatch
def assemble_matrix(a: typing.Any, bcs: typing.Optional[typing.List[DirichletBC]] = None,
def assemble_matrix(a: typing.Any, bcs: typing.Optional[list[DirichletBC]] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change, and all similar ones, will break python < 3.9. I think that ruff uses by default whatever version of python is available in the docker image (3.10, if I remember correctly), but there is a configuration option to prevent it from doing that. However, I don't remember which of the rules is enforcing this change (pyupgrade maybe?) to check the actual name of this option.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should discuss deprecating 3.8, only has ~6 months of support left.

Copy link
Member Author

@jhale jhale Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this. The target version is inferred from pyproject.toml.

ruff --show-settings | grep target_version

gives

target_version: Py38,
target_version: Py38,

diagonal: float = 1.0, constants=None, coeffs=None,
block_mode: typing.Optional[la.BlockMode] = None):
"""Assemble bilinear form into a matrix.
Expand Down Expand Up @@ -246,7 +246,7 @@ def assemble_matrix(a: typing.Any, bcs: typing.Optional[typing.List[DirichletBC]


@assemble_matrix.register
def _assemble_matrix_csr(A: la.MatrixCSR, a: Form, bcs: typing.Optional[typing.List[DirichletBC]] = None,
def _assemble_matrix_csr(A: la.MatrixCSR, a: Form, bcs: typing.Optional[list[DirichletBC]] = None,
diagonal: float = 1.0, constants=None, coeffs=None) -> la.MatrixCSR:
"""Assemble bilinear form into a matrix.

Expand Down Expand Up @@ -284,9 +284,9 @@ def _assemble_matrix_csr(A: la.MatrixCSR, a: Form, bcs: typing.Optional[typing.L
# -- Modifiers for Dirichlet conditions ---------------------------------------


def apply_lifting(b: np.ndarray, a: typing.List[Form],
bcs: typing.List[typing.List[DirichletBC]],
x0: typing.Optional[typing.List[np.ndarray]] = None,
def apply_lifting(b: np.ndarray, a: list[Form],
bcs: list[list[DirichletBC]],
x0: typing.Optional[list[np.ndarray]] = None,
scale: float = 1.0, constants=None, coeffs=None) -> None:
"""Modify RHS vector b for lifting of Dirichlet boundary conditions.

Expand Down Expand Up @@ -316,7 +316,7 @@ def apply_lifting(b: np.ndarray, a: typing.List[Form],
_cpp.fem.apply_lifting(b, _a, constants, coeffs, _bcs, x0, scale)


def set_bc(b: np.ndarray, bcs: typing.List[DirichletBC],
def set_bc(b: np.ndarray, bcs: list[DirichletBC],
x0: typing.Optional[np.ndarray] = None, scale: float = 1.0) -> None:
"""Insert boundary condition values into vector.

Expand Down
2 changes: 1 addition & 1 deletion python/dolfinx/fem/bcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def dirichletbc(value: typing.Union[Function, Constant, np.ndarray],


def bcs_by_block(spaces: typing.Iterable[typing.Union[dolfinx.fem.FunctionSpace, None]],
bcs: typing.Iterable[DirichletBC]) -> typing.List[typing.List[DirichletBC]]:
bcs: typing.Iterable[DirichletBC]) -> list[list[DirichletBC]]:
"""Arrange Dirichlet boundary conditions by the function space that
they constrain.

Expand Down
2 changes: 1 addition & 1 deletion python/dolfinx/fem/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def rank(self) -> int:
return self._cpp_object.rank # type: ignore

@property
def function_spaces(self) -> typing.List[FunctionSpace]:
def function_spaces(self) -> list[FunctionSpace]:
"""Function spaces on which this form is defined"""
return self._cpp_object.function_spaces # type: ignore

Expand Down
6 changes: 3 additions & 3 deletions python/dolfinx/fem/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def sub(self, i: int) -> Function:
returned `Function` should be stored by the caller to avoid
repeated re-computation of the subspac.
"""
return Function(self._V.sub(i), self.x, name=f"{str(self)}_{i}")
return Function(self._V.sub(i), self.x, name=f"{self!s}_{i}")

def split(self) -> tuple[Function, ...]:
"""Extract (any) sub-functions.
Expand Down Expand Up @@ -492,13 +492,13 @@ class ElementMetaData(typing.NamedTuple):
"""
family: str
degree: int
shape: typing.Optional[typing.Tuple[int, ...]] = None
shape: typing.Optional[tuple[int, ...]] = None
symmetry: typing.Optional[bool] = None


def functionspace(mesh: Mesh,
element: typing.Union[ufl.FiniteElementBase, ElementMetaData,
typing.Tuple[str, int, typing.Tuple, bool]],
tuple[str, int, tuple, bool]],
form_compiler_options: typing.Optional[dict[str, typing.Any]] = None,
jit_options: typing.Optional[dict[str, typing.Any]] = None) -> FunctionSpace:
"""Create a finite element function space.
Expand Down
66 changes: 33 additions & 33 deletions python/dolfinx/fem/petsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"LinearProblem", "NonlinearProblem"]


def _extract_function_spaces(a: typing.List[typing.List[Form]]):
def _extract_function_spaces(a: list[list[Form]]):
"""From a rectangular array of bilinear forms, extract the function
spaces for each block row and block column.

Expand All @@ -58,8 +58,8 @@ def fn(form):
Vblock: typing.Iterable = map(partial(map, fn), a)

# Compute spaces for each row/column block
rows: typing.List[typing.Set] = [set() for i in range(len(a))]
cols: typing.List[typing.Set] = [set() for i in range(len(a[0]))]
rows: list[set] = [set() for i in range(len(a))]
cols: list[set] = [set() for i in range(len(a[0]))]
for i, Vrow in enumerate(Vblock):
for j, V in enumerate(Vrow):
if V is not None:
Expand Down Expand Up @@ -90,7 +90,7 @@ def create_vector(L: Form) -> PETSc.Vec:
return create_petsc_vector(dofmap.index_map, dofmap.index_map_bs)


def create_vector_block(L: typing.List[Form]) -> PETSc.Vec:
def create_vector_block(L: list[Form]) -> PETSc.Vec:
"""Create a PETSc vector (blocked) that is compaible with a list of linear forms.

Args:
Expand All @@ -105,7 +105,7 @@ def create_vector_block(L: typing.List[Form]) -> PETSc.Vec:
return _cpp.fem.petsc.create_vector_block(maps)


def create_vector_nest(L: typing.List[Form]) -> PETSc.Vec:
def create_vector_nest(L: list[Form]) -> PETSc.Vec:
"""Create a PETSc nested vector (``VecNest``) that is compatible with a list of linear forms.

Args:
Expand Down Expand Up @@ -140,7 +140,7 @@ def create_matrix(a: Form, mat_type=None) -> PETSc.Mat:
return _cpp.fem.petsc.create_matrix(a._cpp_object, mat_type)


def create_matrix_block(a: typing.List[typing.List[Form]]) -> PETSc.Mat:
def create_matrix_block(a: list[list[Form]]) -> PETSc.Mat:
"""Create a PETSc matrix that is compatible with a rectangular array of bilinear forms.

Args:
Expand All @@ -155,7 +155,7 @@ def create_matrix_block(a: typing.List[typing.List[Form]]) -> PETSc.Mat:
return _cpp.fem.petsc.create_matrix_block(_a)


def create_matrix_nest(a: typing.List[typing.List[Form]]) -> PETSc.Mat:
def create_matrix_nest(a: list[list[Form]]) -> PETSc.Mat:
"""Create a PETSc matrix (``MatNest``) that is compatible with a rectangular array of bilinear forms.

Args:
Expand Down Expand Up @@ -232,7 +232,7 @@ def assemble_vector_nest(L: typing.Any, constants=None, coeffs=None) -> PETSc.Ve


@assemble_vector_nest.register
def _assemble_vector_nest_vec(b: PETSc.Vec, L: typing.List[Form], constants=None, coeffs=None) -> PETSc.Vec:
def _assemble_vector_nest_vec(b: PETSc.Vec, L: list[Form], constants=None, coeffs=None) -> PETSc.Vec:
"""Assemble linear forms into a nested PETSc (``VecNest``) vector. The
vector is not zeroed before assembly and it is not finalised, i.e.
ghost values are not accumulated on the owning processes.
Expand All @@ -248,9 +248,9 @@ def _assemble_vector_nest_vec(b: PETSc.Vec, L: typing.List[Form], constants=None

# FIXME: Revise this interface
@functools.singledispatch
def assemble_vector_block(L: typing.List[Form],
a: typing.List[typing.List[Form]],
bcs: typing.List[DirichletBC] = [],
def assemble_vector_block(L: list[Form],
a: list[list[Form]],
bcs: list[DirichletBC] = [],
x0: typing.Optional[PETSc.Vec] = None,
scale: float = 1.0,
constants_L=None, coeffs_L=None,
Expand All @@ -270,9 +270,9 @@ def assemble_vector_block(L: typing.List[Form],

@assemble_vector_block.register
def _assemble_vector_block_vec(b: PETSc.Vec,
L: typing.List[Form],
a: typing.List[typing.List[Form]],
bcs: typing.List[DirichletBC] = [],
L: list[Form],
a: list[list[Form]],
bcs: list[DirichletBC] = [],
x0: typing.Optional[PETSc.Vec] = None,
scale: float = 1.0,
constants_L=None, coeffs_L=None,
Expand Down Expand Up @@ -330,7 +330,7 @@ def _assemble_vector_block_vec(b: PETSc.Vec,

# -- Matrix assembly ---------------------------------------------------------
@functools.singledispatch
def assemble_matrix(a: typing.Any, bcs: typing.List[DirichletBC] = [],
def assemble_matrix(a: typing.Any, bcs: list[DirichletBC] = [],
diagonal: float = 1.0, constants=None, coeffs=None):
"""Assemble bilinear form into a matrix. The returned matrix is not
finalised, i.e. ghost values are not accumulated.
Expand All @@ -357,7 +357,7 @@ def assemble_matrix(a: typing.Any, bcs: typing.List[DirichletBC] = [],


@assemble_matrix.register
def assemble_matrix_mat(A: PETSc.Mat, a: Form, bcs: typing.List[DirichletBC] = [],
def assemble_matrix_mat(A: PETSc.Mat, a: Form, bcs: list[DirichletBC] = [],
diagonal: float = 1.0, constants=None, coeffs=None) -> PETSc.Mat:
"""Assemble bilinear form into a matrix. The returned matrix is not
finalised, i.e. ghost values are not accumulated.
Expand All @@ -376,8 +376,8 @@ def assemble_matrix_mat(A: PETSc.Mat, a: Form, bcs: typing.List[DirichletBC] = [

# FIXME: Revise this interface
@functools.singledispatch
def assemble_matrix_nest(a: typing.List[typing.List[Form]],
bcs: typing.List[DirichletBC] = [], mat_types=[],
def assemble_matrix_nest(a: list[list[Form]],
bcs: list[DirichletBC] = [], mat_types=[],
diagonal: float = 1.0, constants=None, coeffs=None) -> PETSc.Mat:
"""Create a nested matrix and assembled bilinear forms into the matrix.

Expand All @@ -402,8 +402,8 @@ def assemble_matrix_nest(a: typing.List[typing.List[Form]],


@assemble_matrix_nest.register
def _assemble_matrix_nest_mat(A: PETSc.Mat, a: typing.List[typing.List[Form]],
bcs: typing.List[DirichletBC] = [], diagonal: float = 1.0,
def _assemble_matrix_nest_mat(A: PETSc.Mat, a: list[list[Form]],
bcs: list[DirichletBC] = [], diagonal: float = 1.0,
constants=None, coeffs=None) -> PETSc.Mat:
"""Assemble bilinear forms into a nested matrix

Expand Down Expand Up @@ -445,8 +445,8 @@ def _assemble_matrix_nest_mat(A: PETSc.Mat, a: typing.List[typing.List[Form]],

# FIXME: Revise this interface
@functools.singledispatch
def assemble_matrix_block(a: typing.List[typing.List[Form]],
bcs: typing.List[DirichletBC] = [],
def assemble_matrix_block(a: list[list[Form]],
bcs: list[DirichletBC] = [],
diagonal: float = 1.0,
constants=None, coeffs=None) -> PETSc.Mat: # type: ignore
"""Assemble bilinear forms into a blocked matrix."""
Expand All @@ -456,8 +456,8 @@ def assemble_matrix_block(a: typing.List[typing.List[Form]],


@assemble_matrix_block.register
def _assemble_matrix_block_mat(A: PETSc.Mat, a: typing.List[typing.List[Form]],
bcs: typing.List[DirichletBC] = [], diagonal: float = 1.0,
def _assemble_matrix_block_mat(A: PETSc.Mat, a: list[list[Form]],
bcs: list[DirichletBC] = [], diagonal: float = 1.0,
constants=None, coeffs=None) -> PETSc.Mat:
"""Assemble bilinear forms into a blocked matrix."""
constants = [[_pack_constants(form._cpp_object) if form is not None else np.array(
Expand Down Expand Up @@ -503,9 +503,9 @@ def _assemble_matrix_block_mat(A: PETSc.Mat, a: typing.List[typing.List[Form]],

# -- Modifiers for Dirichlet conditions ---------------------------------------

def apply_lifting(b: PETSc.Vec, a: typing.List[Form],
bcs: typing.List[typing.List[DirichletBC]],
x0: typing.List[PETSc.Vec] = [],
def apply_lifting(b: PETSc.Vec, a: list[Form],
bcs: list[list[DirichletBC]],
x0: list[PETSc.Vec] = [],
scale: float = 1.0, constants=None, coeffs=None) -> None:
"""Apply the function :func:`dolfinx.fem.apply_lifting` to a PETSc Vector."""
with contextlib.ExitStack() as stack:
Expand All @@ -515,8 +515,8 @@ def apply_lifting(b: PETSc.Vec, a: typing.List[Form],
_assemble.apply_lifting(b_local.array_w, a, bcs, x0_r, scale, constants, coeffs)


def apply_lifting_nest(b: PETSc.Vec, a: typing.List[typing.List[Form]],
bcs: typing.List[DirichletBC],
def apply_lifting_nest(b: PETSc.Vec, a: list[list[Form]],
bcs: list[DirichletBC],
x0: typing.Optional[PETSc.Vec] = None,
scale: float = 1.0, constants=None, coeffs=None) -> PETSc.Vec:
"""Apply the function :func:`dolfinx.fem.apply_lifting` to each sub-vector in a nested PETSc Vector."""
Expand All @@ -532,15 +532,15 @@ def apply_lifting_nest(b: PETSc.Vec, a: typing.List[typing.List[Form]],
return b


def set_bc(b: PETSc.Vec, bcs: typing.List[DirichletBC],
def set_bc(b: PETSc.Vec, bcs: list[DirichletBC],
x0: typing.Optional[PETSc.Vec] = None, scale: float = 1.0) -> None:
"""Apply the function :func:`dolfinx.fem.set_bc` to a PETSc Vector."""
if x0 is not None:
x0 = x0.array_r
_assemble.set_bc(b.array_w, bcs, x0, scale)


def set_bc_nest(b: PETSc.Vec, bcs: typing.List[typing.List[DirichletBC]],
def set_bc_nest(b: PETSc.Vec, bcs: list[list[DirichletBC]],
x0: typing.Optional[PETSc.Vec] = None, scale: float = 1.0) -> None:
"""Apply the function :func:`dolfinx.fem.set_bc` to each sub-vector of a nested PETSc Vector."""
_b = b.getNestSubVecs()
Expand All @@ -557,7 +557,7 @@ class LinearProblem:

"""

def __init__(self, a: ufl.Form, L: ufl.Form, bcs: typing.List[DirichletBC] = [],
def __init__(self, a: ufl.Form, L: ufl.Form, bcs: list[DirichletBC] = [],
u: typing.Optional[_Function] = None,
petsc_options: typing.Optional[dict] = None,
form_compiler_options: typing.Optional[dict] = None,
Expand Down Expand Up @@ -691,7 +691,7 @@ class NonlinearProblem:

"""

def __init__(self, F: ufl.form.Form, u: _Function, bcs: typing.List[DirichletBC] = [],
def __init__(self, F: ufl.form.Form, u: _Function, bcs: list[DirichletBC] = [],
J: ufl.form.Form = None, form_compiler_options: typing.Optional[dict] = None,
jit_options: typing.Optional[dict] = None):
"""Initialize solver for solving a non-linear problem using Newton's method, :math:`(dF/du)(u) du = -F(u)`.
Expand Down
Loading