Skip to content

Find source docstrings for stub members declared under version checks - #5575

Open
1-DARK wants to merge 1 commit into
meta:mainfrom
1-DARK:fix-5568-hover-stdlib-docstring
Open

1-DARK wants to merge 1 commit into
meta:mainfrom
1-DARK:fix-5568-hover-stdlib-docstring

Conversation

@1-DARK

@1-DARK 1-DARK commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Hover on Path(".").read_text() showed the signature but no docstring.

Typeshed stubs have no docstrings. Hover takes the docstring from the matching declaration in the runtime .py file instead. In pathlib.pyi, read_text
is declared under if sys.version_info >= (3, 13):. The lookup that maps a stub declaration to its source declaration (lsp_attributes.rs) only searched
the top level of a class body, so it never found the method.

This PR treats statements nested in if/elif/else blocks as part of the enclosing scope. It does this both when locating the declaration in the stub
and when searching the source.

Fixes #5568

  • I am a human, and I fully understand the code I am submitting.

Test Plan

  • Added unit test test_method_in_conditional_block. It maps a stub method declared under if sys.version_info to the source method and checks that the
    docstring is found.
  • Ran cargo test hover, and all tests pass.
  • Ran python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema --no-extension, and it is clean.
  • Sent a textDocument/hover request on Path(".").read_text() with Python 3.15. The response now includes "Open the file in text mode, read it, and
    close the file."
  • Not tested on Python 3.13, where Path is defined in pathlib/_local.py and re-exported. That case may still show no docstring.

Hover on a stdlib method such as Path.read_text showed no docstring.
Typeshed stubs declare many members inside `if sys.version_info` blocks,
and the lookup that maps a stub declaration to its source declaration
only searched the top level of a class body, so it gave up.

Treat statements nested in `if` blocks as part of the enclosing scope
when locating a declaration in the stub and in the source.

Fixes meta#5568
@meta-cla meta-cla Bot added the cla signed label Nov 7, 2026
@github-actions github-actions Bot added the size/m label Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

✅ This PR meets all requirements from our pull request checklist.

Note: this comment updates automatically after changes to the PR. Updates may take up to half an hour.

@meta-codesync

meta-codesync Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D130437464. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

This branch has not been deployed

No deployments
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.

Hover does not show pathlib.Path.read_text docstring

2 participants