Skip to content

Commit

Permalink
Merge pull request #1648 from OceanParcels/v/bugbear
Browse files Browse the repository at this point in the history
Enable Bugbear and other rules in Ruff linting
  • Loading branch information
VeckoTheGecko authored Aug 13, 2024
2 parents 7d2f37e + 3bb0d43 commit 2a876f8
Show file tree
Hide file tree
Showing 24 changed files with 84 additions and 67 deletions.
4 changes: 2 additions & 2 deletions docs/examples/example_dask_chunk_OCMs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def periodicBC(particle, fieldSet, time):
if particle.lon > 15.0:
particle_dlon -= 15.0 # noqa
if particle.lon < 0:
particle_dlon += 15.0 # noqa
particle_dlon += 15.0
if particle.lat > 60.0:
particle_dlat -= 11.0 # noqa
if particle.lat < 49.0:
particle_dlat += 11.0 # noqa
particle_dlat += 11.0

pset = parcels.ParticleSet.from_list(fieldset, ptype[mode], lon=lonp, lat=latp)
kernels = pset.Kernel(parcels.AdvectionRK4) + periodicBC
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/example_mitgcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def periodicBC(particle, fieldset, time):
if particle.lon < 0:
particle_dlon += fieldset.domain_width # noqa
elif particle.lon > fieldset.domain_width:
particle_dlon -= fieldset.domain_width # noqa
particle_dlon -= fieldset.domain_width

