Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PEP-0561 compatible stub-only packages #671

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Unreleased: pdoc next

- Support type-hints from stub-only packages. E.g: `scipy-stubs`
([#671](https://github.com/mitmproxy/pdoc/pull/671), @erikdesmedt)

## 2024-01-18: pdoc 14.4.0

Expand Down
14 changes: 11 additions & 3 deletions pdoc/doc_pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
in `.pyi` type stub files ([PEP 561](https://peps.python.org/pep-0561/)).
This makes it possible to add type hints for native modules such as modules written using [PyO3](https://pyo3.rs/).
"""

from __future__ import annotations

from pathlib import Path
Expand All @@ -25,10 +26,17 @@ def find_stub_file(module_name: str) -> Path | None:
"""Try to find a .pyi file with type stubs for the given module name."""
module_path = module_name.replace(".", "/")

for dir in sys.path:
# Find .pyi-file in a PEP 0561 compatible stub-package
module_part_name = module_name.split(".")
mhils marked this conversation as resolved.
Show resolved Hide resolved
module_part_name[0] = f"{module_part_name[0]}-stubs"
module_stub_path = "/".join(module_part_name)

for search_dir in sys.path:
file_candidates = [
Path(dir) / (module_path + ".pyi"),
Path(dir) / (module_path + "/__init__.pyi"),
Path(search_dir) / (module_path + ".pyi"),
Path(search_dir) / (module_path + "/__init__.pyi"),
Path(search_dir) / (module_stub_path + ".pyi"),
Path(search_dir) / (module_stub_path + "/__init__.pyi"),
]
for f in file_candidates:
if f.exists():
Expand Down
Loading