Use `from __future__ import annotations` in every module, enforced by ruff
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 68/100
調査の方向性
Start by reading the existing Ruff configuration and the exclusions for generated or vendored code. Run ruff check src/autobahn --select I002 --fix, format only the touched files, and review the resulting changes. Done means Ruff enforces the import and the Twisted, asyncio, import-smoke, and documentation-build checks in the issue pass.
索引モデルが issue の本文から書いたものです。
説明
Why
Twice now, a type annotation that is fine for type checkers broke import autobahn... at runtime
on CPython 3.11–3.13, and both times it shipped:
- #1878: a string forward reference combined with
| None("ISecurityModule" | None) raised
TypeErrorat class-definition time inautobahn.wamp.cryptosign. - #1955:
ITransport | IProcessTransportraisedTypeErrorinautobahn.twisted.util/.wamp,
because zope.interface'sInterfaceClasssupports|only from 6.2 on (fixed by #1959).
Both happen because, before Python 3.14, annotations on module-level functions and classes are
evaluated eagerly when the def / class statement runs. Any expression in an annotation that
does not work at runtime on that Python, with those dependency versions, becomes an import error.
from __future__ import annotations (PEP 563) makes every annotation in the module a string that is
never evaluated at import time, which removes this whole class of failure rather than one instance.
#1955 already named this as the complementary measure to the fix.
Current state (master ca1e60c7)
- 7 modules under
src/autobahnhave the import; #1959 adds 2 more. ruff check src/autobahn --select I002withrequired-imports = ["from __future__ import annotations"]
reports 91 files without it (39 of them test modules). Generated and vendored code is already
excluded by the existing ruff config (src/autobahn/wamp/gen/*,src/autobahn/flatbuffers/*,
src/autobahn/wamp/message_fbs.py).
Change
-
Enforce it, so it stays consistent without anyone remembering:
[tool.ruff.lint] extend-select = ["I002"] [tool.ruff.lint.isort] required-imports = ["from __future__ import annotations"] -
Apply it once, mechanically:
ruff check src/autobahn --select I002 --fix, thenruff format
on the touched files. I002 inserts the import without the blank line after it, which the
formatter adds.
Is it safe?
Trial on a scratch copy of master, CPython 3.14: the import inserted into all 91 files, then both
suites run:
just test-twisted cpy314:PASSED (skips=32, successes=325)just test-asyncio cpy314:275 passed, 6 skippedtest_import_all.py: all passed
Why it should be safe in general:
- The only place autobahn reads annotations at runtime is the
check_typestype check
(ApplicationSession.type_checkinautobahn/wamp/protocol.py). It reads the annotations of the
user's registered functions, not of autobahn's own modules, so this change does not affect
it. (Related, separate issue below.) - No name becomes unavailable: every name used in an annotation today is imported at runtime,
because the annotation is currently evaluated. Code that later callstyping.get_type_hints()
on autobahn classes resolves the strings against the same module globals. - On Python 3.14, annotation evaluation is deferred natively (PEP 649 / PEP 749) and the
__future__import keeps working. When autobahn's minimum becomes 3.14, the import can be
dropped again; the ruff setting then goes with it.
Also to check in the PR: the documentation build (Sphinx autodoc renders the string annotations).
Out of scope
- The pre-existing
ruff formatdrift: with the current floating ruff (0.16.10),
ruff format --check src/autobahnalready reports 42 files on master. That is a separate,
tool-drift matter; this change should only format the lines it touches. - Related latent bug, for its own issue:
type_checkusesfunc.__annotations__directly and
compares withisinstance(value, annotation). In a user module that itself uses
from __future__ import annotations(or any string annotation), those are strings, and the check
fails. It should resolve them, e.g. viatyping.get_type_hints(func)or
inspect.get_annotations(func, eval_str=True).
Acceptance
- Every module under
src/autobahn(except generated/vendored code) starts with
from __future__ import annotations. - ruff enforces it (
I002), so a new module without it failsjust check-format. - Twisted and asyncio test suites, the import smoke tests (including the lowest-direct job from
#1959) and the docs build are green.
Note: This issue was drafted with AI assistance (Claude Code).
- 主要言語
- Python
- スター
- 2.5k
- フォーク
- 766
- 平均マージ
- 12時間 40分
- マージ済み PR(30日)
- 20
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
crossbario/autobahn-python のほかの issue
-
CI-CD enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
crossbario/autobahn-python#1945 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug CI-CD
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
crossbario/autobahn-python#1932 ·
メンテナーはふだん 1 日以内に返信
-
CI-CD enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
crossbario/autobahn-python#1829 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug CI-CD
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
crossbario/autobahn-python#2013 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
crossbario/autobahn-python#1963 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
crossbario/autobahn-python の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
microsoft/markitdown#2820 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
yt-project/yt#5624 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
NousResearch/hermes-agent#148376 ·
メンテナーはふだん 1 日以内に返信