Skip to content

Commit

Permalink
ruff bugbear
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzm committed Feb 16, 2025
1 parent d251bf9 commit e174bb9
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 26 deletions.
2 changes: 1 addition & 1 deletion doc/misc/examples/obsolete/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def find_chunk_files(input_dir, prefix):
"""
pat = re.compile("^" + re.escape(prefix) + r"(?:(?:Self|Full)Overlap)?_(\d+).csv")
chunks = {}
for root, dirs, files in os.walk(input_dir):
for root, _, files in os.walk(input_dir):
for f in files:
if pat.match(f) is not None:
chunk_id = chunk_id_from_path(f)
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ extend-exclude = [
"extern/*",
"doc/conf.py",
]
output-format = "concise"

[tool.ruff.lint]
select = [
"B", # flake8-bugbear
"E", # pycodestyle
"F", # pyflakes
"N", # pep8-naming
"W", # pycodestyle
"UP", # pyupgrade
"RUF", # Ruff-specific
"UP", # pyupgrade
"W", # pycodestyle
]
ignore = [
]
2 changes: 1 addition & 1 deletion src/admin/python/lsst/qserv/admin/cli/render_targs.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def render_targs(targs: Targs) -> Targs:
rendered[k] = r
changed = True
except jinja2.exceptions.UndefinedError as e:
raise UnresolvableTemplateError(f"Missing template value: {e!s}")
raise UnresolvableTemplateError("Missing template value") from e
if not changed:
break
if any([isinstance(v, str) and "{{" in v for v in rendered.values()]):
Expand Down
2 changes: 1 addition & 1 deletion src/admin/python/lsst/qserv/admin/cli/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ def _run(
args = shlex.split(cmd)
result = subprocess.run(args, env=env, cwd="/home/qserv")
if check_returncode:
result.check_returncode
result.check_returncode()
return result.returncode


Expand Down
2 changes: 1 addition & 1 deletion src/admin/python/lsst/qserv/admin/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def yaml_presets(ctx: click.Context, param: click.core.Option, value: str) -> No
try:
overrides = _read_yaml_presets(value, cmd_name)
except Exception as e:
raise click.BadOptionUsage(cmd_name, f"Error reading overrides file: {e}", ctx)
raise click.BadOptionUsage(cmd_name, "Error reading overrides file", ctx) from e
# Override the defaults for this subcommand
ctx.default_map.update(overrides)

Expand Down
4 changes: 2 additions & 2 deletions src/admin/python/lsst/qserv/admin/itest.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ def run_detached(self, connection: str, qserv: bool, database: str) -> None:
try:
query_id = int(data.split()[0])
_log.debug("SQLCmd.execute query ID = %s", query_id)
except Exception:
raise RuntimeError(f"Failed to read query ID from SUBMIT: {data}")
except Exception as e:
raise RuntimeError(f"Failed to read query ID from SUBMIT: {data}") from e
# wait until query completes
args = [
"mysql",
Expand Down
4 changes: 2 additions & 2 deletions src/admin/python/lsst/qserv/admin/itest_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def do_ingest_database() -> None:
on_backoff=on_backoff(log=_log),
max_time=max_backoff_sec,
)
def do_ingest_table_config() -> None:
def do_ingest_table_config(table: LoadTable = table) -> None:
repl.ingest_table_config(table.ingest_config)

do_ingest_table_config()
Expand Down Expand Up @@ -495,7 +495,7 @@ def _get_cases(cases: list[str] | None, test_cases_data: list[dict[Any, Any]]) -
try:
cases_data = [db_data[case] for case in cases]
except KeyError as e:
raise RuntimeError(f"{e.args[0]} is not in {test_cases_data}")
raise RuntimeError(f"{e.args[0]} is not in {test_cases_data}") from None
else:
cases_data = test_cases_data
return cases_data
Expand Down
4 changes: 2 additions & 2 deletions src/admin/python/lsst/qserv/admin/schema_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def current_version(self) -> int | None:
result = cursor.fetchone()
try:
count = result[0]
except Exception:
raise RuntimeError(f"Could not extract version from query result: {result}.")
except Exception as e:
raise RuntimeError(f"Could not extract version from query result: {result}.") from e
return Uninitialized if count == 0 else 0

def apply_migrations(self, migrations: Sequence[Migration]) -> int:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_missing_value(self):
"""Test for failure when a template value is not provided."""
with self.assertRaises(UnresolvableTemplateError) as r:
render_targs({"a": "{{b}}"})
self.assertIn("Missing template value:", str(r.exception))
self.assertIn("Missing template value", str(r.exception))

def test_list(self):
"""Verify that lists can be used as values and manipulated by the template."""
Expand Down
2 changes: 1 addition & 1 deletion src/admin/python/lsst/qserv/admin/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_split(self):

def test_value_random_uniform(self):
gen = config._ValueRandomUniform(1.0, 42.0)
for i in range(100):
for _ in range(100):
val = gen()
self.assertGreaterEqual(val, 1.0)
self.assertLessEqual(val, 42.0)
Expand Down
20 changes: 15 additions & 5 deletions src/admin/python/lsst/qserv/testing/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class Monitor(ABC):
"""Interface definition for monitoring."""

@abstractmethod
def add_metrics(self, name, tags={}, _ts=None, **kw):
def add_metrics(self, name, tags=None, _ts=None, **kw):
"""Add more metrics to the monitor.
Parameters
----------
name : `str`
Metric name.
tags : `dict` [`str`, `Any`]
tags : `dict` [`str`, `Any`] | None
Dictionary with tags and tag values for the new measurement, tag
is a name, it's better to have tag names in the form of regular
identifiers. Tag value can be a string or an integer number.
Expand Down Expand Up @@ -61,8 +61,12 @@ def __init__(self, logger, level=logging.INFO, prefix="monitor", tags=None):
self._prefix = prefix
self._tags = tags

