Skip to content

Commit d2194d5

Browse files
claudeCarreau
authored andcommitted
Harden mypy/ruff tooling and improve typing
Toolchain: - Bump the pinned linters that CI enforces: ruff 0.2.0 -> 0.15.8 and the mypy pre-commit hook 1.8.0 -> 1.20.2 (aligning the package type-check with the `mypy>=1.20` floor the test suite now requires), and switch to the non-deprecated `ruff-check` hook id (updating the hatch lint:build script to match). - Enable additional mypy error codes: truthy-iterable, possibly-undefined, redundant-self, unused-awaitable. - Drop ruff rules removed upstream (UP038, PT004, PT005); ignore PLC0415 (traitlets relies on lazy/optional imports); relax RUF059/PLW0108/B024 for tests, matching the existing per-file test allowances. Typing / correctness: - parse_notifier_name / observe / unobserve: Iterable -> Collection, so the `not names` / `All in names` checks are honest (a generator was a latent bug) and truthy-iterable is satisfied. - SingletonConfigurable.instance now returns Self instead of a bespoke TypeVar (PYI019). - _CallbackWrapper gets a __hash__ consistent with __eq__ (PLW1641). - Use `is` instead of `==` for exact trait-type comparisons (E721). - Correct/remove stale `type: ignore` codes and unused noqa directives surfaced by the newer mypy. Cleanups: - Modernize %-formatting and str.format to f-strings (UP031/UP032), across traitlets/, tests/, examples/ and docs/. - Replace dict-index iteration with .items() (PLC0206), iterable unpacking (RUF005), and lazy %-args for a logging call (G004). - Reformat with the newer ruff-format. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TBmDBHmq8vVSGig4i37KK7
1 parent 221d2d6 commit d2194d5

35 files changed

Lines changed: 285 additions & 262 deletions

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ repos:
5151
args: ["-L", "sur,nd"]
5252

5353
- repo: https://github.com/pre-commit/mirrors-mypy
54-
rev: "v1.8.0"
54+
rev: "v1.20.2"
5555
hooks:
5656
- id: mypy
5757
files: "^traitlets"
@@ -67,9 +67,9 @@ repos:
6767
- id: rst-inline-touching-normal
6868

6969
- repo: https://github.com/astral-sh/ruff-pre-commit
70-
rev: v0.2.0
70+
rev: v0.15.8
7171
hooks:
72-
- id: ruff
72+
- id: ruff-check
7373
types_or: [python, jupyter]
7474
args: ["--fix", "--show-fixes"]
7575
- id: ruff-format

docs/sphinxext/github.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Doug Hellmann
1414
* Min RK
1515
"""
16+
1617
#
1718
# Original Copyright (c) 2010 Doug Hellmann. All rights reserved.
1819
#
@@ -42,9 +43,7 @@ def make_link_node(rawtext, app, type, slug, options):
4243
if not base.endswith("/"):
4344
base += "/"
4445
except AttributeError as err:
45-
raise ValueError(
46-
"github_project_url configuration value is not set (%s)" % str(err)
47-
) from err
46+
raise ValueError(f"github_project_url configuration value is not set ({err!s})") from err
4847

4948
ref = base + type + "/" + slug + "/"
5049
set_classes(options)
@@ -79,7 +78,7 @@ def ghissue_role(name, rawtext, text, lineno, inliner, options=None, content=Non
7978
except ValueError:
8079
msg = inliner.reporter.error(
8180
"GitHub issue number must be a number greater than or equal to 1; "
82-
'"%s" is invalid.' % text,
81+
f'"{text}" is invalid.',
8382
line=lineno,
8483
)
8584
prb = inliner.problematic(rawtext, rawtext, msg)
@@ -92,7 +91,7 @@ def ghissue_role(name, rawtext, text, lineno, inliner, options=None, content=Non
9291
category = "issues"
9392
else:
9493
msg = inliner.reporter.error(
95-
'GitHub roles include "ghpull" and "ghissue", "%s" is invalid.' % name, line=lineno
94+
f'GitHub roles include "ghpull" and "ghissue", "{name}" is invalid.', line=lineno
9695
)
9796
prb = inliner.problematic(rawtext, rawtext, msg)
9897
return [prb], [msg]
@@ -149,9 +148,7 @@ def ghcommit_role(name, rawtext, text, lineno, inliner, options=None, content=No
149148
if not base.endswith("/"):
150149
base += "/"
151150
except AttributeError as err:
152-
raise ValueError(
153-
"github_project_url configuration value is not set (%s)" % str(err)
154-
) from err
151+
raise ValueError(f"github_project_url configuration value is not set ({err!s})") from err
155152

156153
ref = base + text
157154
node = nodes.reference(rawtext, text[:6], refuri=ref, **options)

examples/argcomplete_app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
If completions are not showing, you can set the environment variable ``_ARC_DEBUG=1``
6464
to assist in debugging argcomplete. This was last checked with ``argcomplete==1.12.3``.
6565
"""
66+
6667
from __future__ import annotations
6768

6869
import json

examples/docs/aliases.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# PYTHON_ARGCOMPLETE_OK
33
"""A simple example of using Application aliases, for docs"""
4+
45
from __future__ import annotations
56

67
from traitlets import Bool

examples/docs/container.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# PYTHON_ARGCOMPLETE_OK
33
"""A simple example of using container traits in Application command-line"""
4+
45
from __future__ import annotations
56

67
from traitlets import Dict, Integer, List, Unicode

examples/docs/flags.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# PYTHON_ARGCOMPLETE_OK
33
"""A simple example of using Application flags, for docs"""
4+
45
from __future__ import annotations
56

67
from traitlets import Bool

examples/docs/from_string.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# PYTHON_ARGCOMPLETE_OK
33
"""A simple example of using TraitType.from_string, for docs"""
4+
45
from __future__ import annotations
56

67
from binascii import a2b_hex

examples/docs/load_config_app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
$ ./examples/docs/load_config_app.py -c ""
1717
The school MIT has a rank of 1.
1818
"""
19+
1920
from __future__ import annotations
2021

2122
from pathlib import Path

examples/docs/multiple_apps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# PYTHON_ARGCOMPLETE_OK
33
"""A simple example of one application calling another"""
4+
45
from __future__ import annotations
56

67
from traitlets.config import Application

examples/docs/subcommands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# PYTHON_ARGCOMPLETE_OK
33
"""A simple example of using Application subcommands, for docs"""
4+
45
from __future__ import annotations
56

67
from traitlets.config import Application

0 commit comments

Comments
 (0)