mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-21 20:29:20 +08:00
micha/flat-python-ast
4
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
e501689d12 |
[ty] Inherit project settings and overrides for PEP 723 scripts (#29793)
## Summary This changes how ty handles script options. Up to now, scripts never inherited any settings from the project. I think this is overly pedantic. While I consider script isolation important in terms of dependencies, I don't think the same is true for type checking or linter settings. The main motivation for not-inheriting project settings is that scripts can be shared. Checking them in isolation ensures that everyone gets a consistent experience. I don't think this is something most users care about in terms of linting and type checking scripts. Our own scripts are a good example. We have multiple scripts and we have no intention to share them outside this project. In fact, we've found it very annoying to repeat the same ty configuration in every script. I also ran an experiment in https://github.com/astral-sh/ruff/pull/29768 to understand the ecosystem impact of using ty's current configuration inheritance in Ruff. Without project inheritance, 17 of 38 scripts in the seven projects sampled had diagnostics. Not only would this be a very disruptive change. I also think that it's very reasonable that you want to enforce the same standard to both regular python files as well as scripts. This PR changes how ty resolve script settings to: * If a script has a `tool.ty` section, ignore the project's configuration. Users who prefer the current behavior can restore it by adding an empty `tool.ty` section to their script (or check their script outside the project). * If a script has no `tool.ty` section, inherit the project's configuration. This is the main change. * If a script has no `tool.ty` section, and the script matches an `overrides` entry, inherit the project and override settings, like we do for any other file. This allows users to extend the project configuration (except for `environment` settings). An explicit `--config-file` still overrides any settings specified in the script. Matching file overrides from that config also apply. A script's `requires-python` takes precedence over the project's Python version. An inline `environment.python-version` takes precedence over `requires-python`. Fixes astral-sh/ty#4083 and addresses the [ignored file override reported in ty#4566](https://github.com/astral-sh/ty/issues/4566#issuecomment-5762949445). ## Considerations The main downside of this change is that changing a script's `environment.root` from `[]` to `["."]` implies opting out of the project's config inheritance, because it requires adding a `tool.ty` section. I don't have a good solution to this, other that we could consider adding a `extend` configuration option in the future, that allows explicitly inheriting from another configuration. Which is something users have asked for before (for projects). The other nuanced decision in this PR is that script's inherit a project's `environment.extra-paths`. I think this is okay, given that the main use case is to override e.g. typing stubs. But I could see an argument that a script should not inherit any of the search path related settings. ## Test Plan All script CLI tests passed, including the uv integration tests. |
||
|
|
a103832c28 |
[ty] Check PEP 723 scripts in isolation (#27462)
## Summary This PR extends the PEP 723 configuration support to `ty.environment` settings. That is: * Scripts are checked with a separate virtual environment and search paths * Scripts can configure a different Python version * Scripts can configure a different Python platform Our long-term plan is to use uv to create the script's virtual environment. We don't do this in this PR. Instead, we: * Respect `VIRTUAL_ENV`, LSP fallback environment (the selected environment), global available Python installation. Unlike for projects, ty does not search for a local `.venv` folder * Unlike for projects, scripts default to an empty `environment.root` because scripts are considered to work in isolation. Users can customize `environment.root`. Out of scope for this PR but things we should look into next: * Report a diagnostic if a script's metadata fails to parse instead of falling back to the project's configuration. * Add support for the Python environment extension's file-specific virtual env (so that you can select a virtual env for a specific file) * Preserve source ranges for configuration options from script metadata (shouldn't be too hard but the PR is already large as it is) * Integrating with uv. I expect that this will require significant changes to the implementation to provide good UX, but I like to tackle problems one step at a time (it also makes it easier to explain why we changed something). * We currently don't watch for changes in script search paths. That means, results in the LSP or when using `ty check --watch` could get outdated ## Test Plan Testing: Added integration and language-server coverage and verified the relevant unit, CLI, server, and Markdown suites. |
||
|
|
9b64be4e40 | [ty] Simplify script metadata query (#27121) | ||
|
|
a415d0a065 |
[ty] Respect rules and analysis in PEP 723 script metadata configurations (#26671)
## Summary Add support for configuring some (`rules` and `analysis` settings, same as for `overrides`) from PEP 723 `tool.ty` script metadata. I do plan to add support for `environment` settings and maybe even `terminal`, but that requires more work, because different environments require different dbs. Given that we'll need to use different dbs. Why bother with integrating this into `file_settings`, which only allows overriding a small sub-set of options. The idea behind this is that I want to share the same db for scripts with the same `environment` settings (and dependencies in the future). This should reduce overall resource consumption because it allows sharing parsed ASTs, semantic indices, for scripts that use the same environment (and venv, which might not be that uncommon, thanks to uv's canonical venvs). The integration into `file_settings` enables this sharing by creating separate DBs only for the settings where this is necessary, but relying on `file_settings` to handle differences in enabled rules etc. Using `file_settings` has one downside. It most likely means that the script metadata is parsed twice. I think this is negligible. Most metadata blocks are tiny. https://github.com/user-attachments/assets/d2be2e67-2a6f-405a-9852-860377f8496c ## Configuration precedence Scripts don't inherit any settings from the project (you got me, this is not a 100% true today where we still use the project's python version, but it will be). They're fully isolated. Each of them represent their own project. I'm not a 100% sure yet if this is the ideal behavior or if we need some mechanism that allows inheriting settings from the project. But I'd like to defer this discussion to later. Scripts respect configurations from other sources, the same as projects do: * CLI options or LSP configurations take highest precedence, overriding configurations in the script metadata * The scripts own configuration * User configuration * Fallback configurations. We only use this in the LSP today as a hint of what the python version is. Specifically, we use the selected python environment in VS Code, if the user didn't specify a python environment. ## Error handling For now, I've intentionally decided not to deal with invalid script metadata blocks. I expect that the validation should happen in `check_file`, or even earlier, in the code constructing the script's database. This is something I'll tackle next, and is why this PR assumes that all script metadata blocks are valid, blocks with syntax errors are simply ignored, unknown rules are not flagged. ## Memory regression The main regression comes from the new `script_metadata` query. The query already uses an `Option<Box<Metadata>>` to reduce the overhead for files without script metadata. The second cause of the regression is that `file_settings` now depends on `script_metadata`. A dependency that Salsa now needs to track. There's not much we can do about this. I considered moving the text scan into `file_settings` but that only trades a dependency on `script_metadata` for depending on `source_text` (and doing more work). We do need to record the dependency because a user can add a script metadata block to a regular file, which changes the file's settings. I don't see a way (with proper incremental computation) to fully avoid the memory regression. We may want to enable within-same-revision garbage collection in the future, as we don't really need to retain the metadata for a prolonged time. ## Test Plan Added query, mdtest, and CLI coverage. --------- Co-authored-by: Aria Desires <aria.desires@gmail.com> |