def add_metrics(self, name, tags={}, _ts=None, **kw):
def add_metrics(self, name, tags=None, _ts=None, **kw):
# Docstring inherited from Monitor.

if tags is None:
tags = {}

if _ts is None:
_ts = int(round(time.time() * 1e6))

Expand Down Expand Up @@ -119,8 +123,12 @@ def __init__(self, path, period_sec=None, dbname=None, tags=None):

self._open()

def add_metrics(self, name, tags={}, _ts=None, **kw):
def add_metrics(self, name, tags=None, _ts=None, **kw):
# Docstring inherited from Monitor.

if tags is None:
tags = {}

now = time.time()

# rollover to a new file if needed
Expand Down Expand Up @@ -209,8 +217,10 @@ def __init__(self, queue, buffer_size=100):
self._buffer_size = buffer_size
self._buffer = []

def add_metrics(self, name, tags={}, _ts=None, **kw):
def add_metrics(self, name, tags=None, _ts=None, **kw):
# Docstring inherited from Monitor.
if tags is None:
tags = {}
if _ts is None:
_ts = int(round(time.time() * 1e6))
self._buffer.append((name, tags, _ts, kw))
Expand Down
5 changes: 4 additions & 1 deletion src/schema/python/smig.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _load_migration_mgr(
mod_name: str,
connection: str,
scripts_dir: str,
mig_mgr_args: MigMgrArgs = dict(),
mig_mgr_args: MigMgrArgs | None = None,
) -> SchemaMigMgr:
"""Dynamic loading of the migration manager based on module name.
Expand All @@ -66,6 +66,9 @@ def _load_migration_mgr(
Exception is raised for any error.
"""

if mig_mgr_args is None:
mig_mgr_args = {}

# load module "lsst.qserv.<module>.schema_migration"
try:
mod_instance = importlib.import_module("lsst.qserv." + mod_name + "." + _mig_module_name)
Expand Down
12 changes: 6 additions & 6 deletions src/tests/MySqlUdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def testang_sep(self):
self._ang_sep(None, 0.0, 0.0, 0.0, d)
for d in (0.0, 90.0, -90.0):
self._ang_sep(0.0, 0.0, d, 0.0, d)
for i in range(100):
for _ in range(100):
args = [
random.uniform(0.0, 360.0),
random.uniform(-90.0, 90.0),
Expand Down Expand Up @@ -162,11 +162,11 @@ def test_pt_in_sph_circle(self):
self._pt_in_sph_circle(None, 0.0, 0.0, 0.0, d, 0.0)
for r in (-1.0, 181.0):
self._pt_in_sph_circle(None, 0.0, 0.0, 0.0, 0.0, r)
for i in range(10):
for _i in range(10):
ra_cen = random.uniform(0.0, 360.0)
dec_cen = random.uniform(-90.0, 90.0)
radius = random.uniform(0.0001, 10.0)
for j in range(100):
for _j in range(100):
delta = radius / math.cos(math.radians(dec_cen))
ra = random.uniform(ra_cen - delta, ra_cen + delta)
dec = random.uniform(max(dec_cen - radius, -90.0), min(dec_cen + radius, 90.0))
Expand Down Expand Up @@ -195,13 +195,13 @@ def testpt_in_sph_ellipse(self):
self._pt_in_sph_ellipse(None, 0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 0.0)
self._pt_in_sph_ellipse(None, 0.0, 0.0, 0.0, 0.0, 2.0, -1.0, 0.0)
self._pt_in_sph_ellipse(None, 0.0, 0.0, 0.0, 0.0, 36001.0, 1.0, 0.0)
for i in range(10):
for _i in range(10):
ra_cen = random.uniform(0.0, 360.0)
dec_cen = random.uniform(-90.0, 90.0)
smaa = random.uniform(0.0001, 36000.0)
smia = random.uniform(0.00001, smaa)
ang = random.uniform(-180.0, 180.0)
for j in range(100):
for _j in range(100):
smaa_deg = smaa / 3600.0
delta = smaa_deg / math.cos(math.radians(dec_cen))
ra = random.uniform(ra_cen - delta, ra_cen + delta)
Expand Down Expand Up @@ -253,7 +253,7 @@ def test_pt_in_sph_poly(self):
]
for t in tris:
spec = flatten(t)
for i in range(100):
for _ in range(100):
ra = random.uniform(0.0, 360.0)
dec = random.uniform(-90.0, 90.0)
if (t[2][1] > 0 and (dec < 0.0 or ra < t[0][0] or ra > t[1][0])) or (
Expand Down

0 comments on commit e174bb9

Please sign in to comment.