MAINT: Modernize Optional/Union typing to PEP 604 syntax#2109
Open
romanlutz wants to merge 3 commits into
Open
MAINT: Modernize Optional/Union typing to PEP 604 syntax#2109romanlutz wants to merge 3 commits into
romanlutz wants to merge 3 commits into
Conversation
Replace Optional[T] with T | None and Union[A, B] with A | B across priority
files, drop now-unused Optional/Union imports, and update docstring type
references to the modern form. TYPE_CHECKING forward-ref unions use the
whole-string form ("X | None") so annotations stay valid at runtime on
Python 3.10+ (these modules have no `from __future__ import annotations`).
No behavior changes.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…notations Replace quoted forward-ref union strings (e.g. "X | None") with unquoted PEP 604 annotations by adding `from __future__ import annotations` to the eight affected modules. Annotation-only imports are moved into TYPE_CHECKING blocks to satisfy the repo's flake8-type-checking (TCH) rules; runtime-needed imports stay at module level. No behavior changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Phase 1 of a typing-syntax cleanup. The codebase mixes legacy
typing.Optional/typing.Unionwith the modern PEP 604X | None/A | Bsyntax that our style guide requires (and that ruff'sFArule already enforces). This PR modernizes a first batch of 10 priority files so they are consistent with that convention. No behavior changes.What changed:
Optional[T]withT | NoneandUnion[A, B]withA | Bin annotations and in docstring type references.from __future__ import annotationsto the 8 files whose annotations needed it, so the unions can be written unquoted (X | None) rather than as quoted forward-ref strings. This avoids the runtimeTypeErroryou get from evaluating"X" | Nonewhen a referenced name is only imported underTYPE_CHECKING.TYPE_CHECKINGblocks to satisfy the repo'sflake8-type-checking(TCH) rules, keeping runtime-needed imports at module level. This matches the pattern already used by the ~35 existing future-annotations modules.memory/storage/storage.py,memory/storage/serializers.py) only needed docstring type-reference updates, so they get no import or annotation changes.Worth noting: two candidate files were intentionally left untouched because they use
Unionat runtime (backend/services/converter_service.pydoesorigin is Unionchecks;scenario/core/attack_technique_factory.pydoes the same), so converting them would change behavior. Verified that the introspection consumers of these annotations (brick_contract.enforce_keyword_only_init, the converter-service catalog, and the factory'sget_type_hintscall) are unaffected by the switch to deferred annotations.Tests and Documentation
No new tests are needed since this is a no-behavior-change typing refactor. Validated with the existing tooling:
ruff checkandruff format --checkpass on all touched files.ty checkpasses (confirms every unquoted forward-ref name resolves via itsTYPE_CHECKINGimport).Unionmodules.main: all passing.No documentation (JupyText) changes are required for this change.