Skip to content

Commit

Permalink
Support PEP-0561 compatible stub-only packages (#671)
Browse files Browse the repository at this point in the history
Add support for stub-only package.
The typehints for the package `flyingcircus` could be in the
`flyingcircus-stubs` package.
  • Loading branch information
ErikDeSmedt committed Feb 28, 2024
1 parent 25f325d commit f75638f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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(".")
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

0 comments on commit f75638f

Please sign in to comment.