ruff: FA102 errors eventhough requires-python in pyproject.toml is ">=3.12"

I have the following (standard) directory layout:

├── src
│   └── packagename
│       └── ...
├── pyproject.toml
└── ruff.toml

Part from my pyproject.toml:

[project]
name = "packagename"
requires-python = ">=3.12"

My complete ruff.toml:

src = ["src"]

[format]
skip-magic-trailing-comma = true
line-ending = "lf"

[lint]
select = ["ALL"]
ignore = ["ANN101", "D203", "D213", "S320"]

Ruff version: 0.3.0, run with the command ruff check in the directory of pyproject.toml. In the rule FA102, it is mentioned:

This rule respects the target-version setting. For example, if your project targets Python 3.10 and above, adding from future import annotations does not impact your ability to leverage PEP 604-style unions (e.g., to convert Optional[str] to str | None). As such, this rule will only flag such usages if your project targets Python 3.9 or below.

In the documentation of target-version it is mentioned that requires-python in pyproject.toml is preferred over the target-version option in ruff.toml. If I add target-version = "py312" to my ruff.toml, then there are no FA102 errors.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 26 (18 by maintainers)

Commits related to this issue

Most upvoted comments

You can use per-file-target-version or extend with target-version

@Winand whether you want to open a new issue depends on your goal:

  • if you want help, please open a new issue.
  • if you don’t need help and you only want to document another use case where this behavior is problematic, commenting on this issue is the right place.

We should probably do the same as for uv and what’s planned for Red Knot.

If there’s both a ruff.toml and pyproject.toml, use the ruff.toml but fallback to the pyproject.toml’s requires-python if the target-version is left unspecified.

I’d like to make the case that ruff should not be relying on declared configuration and should instead rely on the core metadata for the package. Although pyproject.toml is a standard for a source distribution to declare metadata, it’s not (and should not) be required that the package’s metadata be declared through pyproject.toml. As an extreme example, the coherent build system doesn’t require any pyproject.toml and instead derives all of the metadata (including the Requires-Python declaration) from environmental state, specifically the tags on the CPython repository in GitHub.

For this reason, the supported/required Python versions for a Coherent project or any other build system that dynamically or programmatically generates that metadata field won’t have a declaration in the pyproject.toml nor ruff.toml.

There are other build backends that produce “dynamic” values for various metadata fields, so addressing just the Requires-Python field will leave those fields unaddressed.

There’s clear value for projects in not having to statically manifest all of their metadata in source code (and maintain that state, possibly shared across or even diverging within many projects).

My recommendation would be for Ruff to generate the package metadata through the PEP 517 prepare_metadata_for_build_wheel hook and then read the Requires-Python field.

I know this approach can be potentially expensive (and for Coherent projects is currently very expensive, but that’s largely the backend’s problem), so it may be necessary to cache the value (or the metadata) for extended periods.

This approach would have added benefits that other core metadata can be resolved by ruff, even when it’s dynamic.

Such an approach would sidestep workarounds like extend = "pyproject.toml" or duplicating the metadata in two files and would honor the essential metadata of the package.