Skip to content

Code cleanup: Apply Ruff for formatting and linting - #173

Merged
sy-cui merged 65 commits into
mainfrom
format/lint
Apr 3, 2026
Merged

Code cleanup: Apply Ruff for formatting and linting#173
sy-cui merged 65 commits into
mainfrom
format/lint

Conversation

@sy-cui

@sy-cui sy-cui commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

The goal of this PR is to use Ruff to reformat the code. This includes:

  • Reformat the code
  • Converge on a reasonable set of lint rules that the code should comply to

Several fixes related to this change:

  1. Circular import problems in sopht/simulator
  2. isort is handled natively by ruff Add isort for sorting imports #83

Work in progress

The following compiles a list of rules listed by Ruff that sopht has yet to comply to. This PR may not resolve all of them

  • NPY002 numpy-legacy-random
  • N806 non-lowercase-variable-in-function
  • UP031 printf-string-formatting
  • ARG002 unused-method-argument
  • N803 invalid-argument-name
  • RUF059 unused-unpacked-variable
  • G004 logging-f-string
  • PT011 pytest-raises-too-broad
  • SLF001 private-member-access
  • C408 unnecessary-collection-call
  • PERF401 manual-list-comprehension
  • N802 invalid-function-name
  • PLC0206 dict-index-missing-items
  • RUF013 implicit-optional
  • T201 print
  • TC003 typing-only-standard-library-import
  • PLR0402 manual-from-import
  • RUF002 ambiguous-unicode-character-docstring
  • UP007 non-pep604-annotation-union

Additional notes

  • Lint checks for all docstrings are disabled at this moment. In the future, it should be enabled at least for the sopht package (we can be lenient with examples and tests). This should be resolved together with Documentation for SophT #115

@sy-cui sy-cui self-assigned this Apr 1, 2026
@sy-cui sy-cui added prio:low Low priority refactor Refactor existing codebase labels Apr 1, 2026
@sy-cui
sy-cui requested review from Copilot and removed request for Copilot April 3, 2026 03:49
@sy-cui
sy-cui marked this pull request as ready for review April 3, 2026 03:58
@sy-cui
sy-cui requested a review from Copilot April 3, 2026 03:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR applies Ruff formatting/linting across the repository and includes a handful of small refactors needed to satisfy the chosen Ruff ruleset (notably around imports, logging, test determinism, and shell-out usage).

Changes:

  • Standardize formatting/linting via Ruff (including import sorting) and add per-folder Ruff configs for tests/ and examples/.
  • Refactor several modules/tests for determinism and safer operations (e.g., pytest RNG fixture, avoid os.system, structured logging).
  • Restructure/clarify public exports via __all__ in key packages and adjust some simulator factory entrypoints.

Reviewed changes

