Skip to content

Commit ef8de11

Browse files
committed
Refactor StaticInspector to TypesDatabase
Reflects the current implementation why more.
1 parent 848a1ff commit ef8de11

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

src/docstub/_analysis.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,18 +346,18 @@ def leave_FunctionDef(self, original_node: cst.FunctionDef) -> None:
346346
self._stack.pop()
347347

348348

349-
class StaticInspector:
350-
"""Static analysis of Python packages.
349+
class TypesDatabase:
350+
"""A static database of collected types usable as an annotation.
351351
352352
Attributes
353353
----------
354354
current_source : ~.PackageFile | None
355355
356356
Examples
357357
--------
358-
>>> from docstub._analysis import StaticInspector, common_known_imports
359-
>>> inspector = StaticInspector(known_imports=common_known_imports())
360-
>>> inspector.query("Any")
358+
>>> from docstub._analysis import TypesDatabase, common_known_imports
359+
>>> db = TypesDatabase(known_imports=common_known_imports())
360+
>>> db.query("Any")
361361
('Any', <KnownImport 'from typing import Any'>)
362362
"""
363363

src/docstub/_cli.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
from ._analysis import (
1010
KnownImport,
11-
StaticInspector,
1211
TypeCollector,
12+
TypesDatabase,
1313
common_known_imports,
1414
)
1515
from ._cache import FileCache
@@ -145,12 +145,12 @@ def main(source_dir, out_dir, config_path, verbose):
145145
config = _load_configuration(config_path)
146146
known_imports = _build_import_map(config, source_dir)
147147

148-
inspector = StaticInspector(
148+
types_db = TypesDatabase(
149149
source_pkgs=[source_dir.parent.resolve()], known_imports=known_imports
150150
)
151151
# and the stub transformer
152152
stub_transformer = Py2StubTransformer(
153-
inspector=inspector, replace_doctypes=config.replace_doctypes
153+
types_db=types_db, replace_doctypes=config.replace_doctypes
154154
)
155155

156156
if not out_dir:
@@ -182,14 +182,14 @@ def main(source_dir, out_dir, config_path, verbose):
182182
fo.write(stub_content)
183183

184184
# Report basic statistics
185-
successful_queries = inspector.stats["successful_queries"]
185+
successful_queries = types_db.stats["successful_queries"]
186186
click.secho(f"{successful_queries} matched annotations", fg="green")
187187

188188
grammar_errors = stub_transformer.transformer.stats["grammar_errors"]
189189
if grammar_errors:
190190
click.secho(f"{grammar_errors} grammar violations", fg="red")
191191

192-
unknown_doctypes = inspector.stats["unknown_doctypes"]
192+
unknown_doctypes = types_db.stats["unknown_doctypes"]
193193
if unknown_doctypes:
194194
click.secho(f"{len(unknown_doctypes)} unknown doctypes:", fg="red")
195195
click.echo(" " + "\n ".join(set(unknown_doctypes)))

src/docstub/_docstrings.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,12 @@ class DoctypeTransformer(lark.visitors.Transformer):
155155
[('tuple', 0, 5), ('int', 9, 12)]
156156
"""
157157

158-
def __init__(self, *, inspector=None, replace_doctypes=None, **kwargs):
158+
def __init__(self, *, types_db=None, replace_doctypes=None, **kwargs):
159159
"""
160160
Parameters
161161
----------
162-
inspector : ~.StaticInspector
163-
A dictionary mapping atomic names used in doctypes to information such
164-
as where to import from or how to replace the name itself.
162+
types_db : ~.TypesDatabase
163+
A static database of collected types usable as an annotation.
165164
replace_doctypes : dict[str, str], optional
166165
Replacements for human-friendly aliases.
167166
kwargs : dict[Any, Any], optional
@@ -170,7 +169,7 @@ def __init__(self, *, inspector=None, replace_doctypes=None, **kwargs):
170169
if replace_doctypes is None:
171170
replace_doctypes = {}
172171

173-
self.inspector = inspector
172+
self.types_db = types_db
174173
self.replace_doctypes = replace_doctypes
175174

176175
self._collected_imports = None
@@ -302,16 +301,16 @@ def contains(self, tree):
302301
def literals(self, tree):
303302
out = ", ".join(tree.children)
304303
out = f"Literal[{out}]"
305-
if self.inspector is not None:
306-
_, known_import = self.inspector.query("Literal")
304+
if self.types_db is not None:
305+
_, known_import = self.types_db.query("Literal")
307306
if known_import:
308307
self._collected_imports.add(known_import)
309308
return out
310309

311310
def _find_import(self, qualname, meta):
312311
"""Match type names to known imports."""
313-
if self.inspector is not None:
314-
annotation_name, known_import = self.inspector.query(qualname)
312+
if self.types_db is not None:
313+
annotation_name, known_import = self.types_db.query(qualname)
315314
else:
316315
annotation_name = None
317316
known_import = None

src/docstub/_stubs.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class Py2StubTransformer(cst.CSTTransformer):
179179
180180
Attributes
181181
----------
182-
inspector : ~._analysis.StaticInspector
182+
types_db : ~.TypesDatabase
183183
"""
184184

185185
METADATA_DEPENDENCIES = (cst.metadata.PositionProvider,)
@@ -198,17 +198,17 @@ class Py2StubTransformer(cst.CSTTransformer):
198198
_Annotation_Any = cst.Annotation(cst.Name("Any"))
199199
_Annotation_None = cst.Annotation(cst.Name("None"))
200200

201-
def __init__(self, *, inspector=None, replace_doctypes=None):
201+
def __init__(self, *, types_db=None, replace_doctypes=None):
202202
"""
203203
Parameters
204204
----------
205-
inspector : ~._analysis.StaticInspector
205+
types_db : ~.TypesDatabase
206206
replace_doctypes : dict[str, str]
207207
"""
208-
self.inspector = inspector
208+
self.types_db = types_db
209209
self.replace_doctypes = replace_doctypes
210210
self.transformer = DoctypeTransformer(
211-
inspector=inspector, replace_doctypes=replace_doctypes
211+
types_db=types_db, replace_doctypes=replace_doctypes
212212
)
213213
# Relevant docstring for the current context
214214
self._scope_stack = None # Entered module, class or function scopes
@@ -225,8 +225,8 @@ def current_source(self):
225225
@current_source.setter
226226
def current_source(self, value):
227227
self._current_source = value
228-
if self.inspector is not None:
229-
self.inspector.current_source = value
228+
if self.types_db is not None:
229+
self.types_db.current_source = value
230230

231231
def python_to_stub(self, source, *, module_path=None):
232232
"""Convert Python source code to stub-file ready code.

tests/test_analysis.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import pytest
22

3-
from docstub._analysis import KnownImport, StaticInspector
3+
from docstub._analysis import KnownImport, TypesDatabase
44

55

6-
class Test_StaticInspector:
6+
class Test_TypesDatabase:
77
known_imports = { # noqa: RUF012
88
"dict": KnownImport(builtin_name="dict"),
99
"np": KnownImport(import_name="numpy", import_alias="np"),
@@ -48,9 +48,9 @@ class Test_StaticInspector:
4848
]
4949
)
5050
def test_query(self, name, exp_annotation, exp_import_line):
51-
inspector = StaticInspector(known_imports=self.known_imports.copy())
51+
db = TypesDatabase(known_imports=self.known_imports.copy())
5252

53-
annotation, known_import = inspector.query(name)
53+
annotation, known_import = db.query(name)
5454

5555
if exp_annotation is None and exp_import_line is None:
5656
assert exp_annotation is annotation

0 commit comments

Comments
 (0)