|
12 | 12 | import lark.visitors |
13 | 13 | from numpydoc.docscrape import NumpyDocString # type: ignore[import-untyped] |
14 | 14 |
|
15 | | -from ._analysis import KnownImport |
| 15 | +from ._analysis import KnownImport, TypesDatabase |
16 | 16 | from ._utils import ContextFormatter, DocstubError, accumulate_qualname, escape_qualname |
17 | 17 |
|
18 | 18 | logger = logging.getLogger(__name__) |
@@ -164,11 +164,13 @@ class DoctypeTransformer(lark.visitors.Transformer): |
164 | 164 | Examples |
165 | 165 | -------- |
166 | 166 | >>> 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 | + ... ) |
168 | 170 | >>> annotation.value |
169 | | - 'tuple[int]' |
| 171 | + 'tuple[int | ndarray]' |
170 | 172 | >>> unknown_names |
171 | | - [('tuple', 0, 5), ('int', 9, 12)] |
| 173 | + [('ndarray', 17, 24)] |
172 | 174 | """ |
173 | 175 |
|
174 | 176 | blacklisted_qualnames = frozenset( |
@@ -212,15 +214,19 @@ def __init__(self, *, types_db=None, replace_doctypes=None, **kwargs): |
212 | 214 | """ |
213 | 215 | Parameters |
214 | 216 | ---------- |
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`). |
217 | 221 | replace_doctypes : dict[str, str], optional |
218 | 222 | Replacements for human-friendly aliases. |
219 | 223 | kwargs : dict[Any, Any], optional |
220 | 224 | Keyword arguments passed to the init of the parent class. |
221 | 225 | """ |
222 | 226 | if replace_doctypes is None: |
223 | 227 | replace_doctypes = {} |
| 228 | + if types_db is None: |
| 229 | + types_db = TypesDatabase() |
224 | 230 |
|
225 | 231 | self.types_db = types_db |
226 | 232 | self.replace_doctypes = replace_doctypes |
|
0 commit comments