Skip to content

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Jun 25, 2025

Summary

This PR moves the SearchPaths::from_settings call and the resolution of the final Python version into Options::to_program_settings.

We want to use ProgramSettings as a cache key for persistent caching (e.g., checking different Python versions should resolve in loading different persistent caches). For this to work well, it's important that ProgramSettings mainly holds resolved settings. For example, it's essential that the following two runs in an activated virtual environment using Python 3.10 result in the same computed cache key: ty check --python-version 3.10 and ty check.

The reason I made this change now is that I investigated what it would mean if the LSP passed the selected interpreter in the Python extension as a lowest-priority fallback value. We want to make sure that ty reuses the persistent cache between a ty check --python-version 3.10 run and when the interpreter selected in the IDE uses Python 3.10.

This should hopefully also simplify the handling of VIRTUAL_ENV and CONDA_ENV as fallback if no python version is provided.

Test Plan

Existing tests. I did move some tests from ProjectMetadata to CLI tests because keeping them in the metadata tests would have required increasing the visibility of many fields.

@MichaReiser MichaReiser changed the title [ty] Move search path resolution to Options::to_program_settings" [ty] Move search path resolution to Options::to_program_settings Jun 25, 2025
@MichaReiser MichaReiser added internal An internal refactor or improvement ty Multi-file analysis & type inference labels Jun 25, 2025
@MichaReiser MichaReiser requested a review from AlexWaygood June 25, 2025 13:26
Copy link
Contributor

github-actions bot commented Jun 25, 2025

mypy_primer results

No ecosystem changes detected ✅

Copy link
Contributor

github-actions bot commented Jun 25, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Comment on lines -126 to -128
if current_python_version != &python_version_from_environment
&& current_python_version.source.priority()
<= python_version_from_environment.source.priority()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there's a test demonstrating why this is needed. The Default, Config and Cli cases are inherent by how we resolve options. I would expect that the other cases work because we simply re-run the entire settings resolution (including the search path discovery) whenever the settings change and the priority should be encoded in the setting/search path resolution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was possibly confused about when exactly the update_search_paths method is called.

Could it be called without re-resolving all other settings if e.g. a .pth file in the site-packages directory changes (which affects editable-installation search paths)? If so, it's possible that the update_search_paths method might accidentally override the Python version specified in a pyproject.toml file, even though the version in the pyproject.toml file should always take precedence over the Python version inferred from a virtual environment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only called when the VERSIONS file in the typeshed directory changed and we then resolve all settings the same as we would when the configuration changed.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me!

@MichaReiser MichaReiser marked this pull request as ready for review June 25, 2025 13:54
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked line-by-line but the change makes sense to me, and nothing looked obviously wrong from skimming through. I also think we've got pretty good test coverage for all this, which gives me confidence in the refactor!

@MichaReiser MichaReiser merged commit 5d546c6 into main Jun 25, 2025
36 checks passed
@MichaReiser MichaReiser deleted the micha/move-search-path-resolution branch June 25, 2025 16:00
dcreager added a commit that referenced this pull request Jun 26, 2025
* main:
  [ty] Add regression-benchmark for attribute-assignment hang (#18957)
  [ty] Format conflicting types as an enumeration (#18956)
  [ty] Prevent union builder construction for just one declaration (#18954)
  [ty] Infer nonlocal types as unions of all reachable bindings (#18750)
  [`pyflakes`] Mark `F504`/`F522`/`F523` autofix as unsafe if there's a call with side effect (#18839)
  [`playground`] Add ruff logo docs link to Header.tsx (#18947)
  [ty] Reduce the overwhelming complexity of `TypeInferenceBuilder::infer_call_expression` (#18943)
  [ty] Add subdiagnostic about empty bodies in more cases (#18942)
  [ty] Move search path resolution to `Options::to_program_settings` (#18937)
  [`flake8-errmsg`] Extend `EM101` to support byte strings (#18867)
  Move big rule implementations (#18931)
  [`pylint`] Allow fix with comments and document performance implications (`PLW3301`) (#18936)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants