refactor(typing): use PEP 604 syntax for Optional annotations (UP045) - #8399
Conversation
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.
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #8399 +/- ##
========================================
Coverage 96.64% 96.64%
========================================
Files 296 296
Lines 14767 14767
Branches 1246 1246
========================================
Hits 14271 14271
Misses 361 361
Partials 135 135 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
leandrodamascena
left a comment
There was a problem hiding this comment.
Thanks for tackling this. I like this direction because it resolves the Ruff issue instead of adding another exception we would need to maintain as technical debt.
I found two small typos that currently fail at import time:
tests/e2e/utils/data_fetcher/logs.py:57tests/e2e/utils/data_fetcher/traces.py:109
Could you please change any | None to:
Any | NoneLowercase any is Python's built-in function, so the current annotation raises a TypeError.
…nnotations Optional[any] was tolerated because typing never validated its argument, so the lowercase any went unnoticed. Once converted to any | None the expression is evaluated at import time, and neither module has the future annotations import, so importing them raised TypeError. Also updates the matching docstring line in logs.py. These modules are never imported by the test suite, since make test runs with --ignore tests/e2e, which is why this was not caught locally.
|
Good catch, thank you. Fixed in 06b30b6. Your diagnosis is right. Both signatures are now This slipped past me because All four |
leandrodamascena
left a comment
There was a problem hiding this comment.
Thanks for fixing this and for checking the remaining converted annotations as well. I pulled the latest commit and confirmed that both modules now evaluate the annotations correctly, with Ruff checks also passing.
This fully addresses my review and resolves the Ruff rule without leaving another exception for us to maintain.
Thank you very much for the work. APPROVED!
|
Tick the box to add this pull request to the merge queue (same as
|
|
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |



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]becomesX | Noneacross the repository, and"UP045"is removed fromlint.ignoreso the rule is enforced from here on.Changes
aws_lambda_powertools, 25 inexamples, 15 intests, plusruff.tomlOptionalimports left unused by the conversion were removed, sinceF401flags themThe two manual cases. Both are in
aws_lambda_powertools/utilities/batch/types.py, whereOptional[...]sits in a runtime assignment rather than an annotation: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:
Unionis deliberately left in place there, since it belongs toUP007. 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,
UP045is now enforced, so the olderOptional[X]spelling gets caught bymake lintinstead of being silently accepted.Verification
ruff checkpasses withUP045enforcedruff format --check: 1310 files already formatteddevelopmypy aws_lambda_powertools examples: same error count before and after, no new errors introducedHappy to carry on with
UP007and thenUP035in 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.