Copilot reviewed 171 out of 173 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
uv.lock Lockfile updated for new deps (e.g., ffmpeg-python, future, typing-extensions).
pyproject.toml Adds deps (ffmpeg-python, typing-extensions), expands Ruff to select=["ALL"], and adjusts line length/excludes.
justfile Adds format/check tasks and makes pytest invocation more verbose.
README.md Adds MacOS-specific pystencils/OpenMP troubleshooting section.
.gitignore Ignores .vscode/.
.coveragerc Adjusts omit list (no longer omits rod_viz.py).
tests/ruff.toml Ruff config for tests (extends root; ignores docstyle/namespace/assert rules).
tests/conftest.py Adds deterministic rng fixture for tests.
tests/test_utils/test_restart.py Uses helper cleanup, f-strings, and formatting updates.
tests/test_utils/test_precision.py Formatting-only update to pytest.raises.
tests/test_simulator/test_immersed_body/test_immersed_body_forcing_grid.py Strengthens logging assertions by checking emitted logger records.
tests/test_simulator/test_immersed_body/rigid_body/test_derived_rigid_bodies.py Avoids mutable default args; asserts warnings via log records; formatting updates.
tests/test_simulator/test_immersed_body/cosserat_rod/test_cosserat_rod_flow_interaction.py Import order formatting.
tests/test_simulator/test_flow/test_passive_transport_flow_simulators.py Uses rng fixture instead of global np.random; minor formatting.
tests/test_simulator/test_flow/test_flow_simulators_3d.py Switches to create_unbounded_flow_simulator_3d, adds rng usage, and updates cfl kwarg.
tests/test_simulator/test_flow/test_flow_simulators_2d.py Switches to create_unbounded_flow_simulator_2d and adds rng usage.
tests/test_numeric/test_immersed_boundary_ops/test_brinkmann_boundary_forcing.py Uses rng fixture; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_vorticity_stretching_flux_3d.py Uses injected RNG; reshapes random generation; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_update_vorticity_from_velocity_forcing_3d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_laplacian_filter_3d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_elementwise_ops_3d.py Import ordering and formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_divergence_3d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_diffusion_timestep_3d.py Uses injected RNG; small refactors/formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_diffusion_flux_3d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_curl_3d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_char_func_from_level_set_3d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_3d/test_brinkmann_penalise_3d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_update_vorticity_from_velocity_forcing_2d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_penalise_field_boundary_2d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_outplane_field_curl_2d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_inplane_field_curl_2d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_elementwise_ops_2d.py Import ordering and formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_diffusion_timestep_2d.py Uses injected RNG; refactors helper solution signature; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_diffusion_flux_2d.py Uses injected RNG; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_char_func_from_level_set_2d.py Uses injected RNG; refactors solution signature; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_advection_timestep_2d.py Uses injected RNG; refactors solution signature; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_stencil_ops_2d/test_advection_flux_2d.py Uses injected RNG; refactors solution signature; formatting cleanup.
tests/test_numeric/test_eulerian_grid_ops/test_poisson_solver_3d/test_fft_kernel_3d.py Uses injected RNG; import cleanup and formatting.
tests/test_numeric/test_eulerian_grid_ops/test_poisson_solver_2d/test_fft_kernel_2d.py Uses injected RNG; type annotations; formatting cleanup.
sopht/init.py Adds NullHandler to avoid “No handler found” warnings.
sopht/utils/restart_sim.py Uses pathlib and module logger; avoids print; improves exception messages.
sopht/utils/pyst_kernel_config.py Improves error messages/types; minor signature typing; formatting cleanup.
sopht/utils/precision.py Simplifies control flow; improves error message handling; formatting cleanup.
sopht/utils/post_process.py Replaces shelling-out with ffmpeg-python + pathlib cleanup/moves.
sopht/utils/lab_cmap.py Import order + small return simplification.
sopht/utils/field.py Normalizes TODO comment formatting.
sopht/utils/init.py Adds __all__ and reorganizes exports.
sopht/simulator/immersed_body/rigid_body/rigid_body_flow_interaction.py Uses explicit imports, typing updates, and override.
sopht/simulator/immersed_body/rigid_body/derived_rigid_bodies.py Logging cleanup, override, super() modernization, and message formatting.
sopht/simulator/immersed_body/rigid_body/init.py Removes re-exports; replaces with module docstring.
sopht/simulator/immersed_body/immersed_body_forcing_grid.py Uses module logger; updates warning formatting.
sopht/simulator/immersed_body/immersed_body_flow_interaction.py Logging restructuring; typing updates; minor return simplification.
sopht/simulator/immersed_body/flow_forces.py Import refactor and override usage; formatting cleanup.
sopht/simulator/immersed_body/cosserat_rod/cosserat_rod_flow_interaction.py Uses explicit imports, typing updates, and override.
sopht/simulator/immersed_body/cosserat_rod/init.py Removes re-exports; replaces with module docstring.
sopht/simulator/immersed_body/cosserat_rod_cpp/cosserat_rod_cpp_forcing_grids.py Removes unused/dead module content.
sopht/simulator/immersed_body/cosserat_rod_cpp/cosserat_rod_cpp_flow_interaction.py Removes unused/dead module content.
sopht/simulator/immersed_body/cosserat_rod_cpp/init.py Removes re-exports for deleted CPP modules.
sopht/simulator/immersed_body/init.py Replaces star exports with explicit exports and __all__.
sopht/simulator/flow/flow_simulators_3d.py Renames factory to create_unbounded_flow_simulator_3d and switches CFLcfl.
sopht/simulator/flow/flow_simulators_2d.py Renames factory to create_unbounded_flow_simulator_2d and switches CFLcfl.
sopht/simulator/flow/init.py Reworks exports and adds __all__.
sopht/simulator/init.py Replaces star exports with explicit exports and __all__.
sopht/numeric/immersed_boundary_ops/VirtualBoundaryForcing.py Replaces assert with ValueError; formatting cleanup.
sopht/numeric/immersed_boundary_ops/experimental/BrinkmannBoundaryForcing.py Replaces assert with ValueError; formatting cleanup.
sopht/numeric/immersed_boundary_ops/experimental/init.py Adds module docstring.
sopht/numeric/immersed_boundary_ops/init.py Adds explicit __all__ and export ordering.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/vorticity_stretching_timestep_3d.py Imports cleanup + typing (collections.abc.Callable) + formatting.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/vorticity_stretching_flux_3d.py Imports cleanup + formatting improvements.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/divergence_3d.py Imports cleanup + formatting improvements.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/diffusion_timestep_3d.py Imports cleanup + formatting improvements.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/diffusion_flux_3d.py Imports cleanup + improved error message handling.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/curl_3d.py Imports cleanup + formatting improvements.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/char_func_from_level_set_3d.py Simplifies to direct return of compiled kernel; imports cleanup.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/brinkmann_penalise_3d.py Imports cleanup + improved error message handling.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/advection_timestep_3d.py Imports cleanup + improved error message handling.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/advection_flux_3d.py Imports cleanup + comment formatting updates.
sopht/numeric/eulerian_grid_ops/stencil_ops_3d/init.py Removes re-export surface (keeps docstring only).
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/update_vorticity_from_velocity_forcing_2d.py Imports cleanup + TODO comment normalization.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/penalise_field_boundary_2d.py Replaces assert with ValueError; imports/formatting cleanup.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/outplane_field_curl_2d.py Imports cleanup + TODO comment normalization.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/inplane_field_curl_2d.py Minor signature formatting and kernel expression formatting.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/diffusion_timestep_2d.py Imports cleanup + formatting improvements.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/diffusion_flux_2d.py Imports cleanup + formatting improvements.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/char_func_from_level_set_2d.py Simplifies to direct return of compiled kernel; imports cleanup.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/brinkmann_penalise_2d.py Improved error message handling; formatting cleanup.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/advection_timestep_2d.py Imports cleanup + formatting improvements.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/advection_flux_2d.py Imports cleanup + TODO comment normalization.
sopht/numeric/eulerian_grid_ops/stencil_ops_2d/init.py Removes re-export surface (keeps docstring only).
sopht/numeric/eulerian_grid_ops/poisson_solver_3d/UnboundedPoissonSolverPYFFTW3D.py Formatting cleanup + TODO comment normalization.
sopht/numeric/eulerian_grid_ops/poisson_solver_3d/scipy_fft_3d.py Formatting cleanup.
sopht/numeric/eulerian_grid_ops/poisson_solver_3d/FFTPyFFTW3D.py Formatting cleanup.
sopht/numeric/eulerian_grid_ops/poisson_solver_3d/FastDiagPoissonSolver3D.py Typing import cleanup + formatting improvements.
sopht/numeric/eulerian_grid_ops/poisson_solver_3d/init.py Removes re-export surface (keeps docstring only).
sopht/numeric/eulerian_grid_ops/poisson_solver_2d/UnboundedPoissonSolverPYFFTW2D.py Formatting cleanup + TODO comment normalization.
sopht/numeric/eulerian_grid_ops/poisson_solver_2d/scipy_fft_2d.py Formatting cleanup.
sopht/numeric/eulerian_grid_ops/poisson_solver_2d/FFTPyFFTW2D.py Formatting cleanup.
sopht/numeric/eulerian_grid_ops/poisson_solver_2d/FastDiagPoissonSolver2D.py Typing import cleanup + formatting improvements.
sopht/numeric/eulerian_grid_ops/poisson_solver_2d/init.py Removes re-export surface (keeps docstring only).
scripts/cluster/submit.py Formatting-only whitespace tweak.
examples/ruff.toml Ruff config for examples (extends root; ignores docstyle/namespace/print rules).
examples/3d_examples/PointSourceAdvectAndDiffuseCase/point_source_helpers.py Formatting-only cleanup.
examples/3d_examples/PointSourceAdvectAndDiffuseCase/point_source_advection_diffusion.py Uses f-strings for filenames/progress; import ordering.
examples/3d_examples/OctopusSwimming/oscillation_activation_functions.py Signature cleanup (drops unused system arg) + formatting.
examples/3d_examples/OctopusSwimming/octopus_initializer_functions.py Formatting-only cleanup.
examples/3d_examples/OctopusArmCase3D/arm_functions_3d.py Signature cleanup (drops unused system arg) + formatting.
examples/3d_examples/HillSphericalVortexCase/hill_sphere_vortex_helpers.py Formatting cleanup + minor variable unused cleanup.
examples/3d_examples/HillSphericalVortexCase/hill_sphere_vortex_case.py Adds noqa for private member access; formatting cleanup.
examples/3d_examples/FlowThroughPipeCase/flow_through_circular_pipe_case.py Formatting cleanup + f-string filenames/progress.
examples/3d_examples/FlowPastSphereCase/flow_past_sphere_case.py Formatting cleanup + f-string filenames/progress.
examples/3d_examples/FishSwimmingCase/elastic_fish_utils/fish_geometry.py Replaces assert with explicit ValueError; formatting cleanup.
examples/3d_examples/FishSwimmingCase/elastic_fish_utils/fish_connection.py super() modernization + override usage + formatting.
examples/3d_examples/FishSwimmingCase/elastic_fish_utils/carling_fish_bc.py Import order cleanup.
examples/2d_examples/OctopusArmCase/run_coupling_tapered_arm_and_cylinder.py Formatting cleanup + f-string filenames/progress; drops unused vars.
examples/2d_examples/OctopusArmCase/arm_functions_2d.py Signature cleanup (drops unused system arg) + formatting.
examples/2d_examples/LidDrivenCavityCase/lid_driven_cavity_grid.py Formatting cleanup; simplifies assignments; removes redundant pass statements.
examples/2d_examples/LidDrivenCavityCase/lid_driven_cavity_case.py Formatting cleanup + f-string filenames/progress.
examples/2d_examples/LambOseenVortexCase/lamb_oseen_vortex.py Formatting cleanup + f-string filenames/progress.
examples/2d_examples/LambOseenVortexCase/lamb_oseen_helpers.py Formatting cleanup.
examples/2d_examples/ImmersedFlexiblePendulumCases/immersed_flexible_pendulum_with_rigid_cylinder.py Formatting cleanup + f-string filenames/progress.
examples/2d_examples/ImmersedContinuumSnakeCase/immersed_continuum_snake.py Formatting cleanup + minor step loop simplification.
examples/2d_examples/FlowPastRodCase/flow_past_rod.py Formatting cleanup + f-string filenames/progress.
examples/2d_examples/FlowPastCylinderCase/flow_past_cylinder.py Import order cleanup + f-string filenames/progress.
examples/2d_examples/FishSwimmingCase/elastic_fish_utils/fish_geometry_2d.py Replaces assert with explicit ValueError; formatting cleanup.
examples/2d_examples/FishSwimmingCase/elastic_fish_utils/fish_connection_2d.py super() modernization + override usage + formatting.
examples/2d_examples/FishSwimmingCase/elastic_fish_utils/carling_fish_bc_2d.py Import order cleanup.
examples/2d_examples/FishSwimmingCase/elastic_fish_swimming_case_2d.py Switches to new 2D flow factory; formatting cleanup + f-string filenames/progress.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sopht/simulator/flow/flow_simulators_2d.py
Comment thread sopht/simulator/flow/flow_simulators_3d.py
Comment thread sopht/simulator/flow/__init__.py
Comment thread sopht/simulator/__init__.py Outdated
Comment thread sopht/utils/post_process.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 171 out of 173 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sopht/simulator/immersed_body/flow_forces.py
Comment thread tests/test_simulator/test_immersed_body/test_immersed_body_forcing_grid.py Outdated
Comment thread tests/test_simulator/test_immersed_body/rigid_body/test_derived_rigid_bodies.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 171 out of 173 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sopht/utils/__init__.py

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 170 out of 172 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sy-cui
sy-cui merged commit ffd5354 into main Apr 3, 2026
8 checks passed
@sy-cui
sy-cui deleted the format/lint branch April 3, 2026 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

prio:low Low priority refactor Refactor existing codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants