Skip to content

Commit

Permalink
refactor: Change linting to Ruff (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro authored Nov 18, 2024
1 parent 71e8e47 commit 6c28e65
Show file tree
Hide file tree
Showing 26 changed files with 98 additions and 128 deletions.
36 changes: 0 additions & 36 deletions .flake8

This file was deleted.

33 changes: 17 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
default_stages: [pre-commit]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: check-toml
- id: detect-private-key
- id: end-of-file-fixer
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: check-toml
- id: detect-private-key
- id: end-of-file-fixer
exclude: (\.min\.js$|\.svg$)
- id: trailing-whitespace
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.3
hooks:
- id: flake8
- repo: https://github.com/hoxbro/clean_notebook
rev: v0.1.10
hooks:
- id: clean-notebook
- id: ruff
exclude: \.ipynb
- repo: https://github.com/hoxbro/clean_notebook
rev: v0.1.15
hooks:
- id: clean-notebook
6 changes: 3 additions & 3 deletions numbergen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import param


from param import __version__ # noqa: API import
from param import __version__

class TimeAware(param.Parameterized):
"""
Expand Down Expand Up @@ -763,5 +763,5 @@ def __call__(self):
else: return val


_public = list({_k for _k,_v in locals().items() if isinstance(_v,type) and issubclass(_v,NumberGenerator)})
__all__ = _public
_public = {_k for _k,_v in locals().items() if isinstance(_v,type) and issubclass(_v,NumberGenerator)}
__all__ = ["__version__", *_public]
4 changes: 2 additions & 2 deletions param/ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def param_docstrings(self, info, max_col_len=100, only_changed=False):
contents = []
displayed_params = []
for name in self.sort_by_precedence(params):
if only_changed and not (name in changed):
if only_changed and name not in changed:
continue
displayed_params.append((name, params[name]))

Expand Down Expand Up @@ -155,7 +155,7 @@ def _build_table(self, info, order, max_col_len=40, only_changed=False):
ordering = self.sort_by_precedence(params)
for name in ordering:
p = params[name]
if only_changed and not (name in changed):
if only_changed and name not in changed:
continue

constant = 'C' if p.constant else 'V'
Expand Down
25 changes: 7 additions & 18 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
import warnings
from inspect import getfullargspec

# Allow this file to be used standalone if desired, albeit without JSON serialization
try:
from . import serializer
except ImportError:
serializer = None

from collections import defaultdict, namedtuple, OrderedDict
from functools import partial, wraps, reduce
from html import escape
Expand All @@ -39,6 +33,7 @@
from contextlib import contextmanager
from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL

from . import serializer
from ._utils import (
DEFAULT_SIGNATURE,
ParamDeprecationWarning as _ParamDeprecationWarning,
Expand Down Expand Up @@ -67,7 +62,7 @@
try:
from .ipython import ParamPager, ipython_async_executor as async_executor
param_pager = ParamPager(metaclass=True) # Generates param description
except ImportError:
except ModuleNotFoundError:
from ._utils import async_executor
else:
from ._utils import async_executor
Expand Down Expand Up @@ -340,8 +335,6 @@ def edit_constant(parameterized):
p.constant = False
try:
yield
except:
raise
finally:
for (p, const) in zip(params, constants):
p.constant = const
Expand All @@ -359,8 +352,6 @@ def discard_events(parameterized):
list(parameterized.param._events))
try:
yield
except:
raise
finally:
parameterized.param._BATCH_WATCH = batch_watch
parameterized.param._state_watchers = watchers
Expand Down Expand Up @@ -1311,8 +1302,6 @@ def deserialize(cls, value):
return value

def schema(self, safe=False, subset=None, mode='json'):
if serializer is None:
raise ImportError('Cannot import serializer.py needed to generate schema')
if mode not in self._serializers:
raise KeyError(f'Mode {mode!r} not in available serialization formats {list(self._serializers.keys())!r}')
return self._serializers[mode].param_schema(self.__class__.__name__, self,
Expand Down Expand Up @@ -1747,7 +1736,7 @@ def is_equal(cls, obj1, obj2):

@classmethod
def compare_iterator(cls, obj1, obj2):
if type(obj1) != type(obj2) or len(obj1) != len(obj2):
if type(obj1) is not type(obj2) or len(obj1) != len(obj2):
return False
for o1, o2 in zip(obj1, obj2):
if not cls.is_equal(o1, o2):
Expand All @@ -1756,7 +1745,7 @@ def compare_iterator(cls, obj1, obj2):

@classmethod
def compare_mapping(cls, obj1, obj2):
if type(obj1) != type(obj2) or len(obj1) != len(obj2): return False
if type(obj1) is not type(obj2) or len(obj1) != len(obj2): return False
for k in obj1:
if k in obj2:
if not cls.is_equal(obj1[k], obj2[k]):
Expand Down Expand Up @@ -2354,7 +2343,7 @@ def _update(self_, arg=Undefined, /, **kwargs):
raise ValueError(f"{k!r} is not a parameter of {self_.cls.__name__}")
try:
setattr(self_or_cls, k, v)
except:
except Exception:
self_._BATCH_WATCH = False
raise

Expand Down Expand Up @@ -2386,7 +2375,7 @@ def set_param(self_, *args,**kwargs):
"""
self_or_cls = self_.self_or_cls
if args:
if len(args) == 2 and not args[0] in kwargs and not kwargs:
if len(args) == 2 and args[0] not in kwargs and not kwargs:
kwargs[args[0]] = args[1]
else:
raise ValueError("Invalid positional arguments for %s.set_param" %
Expand Down Expand Up @@ -3388,7 +3377,7 @@ def __set_name(mcs, name, dict_):
"""
name_param = dict_.get("name", None)
if name_param is not None:
if not type(name_param) is String:
if type(name_param) is not String:
raise TypeError(
f"Parameterized class {name!r} cannot override "
f"the 'name' Parameter with type {type(name_param)}. "
Expand Down
4 changes: 2 additions & 2 deletions param/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,7 @@ def serialize(cls, value):
if not isinstance(v, (dt.datetime, dt.date)): # i.e np.datetime64
v = v.astype(dt.datetime)
# Separate date and datetime to deserialize to the right type.
if type(v) == dt.date:
if type(v) is dt.date:
v = v.strftime("%Y-%m-%d")
else:
v = v.strftime("%Y-%m-%dT%H:%M:%S.%f")
Expand Down Expand Up @@ -1958,7 +1958,7 @@ def _ensure_value_is_in_objects(self, val):
Subclasses can override if they support multiple items on a list,
to check each item instead.
"""
if not (val in self.objects):
if val not in self.objects:
self._objects.append(val)

def get_range(self):
Expand Down
4 changes: 2 additions & 2 deletions param/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def objectselector_schema(cls, p, safe=False):
schema = {'anyOf': allowed_types}
schema['enum'] = p.objects
return schema
except:
except Exception:
if safe is True:
msg = ('ObjectSelector cannot be guaranteed to be safe for '
'serialization due to unserializable type in objects')
Expand All @@ -267,7 +267,7 @@ def selector_schema(cls, p, safe=False):
schema = {'anyOf': allowed_types}
schema['enum'] = p.objects
return schema
except:
except Exception:
if safe is True:
msg = ('Selector cannot be guaranteed to be safe for '
'serialization due to unserializable type in objects')
Expand Down
19 changes: 9 additions & 10 deletions param/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

__author__ = 'Jean-Luc Stevens'

import os, subprocess, json
import os
import subprocess
import json

def run_cmd(args, cwd=None):
kwargs = {}
Expand Down Expand Up @@ -192,7 +194,7 @@ def git_fetch(self, cmd='git', as_string=False):
output = self._output_from_file()
if output is not None:
self._update_from_vcs(output)
except: pass
except Exception: pass
if output is None:
# glob pattern (not regexp) matching vX.Y.Z* tags
output = run_cmd([cmd, 'describe', '--long', '--match',
Expand Down Expand Up @@ -251,7 +253,7 @@ def _output_from_file(self, entry='git_describe'):
vfile = os.path.join(os.path.dirname(self.fpath), '.version')
with open(vfile) as f:
return json.loads(f.read()).get(entry, None)
except: # File may be missing if using pip + git archive
except Exception: # File may be missing if using pip + git archive
return None


Expand Down Expand Up @@ -427,7 +429,7 @@ def setup_version(cls, setup_path, reponame, archive_commit=None,

if git_describe is not None:
info['git_describe'] = git_describe
except: pass
except Exception: pass

if git_describe is None:
extracted_directory_tag = Version.extract_directory_tag(setup_path, reponame)
Expand All @@ -436,7 +438,7 @@ def setup_version(cls, setup_path, reponame, archive_commit=None,
try:
with open(os.path.join(setup_path, pkgname, '.version'), 'w') as f:
f.write(json.dumps({'extracted_directory_tag':extracted_directory_tag}))
except:
except Exception:
print('Error in setup_version: could not write .version file.')


Expand All @@ -449,7 +451,7 @@ def setup_version(cls, setup_path, reponame, archive_commit=None,
try:
with open(os.path.join(setup_path, pkgname, '.version'), 'w') as f:
f.write(json.dumps(info))
except:
except Exception:
print('Error in setup_version: could not write .version file.')

return info['version_string']
Expand Down Expand Up @@ -515,10 +517,7 @@ def get_setupcfg_version():
parse setup.cfg...but then git export-subst would not work.
"""
try:
import configparser
except ImportError:
import ConfigParser as configparser # python2 (also prevents dict-like access)
import configparser
import re
cfg = "setup.cfg"
autover_section = 'tool:autover'
Expand Down
16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,19 @@ asyncio_default_fixture_loop_scope="function"

[tool.coverage.report]
omit = ["param/version.py"]

[tool.ruff]
fix = true
include = ["*.py"]

[tool.ruff.lint]
select = ["W", "E", "F"]
ignore = [
"E402", # Module level import not at top of file
"E501", # Line too long
"E701", # Multiple statements on one line
"E712", # Comparison to true should be is
"E731", # Do not assign a lambda expression, use a def
"E741", # Ambiguous variable name
"F405", # From star imports
]
2 changes: 1 addition & 1 deletion tests/testclassselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _check_defaults(self, p):
assert p.allow_None is True
assert p.instantiate is True
assert p.is_instance is True
assert p.class_ == dict
assert p.class_ is dict

def test_defaults_class(self):
class P(param.Parameterized):
Expand Down
4 changes: 2 additions & 2 deletions tests/testcomparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

try:
import numpy as np
except ImportError:
except ModuleNotFoundError:
np = None
try:
import pandas as pd
except ImportError:
except ModuleNotFoundError:
pd = None

_now = datetime.datetime.now()
Expand Down
2 changes: 1 addition & 1 deletion tests/testdateparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

try:
import numpy as np
except:
except ModuleNotFoundError:
np = None

from .utils import check_defaults
Expand Down
2 changes: 1 addition & 1 deletion tests/testdaterangeparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

try:
import numpy as np
except:
except ModuleNotFoundError:
np = None

# Assuming tests of range parameter cover most of what's needed to
Expand Down
Loading

0 comments on commit 6c28e65

Please sign in to comment.