Use `from __future__ import annotations` in every module, enforced by ruff
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 68/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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).
- Lingua principale
- Python
- Stelle
- 2.5k
- Fork
- 770
- Merge medio
- 2g 6h
- PR unite (30g)
- 10
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di crossbario/autobahn-python
-
CI-CD enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
crossbario/autobahn-python#1945 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug CI-CD
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
crossbario/autobahn-python#1932 ·
I maintainer di solito rispondono entro 1 giorno
-
CI-CD enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
crossbario/autobahn-python#1829 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
crossbario/autobahn-python#1963 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 52/100
crossbario/autobahn-python#1958 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di crossbario/autobahn-python
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
MystenLabs/MemWal#1163 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
infertopics leaves new nodes without a topic when untopiced neighbours outnumber topiced onesForse già presa @moneebullah25 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
FinanceFlash/unvibecode#218 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
NVIDIA/earth2studio#1241 ·
I maintainer di solito rispondono entro 3 giorni