ruff rule UP007: Use X | Y
for type annotations from PEP 604
#8252
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.
@cdrini Related to #8236 (comment), are there other solr-related files that cannot use the PEP 604 syntax for type annotations?
str | None
vs.Optional[str]
%
ruff rule UP007
non-pep604-annotation (UP007)
Derived from the pyupgrade linter.
Autofix is sometimes available.
What it does
Check for type annotations that can be rewritten based on PEP 604 syntax.
Why is this bad?
PEP 604 introduced a new syntax for union type annotations based on the
|
operator. This syntax is more concise and readable than the previoustyping.Union
andtyping.Optional
syntaxes.This rule is enabled when targeting Python 3.10 or later (see:
[
target-version
]). By default, it's also enabled for earlier Pythonversions if
from __future__ import annotations
is present, as__future__
annotations are not evaluated at runtime. If your code relieson runtime type annotations (either directly or via a library like
Pydantic), you can disable this behavior for Python versions prior to 3.10
by setting [
pyupgrade.keep-runtime-typing
] totrue
.Example
Use instead:
Options
target-version
pyupgrade.keep-runtime-typing