# Release particles 5 cells away from the Eastern boundary
pset = parcels.ParticleSet.from_line(
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/example_moving_eddies.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ def periodicBC(particle, fieldset, time):
if particle.lon < fieldset.halo_west:
particle_dlon += fieldset.halo_east - fieldset.halo_west # noqa
elif particle.lon > fieldset.halo_east:
particle_dlon -= fieldset.halo_east - fieldset.halo_west # noqa
particle_dlon -= fieldset.halo_east - fieldset.halo_west
if particle.lat < fieldset.halo_south:
particle_dlat += fieldset.halo_north - fieldset.halo_south # noqa
elif particle.lat > fieldset.halo_north:
particle_dlat -= fieldset.halo_north - fieldset.halo_south # noqa
particle_dlat -= fieldset.halo_north - fieldset.halo_south

def slowlySouthWestward(particle, fieldset, time):
particle_dlon -= 5 * particle.dt / 1e5 # noqa
Expand Down
22 changes: 11 additions & 11 deletions parcels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
__version__ = version

import parcels.rng as ParcelsRandom # noqa
from parcels.application_kernels import * # noqa
from parcels.field import * # noqa
from parcels.fieldset import * # noqa
from parcels.grid import * # noqa
from parcels.gridset import * # noqa
from parcels.interaction import * # noqa
from parcels.kernel import * # noqa
from parcels.particle import * # noqa
from parcels.particlefile import * # noqa
from parcels.particleset import * # noqa
from parcels.tools import * # noqa
from parcels.application_kernels import *
from parcels.field import *
from parcels.fieldset import *
from parcels.grid import *
from parcels.gridset import *
from parcels.interaction import *
from parcels.kernel import *
from parcels.particle import *
from parcels.particlefile import *
from parcels.particleset import *
from parcels.tools import *
2 changes: 1 addition & 1 deletion parcels/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess

try:
from parcels._version_setup import version # noqa
from parcels._version_setup import version
except ModuleNotFoundError:
try:
version = (
Expand Down
6 changes: 3 additions & 3 deletions parcels/application_kernels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .advection import * # noqa
from .advectiondiffusion import * # noqa
from .interaction import * # noqa
from .advection import *
from .advectiondiffusion import *
from .interaction import *
15 changes: 9 additions & 6 deletions parcels/compilation/codecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
except ModuleNotFoundError:
MPI = None

_tmp_dir = os.getcwd()

class Compiler_parameters:
def __init__(self):
Expand Down Expand Up @@ -79,15 +80,15 @@ def __init__(self, cppargs=None, ldargs=None, incdirs=None, libdirs=None, libs=N

Iflags = []
if isinstance(incdirs, list):
for i, dir in enumerate(incdirs):
for dir in incdirs:
Iflags.append("-I"+dir)
Lflags = []
if isinstance(libdirs, list):
for i, dir in enumerate(libdirs):
for dir in libdirs:
Lflags.append("-L"+dir)
lflags = []
if isinstance(libs, list):
for i, lib in enumerate(libs):
for lib in libs:
lflags.append("-l" + lib)

cc_env = os.getenv('CC')
Expand Down Expand Up @@ -207,7 +208,9 @@ class CCompiler:
A list of arguments to the linker (optional).
"""

def __init__(self, cc=None, cppargs=None, ldargs=None, incdirs=None, libdirs=None, libs=None, tmp_dir=os.getcwd()):
def __init__(self, cc=None, cppargs=None, ldargs=None, incdirs=None, libdirs=None, libs=None, tmp_dir=None):
if tmp_dir is None:
tmp_dir = _tmp_dir
if cppargs is None:
cppargs = []
if ldargs is None:
Expand Down Expand Up @@ -249,7 +252,7 @@ def _create_compile_process_(self, cmd, src, log):
class CCompiler_SS(CCompiler):
"""Single-stage C-compiler; used for a SINGLE source file."""

def __init__(self, cc=None, cppargs=None, ldargs=None, incdirs=None, libdirs=None, libs=None, tmp_dir=os.getcwd()):
def __init__(self, cc=None, cppargs=None, ldargs=None, incdirs=None, libdirs=None, libs=None, tmp_dir=None):
super().__init__(cc=cc, cppargs=cppargs, ldargs=ldargs, incdirs=incdirs, libdirs=libdirs, libs=libs, tmp_dir=tmp_dir)

def __str__(self):
Expand Down Expand Up @@ -282,7 +285,7 @@ class GNUCompiler_SS(CCompiler_SS):
A list of arguments to pass to the linker (optional).
"""

def __init__(self, cppargs=None, ldargs=None, incdirs=None, libdirs=None, libs=None, tmp_dir=os.getcwd()):
def __init__(self, cppargs=None, ldargs=None, incdirs=None, libdirs=None, libs=None, tmp_dir=None):
c_params = GNU_parameters(cppargs, ldargs, incdirs, libdirs, libs)
super().__init__(c_params.compiler, cppargs=c_params.cppargs, ldargs=c_params.ldargs, incdirs=c_params.incdirs, libdirs=c_params.libdirs, libs=c_params.libs, tmp_dir=tmp_dir)
self._dynlib_ext = c_params.dynlib_ext
Expand Down
4 changes: 2 additions & 2 deletions parcels/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,8 +1269,8 @@ def chunk_setup(self):
self.grid.load_chunk = np.zeros(npartitions, dtype=c_int, order='C')
# self.grid.chunk_info format: number of dimensions (without tdim); number of chunks per dimensions;
# chunksizes (the 0th dim sizes for all chunk of dim[0], then so on for next dims
self.grid.chunk_info = [[len(self.nchunks)-1], list(self.nchunks[1:]), sum(list(list(ci) for ci in chunks[1:]), [])]
self.grid.chunk_info = sum(self.grid.chunk_info, [])
self.grid.chunk_info = [[len(self.nchunks)-1], list(self.nchunks[1:]), sum(list(list(ci) for ci in chunks[1:]), [])] # noqa: RUF017 # TODO: Perhaps avoid quadratic list summation here
self.grid.chunk_info = sum(self.grid.chunk_info, []) # noqa: RUF017
self.chunk_set = True

def chunk_data(self):
Expand Down
4 changes: 2 additions & 2 deletions parcels/fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def parse_wildcards(cls, paths, filenames, var):
paths = sorted(glob(str(paths)))
if len(paths) == 0:
notfound_paths = filenames[var] if isinstance(filenames, dict) and var in filenames else filenames
raise OSError(f"FieldSet files not found for variable {var}: {str(notfound_paths)}")
raise OSError(f"FieldSet files not found for variable {var}: {notfound_paths}")
for fp in paths:
if not os.path.exists(fp):
raise OSError(f"FieldSet file not found: {fp}")
Expand Down Expand Up @@ -1168,7 +1168,7 @@ def add_periodic_halo(self, zonal=False, meridional=False, halosize=5):
"""
for grid in self.gridset.grids:
grid.add_periodic_halo(zonal, meridional, halosize)
for attr, value in iter(self.__dict__.items()):
for value in self.__dict__.values():
if isinstance(value, Field):
value.add_periodic_halo(zonal, meridional, halosize)

Expand Down
6 changes: 3 additions & 3 deletions parcels/interaction/neighborsearch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from parcels.interaction.neighborsearch.bruteforce import ( # noqa
from parcels.interaction.neighborsearch.bruteforce import (
BruteFlatNeighborSearch,
BruteSphericalNeighborSearch,
)
from parcels.interaction.neighborsearch.hashflat import HashFlatNeighborSearch
from parcels.interaction.neighborsearch.hashspherical import ( # noqa
from parcels.interaction.neighborsearch.hashspherical import (
HashSphericalNeighborSearch,
)
from parcels.interaction.neighborsearch.kdtreeflat import ( # noqa
from parcels.interaction.neighborsearch.kdtreeflat import (
KDTreeFlatNeighborSearch,
)

Expand Down
7 changes: 3 additions & 4 deletions parcels/particledata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from abc import ABC
from ctypes import POINTER, Structure
from operator import attrgetter

Expand Down Expand Up @@ -42,7 +41,7 @@ def partitionParticlesMPI_default(coords, mpi_size=1):
return mpiProcs


class ParticleData(ABC):
class ParticleData:

def __init__(self, pclass, lon, lat, depth, time, lonlatdepth_dtype, pid_orig, ngrid=1, **kwargs):
"""
Expand Down Expand Up @@ -361,7 +360,7 @@ def set_variable_write_status(self, var, write_status):
raise SyntaxError(f'Could not change the write status of {var}, because it is not a Variable name')


class ParticleDataAccessor(ABC):
class ParticleDataAccessor:
"""Wrapper that provides access to particle data, as if interacting with the particle itself.
Parameters
Expand Down Expand Up @@ -438,7 +437,7 @@ def delete(self):
self.state = StatusCode.Delete


class ParticleDataIterator(ABC):
class ParticleDataIterator:
"""Iterator for looping over the particles in the ParticleData.
Parameters
Expand Down
3 changes: 1 addition & 2 deletions parcels/particlefile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Module controlling the writing of ParticleSets to Zarr file."""
import os
from abc import ABC
from datetime import timedelta

import numpy as np
Expand All @@ -26,7 +25,7 @@ def _set_calendar(origin_calendar):
return origin_calendar


class ParticleFile(ABC):
class ParticleFile:
"""Initialise trajectory output.
Parameters
Expand Down
3 changes: 1 addition & 2 deletions parcels/particleset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import sys
from abc import ABC
from copy import copy
from datetime import date, datetime, timedelta

Expand Down Expand Up @@ -49,7 +48,7 @@ def _convert_to_reltime(time):
return False


class ParticleSet(ABC):
class ParticleSet:
"""Class for storing particle and executing kernel over them.
Please note that this currently only supports fixed size particle sets, meaning that the particle set only
Expand Down
14 changes: 7 additions & 7 deletions parcels/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .converters import * # noqa
from .exampledata_utils import * # noqa
from .global_statics import * # noqa
from .interpolation_utils import * # noqa
from .loggers import * # noqa
from .statuscodes import * # noqa
from .timer import * # noqa
from .converters import *
from .exampledata_utils import *
from .global_statics import *
from .interpolation_utils import *
from .loggers import *
from .statuscodes import *
from .timer import *
2 changes: 1 addition & 1 deletion parcels/tools/statuscodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DaskChunkingError(RuntimeError):
"""Error indicating to the user that something with setting up Dask and chunked fieldsets went wrong."""

def __init__(self, src_class_type, message):
msg = f"[{str(src_class_type)}]: {message}"
msg = f"[{src_class_type}]: {message}"
super().__init__(msg)


Expand Down
3 changes: 1 addition & 2 deletions parcels/tools/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def print_tree_sequential(self, step=0, root_time=0, parent_time=0):
if step == 0:
root_time = time
print(('(%3d%%)' % round(time/root_time*100)), end='')
for i in range(step+1):
print(' ', end='')
print(' ' * (step + 1), end='')
if step > 0:
print('(%3d%%) ' % round(time/parent_time*100), end='')
t_str = '%1.3e s' % time if root_time < 300 else datetime.timedelta(seconds=time)
Expand Down
18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,26 @@ select = [
"E", # Error
"F", # pyflakes
"I", # isort
"B", # Bugbear
# "UP", # pyupgrade
"LOG", # logging
"ICN", # import conventions
"G", # logging-format
"RUF", # ruff
]

ignore = [
# line too long (82 > 79 characters)
"E501",
# ‘from module import *’ used; unable to detect undefined names
"F403",
# Mutable class attributes should be annotated with `typing.ClassVar`
"RUF012",
# Consider `(slice(2), *block)` instead of concatenation
"RUF005",
# Prefer `next(iter(variable.items()))` over single element slice
"RUF015",


# TODO: ignore for now (requires more work). Remove ignore once fixed
# Missing docstring in public module
Expand All @@ -96,6 +108,12 @@ ignore = [
"D205",
# do not use bare except, specify exception instead
"E722",


# TODO: These bugbear issues are to be resolved
"B011", # Do not `assert False`
"B016", # Cannot raise a literal. Did you intend to return it or raise an Exception?
"B904", # Within an `except` clause, raise exceptions
]

[tool.ruff.lint.pydocstyle]
Expand Down
8 changes: 4 additions & 4 deletions tests/test_fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import pytest
import xarray as xr

from parcels import ( # noqa
from parcels import (
AdvectionRK4,
AdvectionRK4_3D,
FieldSet,
Expand Down Expand Up @@ -648,7 +648,7 @@ def test_fieldset_write(tmpdir):
fieldset.U.to_write = True

def UpdateU(particle, fieldset, time):
tmp1, tmp2 = fieldset.UV[particle] # noqa
tmp1, tmp2 = fieldset.UV[particle]
fieldset.U.data[particle.ti, particle.yi, particle.xi] += 1
fieldset.U.grid.time[0] = time

Expand Down Expand Up @@ -694,11 +694,11 @@ def periodicBoundaryConditions(particle, fieldset, time):
while particle.lon > 180.:
particle_dlon -= 360. # noqa
while particle.lon < -180.:
particle_dlon += 360. # noqa
particle_dlon += 360.
while particle.lat > 90.:
particle_dlat -= 180. # noqa
while particle.lat < -90.:
particle_dlat += 180. # noqa
particle_dlat += 180.

process = psutil.Process(os.getpid())
mem_0 = process.memory_info().rss
Expand Down
2 changes: 1 addition & 1 deletion tests/test_grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def bath_func(lon):

pset = ParticleSet.from_list(fieldset, ptype[mode], lon=[0], lat=[0], depth=[1])

for i in range(10):
for _ in range(10):
pset.execute(AdvectionRK4_3D, runtime=1000, dt=500)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def test_flat_update(test_class):
ref_instance = ref_class(inter_dist_vert=0.3, inter_dist_horiz=0.3)
test_instance = test_class(inter_dist_vert=0.3, inter_dist_horiz=0.3)

for i in range(1, n_active_mask):
for _ in range(1, n_active_mask):
positions = create_flat_positions(n_particle) + 10*np.random.rand()
active_mask = np.random.rand(n_particle) > 0.5
ref_instance.update_values(positions, active_mask)
Expand Down
Loading

0 comments on commit 2a876f8

Please sign in to comment.