Skip to content

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

Description

@manshahH

Why is this needed?

Bumping the ruff target to py310 turned on a set of pyupgrade rules that were silenced rather than fixed. ruff.toml still carries temporary ignores for UP006, UP007, UP035 and UP045.

In #8047 @leandrodamascena proposed clearing these one rule per PR, safest first, with each PR removing its own ignore so the fix and the removal land together. This issue covers UP045 (Optional[X] to X | None), which was listed as one of the two safest to start with.

Current numbers on develop:

  • 285 violations across 65 files (24 in aws_lambda_powertools, 25 in examples, 15 in tests)
  • 283 fixed automatically by the ruff autofix
  • 2 need manual work, both in aws_lambda_powertools/utilities/batch/types.py

The two manual cases are runtime assignments rather than annotations:

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

Ruff correctly refuses to autofix these, since rewriting them changes the object that is actually built at import time.

Opening this as a scoped issue so a PR has something to close. Happy to fold it into the umbrella issue instead if you would rather track all four rules in one place.

Which area does this relate to?

Static typing

Solution

  1. Apply the UP045 autofix across the repository.
  2. Convert the two runtime assignments in batch/types.py by hand, verifying the resulting object compares equal to the previous definition.
  3. Remove the Optional imports left unused by the conversion, which F401 flags.
  4. Remove "UP045" from lint.ignore in ruff.toml.

Union is deliberately left in place, since that belongs to UP007 and keeping it out preserves the one rule per PR split.

Acknowledgment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions