From f03ad4e7d8476389626fdd5698707ce8c4333ff8 Mon Sep 17 00:00:00 2001 From: Leif Denby Date: Fri, 16 Aug 2024 15:43:43 +0200 Subject: [PATCH] fix linting issues --- .flake8 | 6 ++++++ .pre-commit-config.yaml | 4 ++-- lagtraj/domain/sources/era5/load.py | 4 ++-- lagtraj/input_definitions/__init__.py | 8 +++++--- lagtraj/trajectory/create.py | 2 +- lagtraj/utils/xarray.py | 6 +++--- setup.cfg | 4 ++-- tests/test_cli.py | 2 +- 8 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 .flake8 diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..1b7589d3 --- /dev/null +++ b/.flake8 @@ -0,0 +1,6 @@ +[flake8] +max-line-length = 88 +extend-ignore = E203, E713, W604, E231 +select = C,E,F,W,B,B950 +ignore = E203, E501, W503 +per-file-ignores = __init__.py:F401 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6cb3447e..6bfaf086 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,10 +17,10 @@ repos: hooks: - id: black - repo: https://github.com/keewis/blackdoc - rev: v0.3.4 + rev: v0.3.9 hooks: - id: blackdoc - repo: https://github.com/PyCQA/flake8 - rev: 5.0.4 + rev: 6.1.0 hooks: - id: flake8 diff --git a/lagtraj/domain/sources/era5/load.py b/lagtraj/domain/sources/era5/load.py index b51d50bf..1d3c46b3 100644 --- a/lagtraj/domain/sources/era5/load.py +++ b/lagtraj/domain/sources/era5/load.py @@ -165,7 +165,7 @@ def _extra_var(self, v): return da_combined def __getitem__(self, item): - requested_vars = type(item) == set and item or set(item) + requested_vars = set(item) available_vars = self._selected_vars or self.data_vars missing_vars = requested_vars.difference(available_vars) if len(missing_vars) > 0: @@ -292,7 +292,7 @@ def interp(self, kwargs, method="linear", **interp_to): if np.array(interp_to[d]) in d_vals_array: slices[d] = interp_to[d] continue - if type(interp_to[d]) == xr.core.dataarray.DataArray: + if isinstance(interp_to[d], xr.core.dataarray.DataArray): d_interp_val = interp_to[d].values else: d_interp_val = interp_to[d] diff --git a/lagtraj/input_definitions/__init__.py b/lagtraj/input_definitions/__init__.py index 2e81894e..98cbd2cb 100644 --- a/lagtraj/input_definitions/__init__.py +++ b/lagtraj/input_definitions/__init__.py @@ -58,7 +58,7 @@ def _check_field(f_name, f_option): # with the fields `requires` and `choices` can be provided. `requires` # indicate other validations that must pass and choices are the valid # choices if they pass - if type(f_option) == dict: + if isinstance(f_option, dict): if "requires" in f_option and "choices" in f_option: requirements = f_option["requires"] satisfied_requirements = {} @@ -122,7 +122,9 @@ def _check_field(f_name, f_option): # here we check whether the parameter was prescribed to have a specific # type and in that case whether the value provided has the correct type - if type(f_option) == type and type(input_params[f_name]) != f_option: + if isinstance(f_option, type) and not isinstance( + input_params[f_name], f_option + ): raise InvalidInputDefinition( "Field `{}` should have type {}, but has type `{}`".format( f_name, f_option, type(input_params[f_name]) @@ -138,7 +140,7 @@ def _check_field(f_name, f_option): # choices a given as a list or tuple, so we call `_check_field` # recursively to see if one of the options fit - if type(f_option) == list or type(f_option) == tuple: + if isinstance(f_option, list) or isinstance(f_option, tuple): f_options = f_option exceptions = [] new_val = None diff --git a/lagtraj/trajectory/create.py b/lagtraj/trajectory/create.py index 6349c8f4..938ae9bd 100644 --- a/lagtraj/trajectory/create.py +++ b/lagtraj/trajectory/create.py @@ -73,7 +73,7 @@ def main(data_path, trajectory_name): da_times = _get_times_from_domain( trajectory_definition=traj_definition, root_data_path=data_path ) - elif type(traj_definition.timestep) == datetime.timedelta: + elif isinstance(traj_definition.timestep, datetime.timedelta): da_times = _build_times_dataarray( origin=traj_definition.origin, duration=traj_definition.duration, diff --git a/lagtraj/utils/xarray.py b/lagtraj/utils/xarray.py index dcfb800c..af74c7e9 100644 --- a/lagtraj/utils/xarray.py +++ b/lagtraj/utils/xarray.py @@ -14,7 +14,7 @@ def create_attributes_dictionary(*args, **kwargs): """ def _serialize_item(item, prefix=""): - if type(item) == str: + if isinstance(item, str): yield (prefix, item) elif isinstance(item, float) or isinstance(item, int): yield (prefix, str(item)) @@ -24,7 +24,7 @@ def _serialize_item(item, prefix=""): yield (prefix, item.isoformat()) else: sub_items = [] - if type(item) == dict: + if isinstance(item, dict): sub_items = item.items() elif ( isinstance(item, xr.Dataset) @@ -34,7 +34,7 @@ def _serialize_item(item, prefix=""): sub_items = item.attrs.items() # can use isinstance because namedtuple is a kind of tuple and we # want to save the tuple keys in that case - elif isinstance(item, list) or type(item) == tuple: + elif isinstance(item, list) or isinstance(item, tuple): sub_items = zip(range(len(item)), item) else: # collections.named_tuple has a `_asdict` method to turn it into a dictionary diff --git a/setup.cfg b/setup.cfg index d53dff11..2ad7bd2a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -42,9 +42,9 @@ dev = [flake8] max-line-length = 88 -extend-ignore = E203 +extend-ignore = E203, E713, E501, E701 select = C,E,F,W,B,B950 -ignore = E203, E501, W503 +ignore = E203, E501, W503, W604 [isort] profile=black diff --git a/tests/test_cli.py b/tests/test_cli.py index 5b59a568..d0b5ba61 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -87,7 +87,7 @@ def _parse_readme_cli_commands(): lines = [line.replace("$>", "").strip() for line in lines] - cli_commands = list(filter(lambda l: "python -m lagtraj" in l, lines)) + cli_commands = list(filter(lambda line: "python -m lagtraj" in line, lines)) return cli_commands