Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruff rule UP007: Use X | Y for type annotations from PEP 604 #8252

Merged
merged 3 commits into from
Sep 9, 2023

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Sep 1, 2023

@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]

  [tool.ruff.per-file-ignores]
+ "openlibrary/solr/*" = ["UP007"]
+ "openlibrary/utils/solr.py" = ["UP007"]
+ "scripts/solr_*" = ["UP007"]

% 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 previous
typing.Union and typing.Optional syntaxes.

This rule is enabled when targeting Python 3.10 or later (see:
[target-version]). By default, it's also enabled for earlier Python
versions if from __future__ import annotations is present, as
__future__ annotations are not evaluated at runtime. If your code relies
on 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] to true.

Example

from typing import Union

foo: Union[int, str] = 1

Use instead:

foo: int | str = 1

Options

  • target-version
  • pyupgrade.keep-runtime-typing

@RayBB
Copy link
Collaborator

RayBB commented Sep 1, 2023

ha, you beat me to it! Don't forget to remove the now unused imports

@cclauss
Copy link
Contributor Author

cclauss commented Sep 1, 2023

ruff --select=F401 $(git diff --name-only master) | grep typing

@cclauss cclauss merged commit 0badb94 into internetarchive:master Sep 9, 2023
@cclauss cclauss deleted the ruff-rule-UP007 branch September 9, 2023 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants