Skip to content

refactor(typing): use PEP 604 syntax for Optional annotations (UP045) - #8399

Open
manshahH wants to merge 1 commit into
aws-powertools:developfrom
manshahH:refactor/pyupgrade-up045
Open

refactor(typing): use PEP 604 syntax for Optional annotations (UP045)#8399
manshahH wants to merge 1 commit into
aws-powertools:developfrom
manshahH:refactor/pyupgrade-up045

Conversation

@manshahH

@manshahH manshahH commented Aug 24, 2026

Copy link
Copy Markdown

Issue number: closes #8398

Summary

Clears the first of the four temporary pyupgrade ignores in ruff.toml, following the one rule per PR plan @leandrodamascena outlined in #8047.

Optional[X] becomes X | None across the repository, and "UP045" is removed from lint.ignore so the rule is enforced from here on.

Changes

  • 285 violations across 65 files: 24 in aws_lambda_powertools, 25 in examples, 15 in tests, plus ruff.toml
  • 283 fixed by the ruff autofix
  • 2 converted by hand
  • 63 Optional imports left unused by the conversion were removed, since F401 flags them

The two manual cases. Both are in aws_lambda_powertools/utilities/batch/types.py, where Optional[...] sits in a runtime assignment rather than an annotation:

BatchTypeModels = Optional[Union[...]]
BatchSqsTypeModel = Optional[Type[SqsRecordModel]]

Ruff correctly refuses to autofix these, because the expression is evaluated at import time and a rewrite changes the object that actually gets built. I converted them by hand and checked the result against the previous definitions:

Optional[Union[...]] == BatchTypeModels             -> True
Optional[Type[SqsRecordModel]] == BatchSqsTypeModel -> True

Union is deliberately left in place there, since it belongs to UP007. Removing it here would blur the one rule per PR split.

Docstrings. Twenty parameter descriptions and section comments inside files this PR already touches still referred to Optional[...] while the code below them had changed, so they were brought in line. Files not otherwise modified by this PR were left alone.

User experience

No user facing change. This is internal typing syntax only, and the runtime behaviour of every converted annotation and alias is unchanged.

For contributors, UP045 is now enforced, so the older Optional[X] spelling gets caught by make lint instead of being silently accepted.

Verification

  • ruff check passes with UP045 enforced
  • ruff format --check: 1310 files already formatted
  • Full suite excluding e2e: 2497 passed. Six failures are pre existing Windows only path and formatting issues that reproduce identically on a clean develop
  • mypy aws_lambda_powertools examples: same error count before and after, no new errors introduced
  • bandit security baseline and xenon complexity baseline both unaffected

Happy to carry on with UP007 and then UP035 in the same shape once this one looks right.

Acknowledgment


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Convert Optional[X] to X | None across the codebase and remove the
temporary UP045 ignore from ruff.toml, so the fix and the removal of the
ignore land together.

283 of the 285 violations were handled by the ruff autofix. The two
remaining cases in utilities/batch/types.py are runtime assignments
rather than annotations, so they were converted by hand and verified to
compare equal to their previous definitions.

Union is left in place, since it belongs to UP007.
@manshahH
manshahH requested a review from a team as a code owner August 24, 2026 12:30
@manshahH
manshahH requested a review from svozza August 24, 2026 12:30
@boring-cyborg

boring-cyborg Bot commented Aug 24, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@boring-cyborg boring-cyborg Bot added the tests label Aug 24, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Aug 24, 2026
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Maintenance: Fix UP045 ruff lint violations (Optional[X] to X | None)

1 participant