Skip to content

Commit 0b048f3

Browse files
committed
Add basic statistics
1 parent 6b2a639 commit 0b048f3

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/docstub/_analysis.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ def __init__(
341341

342342
self.known_imports = known_imports
343343

344+
self.stats = {
345+
"successful_queries": 0,
346+
"unknown_doctypes": [],
347+
}
348+
344349
def query(self, search_name):
345350
"""Search for a known annotation name.
346351
@@ -414,6 +419,11 @@ def query(self, search_name):
414419
annotation_name.find(known_import.target) :
415420
]
416421

422+
if annotation_name is not None:
423+
self.stats["successful_queries"] += 1
424+
else:
425+
self.stats["unknown_doctypes"].append(search_name)
426+
417427
return annotation_name, known_import
418428

419429
def __repr__(self):

src/docstub/_cli.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,19 @@ def main(source_dir, out_dir, config_path, verbose):
132132
with stub_path.open("w") as fo:
133133
logger.info("wrote %s", stub_path)
134134
fo.write(stub_content)
135+
136+
# Report basic statistics
137+
successful_queries = inspector.stats["successful_queries"]
138+
click.secho(f"{successful_queries} matched annotations", fg="green")
139+
140+
grammar_errors = stub_transformer.transformer.stats["grammar_errors"]
141+
if grammar_errors:
142+
click.secho(f"{grammar_errors} grammar violations", fg="red")
143+
144+
unknown_doctypes = inspector.stats["unknown_doctypes"]
145+
if unknown_doctypes:
146+
click.secho(f"{len(unknown_doctypes)} unknown doctypes:", fg="red")
147+
click.echo(" " + "\n ".join(set(unknown_doctypes)))
148+
149+
if unknown_doctypes or grammar_errors:
150+
sys.exit(1)

src/docstub/_docstrings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ def __init__(self, *, inspector, replace_doctypes, **kwargs):
153153
self._collected_imports = None
154154
super().__init__(**kwargs)
155155

156+
self.stats = {"grammar_errors": 0}
157+
156158
def transform(self, doctype):
157159
"""Turn a type description in a docstring into a type annotation.
158160
@@ -174,6 +176,9 @@ def transform(self, doctype):
174176
value=value, imports=frozenset(self._collected_imports)
175177
)
176178
return annotation
179+
except (lark.exceptions.LexError, lark.exceptions.ParseError):
180+
self.stats["grammar_errors"] += 1
181+
raise
177182
finally:
178183
self._collected_imports = None
179184

0 commit comments

Comments
 (0)