Skip to content

Make typeshed_path a full replacement for bundled stubs #3866

Open
avokin2 wants to merge 1 commit into
facebook:mainfrom
avokin2:avokin/typeshed-path
Open

Make typeshed_path a full replacement for bundled stubs #3866
avokin2 wants to merge 1 commit into
facebook:mainfrom
avokin2:avokin/typeshed-path

Conversation

@avokin2

@avokin2 avokin2 commented Jun 18, 2026

Copy link
Copy Markdown

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:

  • Third-party imports always resolved against the bundled stubs, ignoring typeshed_path entirely.
  • The bundled stdlib was used as a fallback whenever a module was missing from <typeshed_path>/stdlib.
  • Most importantly, the Stdlib object — which supplies the class definitions for int, str, list, … used throughout type checking — was always loaded from a hardcoded
    bundled-typeshed config. So the builtins were bundled even when every other import resolved to the custom typeshed.
  • typeshed_path could only be set from pyrefly.toml, leaving editors no way to point Pyrefly at a typeshed without writing a config file.

Fix

Treat typeshed_path, when set, as a complete replacement for the bundled stubs:

  • Third-party stubs resolve only from <typeshed_path>/stubs, with each distribution subdirectory as its own search root (mirroring typeshed's layout). 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 now carries typeshed_path, so compute_stdlib builds the Stdlib from <typeshed_path>/stdlib as well. 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.

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.

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.
@meta-cla

meta-cla Bot commented Jun 18, 2026

Copy link
Copy Markdown

Hi @avokin2!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Jun 18, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants