You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fallback to requires-python in certain cases when target-version is not found (#16319)
# Summary
This PR introduces the following modifications in configuration
resolution:
1. In the event where we are reading in a configuration file `myconfig`
with no `target-version` specified, we will search for a
`pyproject.toml` in the _same directory_ as `myconfig` and see if it has
a `requires-python` field. If so, we will use that as the
`target-version`.
2. In the event where...
- we have not used the flag `--isolated`, and
- we have not found any configuration file, and
- we have not passed `--config` specifying a target version
then we will search for a `pyproject.toml` file with `required-python`
in an ancestor directory and use that if we find it.
We've also added some debug logs to indicate which of these paths is
taken.
## Implementation
Two small things:
1. I have chosen a method that will sometimes re-parse a
`pyproject.toml` file that has already been parsed at some earlier stage
in the resolution process. It seemed like avoiding that would require
more drastic changes - but maybe there is a clever way that I'm not
seeing!
2. When searching for these fallbacks, I suppress any errors that may
occur when parsing `pyproject.toml`s rather than propagate them. The
reasoning here is that we have already found or decided upon a perfectly
good configuration, and this is just a "bonus" to find a better guess
for the target version.
Closes#14813, #16662
## Testing
The linked issue contains a repo for reproducing the behavior, which we
used for a manual test:
```console
ruff-F821-repro on main
❯ uvx ruff check --no-cache
hello.py:9:11: F821 Undefined name `anext`. Consider specifying `requires-python = ">= 3.10"` or `tool.ruff.target-version = "py310"` in your `pyproject.toml` file.
|
8 | async def main():
9 | print(anext(g()))
| ^^^^^ F821
|
Found 1 error.
ruff-F821-repro on main
❯ ../ruff/target/debug/ruff check --no-cache
All checks passed!
```
In addition, we've added snapshot tests with the CLI output in some
examples. Please let me know if there are some additional scenarios
you'd like me to add tests for!
---------
Co-authored-by: Micha Reiser <[email protected]>
0 commit comments