mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-07 21:25:08 +00:00
describe requires-python fallback in docs (#16704)
Adds description of `requires-python` fallback to documentation for configuration file discovery. --------- Co-authored-by: Micha Reiser <micha@reiser.io> Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
This commit is contained in:
parent
abaa18993b
commit
b9b256209b
3 changed files with 15 additions and 2 deletions
|
@ -312,7 +312,9 @@ pub struct Options {
|
|||
/// ```
|
||||
///
|
||||
/// If both are specified, `target-version` takes precedence over
|
||||
/// `requires-python`.
|
||||
/// `requires-python`. See [_Inferring the Python version_](https://docs.astral.sh/ruff/configuration/#inferring-the-python-version)
|
||||
/// for a complete description of how the `target-version` is determined
|
||||
/// when left unspecified.
|
||||
///
|
||||
/// Note that a stub file can [sometimes make use of a typing feature](https://typing.readthedocs.io/en/latest/spec/distributing.html#syntax)
|
||||
/// before it is available at runtime, as long as the stub does not make
|
||||
|
|
|
@ -307,6 +307,17 @@ If Ruff detects multiple configuration files in the same directory, the `.ruff.t
|
|||
precedence over the `ruff.toml` file, and the `ruff.toml` file will take precedence over
|
||||
the `pyproject.toml` file.
|
||||
|
||||
### Inferring the Python version
|
||||
When no discovered configuration specifies a [`target-version`](settings.md#target-version), Ruff will attempt to fall back to the minimum version compatible with the `requires-python` field in a nearby `pyproject.toml`.
|
||||
The rules for this behavior are as follows:
|
||||
|
||||
1. If a configuration file is passed directly, Ruff does not attempt to infer a missing `target-version`.
|
||||
1. If a configuration file is found in the filesystem hierarchy, Ruff will infer a missing `target-version` from the `requires-python` field in a `pyproject.toml` file in the same directory as the found configuration.
|
||||
1. If we are using a user-level configuration from `${config_dir}/ruff/pyproject.toml`, the `requires-python` field in the first `pyproject.toml` file found in an ancestor of the current working directory takes precedence over the `target-version` in the user-level configuration.
|
||||
1. If no configuration files are found, Ruff will infer the `target-version` from the `requires-python` field in the first `pyproject.toml` file found in an ancestor of the current working directory.
|
||||
|
||||
Note that in these last two cases, the behavior of Ruff may differ depending on the working directory from which it is invoked.
|
||||
|
||||
## Python file discovery
|
||||
|
||||
When passed a path on the command-line, Ruff will automatically discover all Python files in that
|
||||
|
|
2
ruff.schema.json
generated
2
ruff.schema.json
generated
|
@ -702,7 +702,7 @@
|
|||
}
|
||||
},
|
||||
"target-version": {
|
||||
"description": "The minimum Python version to target, e.g., when considering automatic code upgrades, like rewriting type annotations. Ruff will not propose changes using features that are not available in the given version.\n\nFor example, to represent supporting Python >=3.10 or ==3.10 specify `target-version = \"py310\"`.\n\nIf you're already using a `pyproject.toml` file, we recommend `project.requires-python` instead, as it's based on Python packaging standards, and will be respected by other tools. For example, Ruff treats the following as identical to `target-version = \"py38\"`:\n\n```toml [project] requires-python = \">=3.8\" ```\n\nIf both are specified, `target-version` takes precedence over `requires-python`.\n\nNote that a stub file can [sometimes make use of a typing feature](https://typing.readthedocs.io/en/latest/spec/distributing.html#syntax) before it is available at runtime, as long as the stub does not make use of new *syntax*. For example, a type checker will understand `int | str` in a stub as being a `Union` type annotation, even if the type checker is run using Python 3.9, despite the fact that the `|` operator can only be used to create union types at runtime on Python 3.10+. As such, Ruff will often recommend newer features in a stub file than it would for an equivalent runtime file with the same target version.",
|
||||
"description": "The minimum Python version to target, e.g., when considering automatic code upgrades, like rewriting type annotations. Ruff will not propose changes using features that are not available in the given version.\n\nFor example, to represent supporting Python >=3.10 or ==3.10 specify `target-version = \"py310\"`.\n\nIf you're already using a `pyproject.toml` file, we recommend `project.requires-python` instead, as it's based on Python packaging standards, and will be respected by other tools. For example, Ruff treats the following as identical to `target-version = \"py38\"`:\n\n```toml [project] requires-python = \">=3.8\" ```\n\nIf both are specified, `target-version` takes precedence over `requires-python`. See [_Inferring the Python version_](https://docs.astral.sh/ruff/configuration/#inferring-the-python-version) for a complete description of how the `target-version` is determined when left unspecified.\n\nNote that a stub file can [sometimes make use of a typing feature](https://typing.readthedocs.io/en/latest/spec/distributing.html#syntax) before it is available at runtime, as long as the stub does not make use of new *syntax*. For example, a type checker will understand `int | str` in a stub as being a `Union` type annotation, even if the type checker is run using Python 3.9, despite the fact that the `|` operator can only be used to create union types at runtime on Python 3.10+. As such, Ruff will often recommend newer features in a stub file than it would for an equivalent runtime file with the same target version.",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/PythonVersion"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue