Make typeshed_path a full replacement for bundled stubs #3866
Conversation
A configured `typeshed_path` only partially redirected resolution, so a user who pointed Pyrefly at their own typeshed still got results — and the declaration URIs returned over LSP/TSP — out of Pyrefly's bundled stubs: - third-party imports always resolved against the bundled stubs; - the bundled stdlib was used as a fallback when a module was missing from `<typeshed_path>/stdlib`; - most importantly, the `Stdlib` object (which supplies the class definitions for `int`, `str`, `list`, ... used throughout checking) was *always* loaded via a hardcoded bundled-typeshed config, so builtins were bundled even when imports resolved to the custom typeshed; - and `typeshed_path` could only be set from a `pyrefly.toml`, leaving editors no way to point Pyrefly at their typeshed without writing a file. Treat `typeshed_path` as a complete replacement for the bundled stubs. When it is set: - third-party stubs resolve only from `<typeshed_path>/stubs` (each distribution subdirectory is its own search root, mirroring typeshed's layout), and the bundled stdlib branch is skipped — a miss falls through to the remaining stages (site-packages, ...) and never reaches the bundled stubs; - the stdlib loader config carries `typeshed_path`, so `compute_stdlib` builds the `Stdlib` from `<typeshed_path>/stdlib` too. `invalidate_config` drops the cached stdlib so a changed `typeshed_path` is picked up. Because the setting now suffices on its own, no separate opt-out knob is needed. Also surface `typeshed_path` over the LSP `initializationOptions` / `workspace/configuration` channel (as `typeshedPath`), deferring to an explicit value from a user-owned config file.
|
Hi @avokin2! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Problem
A configured typeshed_path only partially redirected import resolution. A user who pointed Pyrefly at their own typeshed still got results — and the declaration URIs
returned over LSP/TSP — out of Pyrefly's bundled stubs:
bundled-typeshed config. So the builtins were bundled even when every other import resolved to the custom typeshed.
Fix
Treat typeshed_path, when set, as a complete replacement for the bundled stubs:
branch is skipped: a miss falls through to the remaining stages (site-packages, …) and never reaches the bundled stubs.
so a changed typeshed_path is picked up.
Because the setting now suffices on its own, no separate opt-out knob is needed.
Finally, surface typeshed_path over LSP via initializationOptions / workspace/configuration (as typeshedPath), deferring to an explicit value from a user-owned config
file when one is present.
Summary
Makes a configured typeshed_path fully shadow Pyrefly's bundled stubs — for stdlib, third-party stubs, and the Stdlib builtins object — and lets editors set it over LSP
without a config file.
Test Plan
Added unit tests for each layer, and replaced the test that locked in the old bundled-always behavior.