10
10
import click
11
11
import lark
12
12
import lark .visitors
13
- from numpydoc .docscrape import NumpyDocString
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__ )
@@ -150,7 +150,10 @@ class DoctypeTransformer(lark.visitors.Transformer):
150
150
151
151
Attributes
152
152
----------
153
- blacklisted_qualnames : frozenset[str]
153
+ types_db : ~.TypesDatabase
154
+ replace_doctypes : dict[str, str]
155
+ stats : dict[str, Any]
156
+ blacklisted_qualnames : ClassVar[frozenset[str]]
154
157
All Python keywords [1]_ are blacklisted from use in qualnames except for ``True``
155
158
``False`` and ``None``.
156
159
@@ -161,11 +164,13 @@ class DoctypeTransformer(lark.visitors.Transformer):
161
164
Examples
162
165
--------
163
166
>>> transformer = DoctypeTransformer()
164
- >>> 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
+ ... )
165
170
>>> annotation.value
166
- 'tuple[int]'
171
+ 'tuple[int | ndarray ]'
167
172
>>> unknown_names
168
- [('tuple ', 0, 5), ('int', 9, 12 )]
173
+ [('ndarray ', 17, 24 )]
169
174
"""
170
175
171
176
blacklisted_qualnames = frozenset (
@@ -209,15 +214,19 @@ def __init__(self, *, types_db=None, replace_doctypes=None, **kwargs):
209
214
"""
210
215
Parameters
211
216
----------
212
- types_db : ~.TypesDatabase
213
- 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`).
214
221
replace_doctypes : dict[str, str], optional
215
222
Replacements for human-friendly aliases.
216
223
kwargs : dict[Any, Any], optional
217
224
Keyword arguments passed to the init of the parent class.
218
225
"""
219
226
if replace_doctypes is None :
220
227
replace_doctypes = {}
228
+ if types_db is None :
229
+ types_db = TypesDatabase ()
221
230
222
231
self .types_db = types_db
223
232
self .replace_doctypes = replace_doctypes
@@ -272,14 +281,14 @@ def __default__(self, data, children, meta):
272
281
----------
273
282
data : lark.Token
274
283
The rule-token of the current node.
275
- children : list[lark.Token, ... ]
284
+ children : list[lark.Token]
276
285
The children of the current node.
277
286
meta : lark.tree.Meta
278
287
Meta information for the current node.
279
288
280
289
Returns
281
290
-------
282
- out : lark.Token or list[lark.Token, ... ]
291
+ out : lark.Token or list[lark.Token]
283
292
Either a token or list of tokens.
284
293
"""
285
294
if isinstance (children , list ) and len (children ) == 1 :
0 commit comments