Skip to content

Commit 4d301ff

Browse files
committed
Make common STD types known by default
1 parent 10d1f8e commit 4d301ff

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

src/docstub/_analysis.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,13 @@ def __init__(
432432
----------
433433
source_pkgs: list[Path], optional
434434
known_imports: dict[str, KnownImport], optional
435+
If not provided, defaults to imports returned by
436+
:func:`common_known_imports`.
435437
"""
436438
if source_pkgs is None:
437439
source_pkgs = []
438440
if known_imports is None:
439-
known_imports = {}
441+
known_imports = common_known_imports()
440442

441443
self.current_source = None
442444
self.source_pkgs = source_pkgs

src/docstub/_docstrings.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import lark.visitors
1313
from numpydoc.docscrape import NumpyDocString # type: ignore[import-untyped]
1414

15-
from ._analysis import KnownImport
15+
from ._analysis import KnownImport, TypesDatabase
1616
from ._utils import ContextFormatter, DocstubError, accumulate_qualname, escape_qualname
1717

1818
logger = logging.getLogger(__name__)
@@ -164,11 +164,13 @@ class DoctypeTransformer(lark.visitors.Transformer):
164164
Examples
165165
--------
166166
>>> transformer = DoctypeTransformer()
167-
>>> annotation, unknown_names = transformer.doctype_to_annotation("tuple of int")
167+
>>> annotation, unknown_names = transformer.doctype_to_annotation(
168+
... "tuple of (int or ndarray)"
169+
... )
168170
>>> annotation.value
169-
'tuple[int]'
171+
'tuple[int | ndarray]'
170172
>>> unknown_names
171-
[('tuple', 0, 5), ('int', 9, 12)]
173+
[('ndarray', 17, 24)]
172174
"""
173175

174176
blacklisted_qualnames = frozenset(
@@ -212,15 +214,19 @@ def __init__(self, *, types_db=None, replace_doctypes=None, **kwargs):
212214
"""
213215
Parameters
214216
----------
215-
types_db : ~.TypesDatabase
216-
A static database of collected types usable as an annotation.
217+
types_db : ~.TypesDatabase, optional
218+
A static database of collected types usable as an annotation. If
219+
not given, defaults to a database with common types from the
220+
standard library (see :func:`~.common_known_imports`).
217221
replace_doctypes : dict[str, str], optional
218222
Replacements for human-friendly aliases.
219223
kwargs : dict[Any, Any], optional
220224
Keyword arguments passed to the init of the parent class.
221225
"""
222226
if replace_doctypes is None:
223227
replace_doctypes = {}
228+
if types_db is None:
229+
types_db = TypesDatabase()
224230

225231
self.types_db = types_db
226232
self.replace_doctypes = replace_doctypes

tests/test_stubs.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ def foo(a=None, b=1):
139139
)
140140
expected = dedent(
141141
"""
142-
from _typeshed import Incomplete as int
143142
def foo(a: int | None=..., b: int=...) -> None: ...
144143
"""
145144
)
@@ -252,11 +251,7 @@ def __init__(self, a):
252251
)
253252
expected = dedent(
254253
"""
255-
from _typeshed import Incomplete as ClassVar
256-
from _typeshed import Incomplete as bool
257-
from _typeshed import Incomplete as float
258-
from _typeshed import Incomplete as int
259-
from _typeshed import Incomplete as tuple
254+
from typing import ClassVar
260255
class Foo:
261256
a: int
262257
b: float
@@ -306,7 +301,6 @@ def foo() -> str:
306301
)
307302
expected = dedent(
308303
"""
309-
from _typeshed import Incomplete as int
310304
def foo() -> int: ...
311305
"""
312306
)

0 commit comments

Comments
 (0)