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 10 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
9 changes: 4 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ install-python-components: &install-python-components
pip3 install git+https://github.com/FEniCS/ufl.git
pip3 install git+https://github.com/FEniCS/ffcx.git

flake8-python-code: &flake8-python-code
ruff-isort-python-code: &ruff-isort-python-code
name: Flake8 checks on Python code
command: |
cd python/
python3 -m flake8 dolfinx
python3 -m flake8 demo
python3 -m flake8 test
ruff check .
isort --check .

configure-cpp: &configure-cpp
name: Configure (C++)
Expand Down Expand Up @@ -101,7 +100,7 @@ jobs:
steps:
- checkout
- run: *install-python-components
- run: *flake8-python-code
- run: *ruff-isort-python-code
- run: *configure-cpp
- run: *build-install-cpp

Expand Down
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
Loading
Loading