New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Any Expressions report #3492
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,58 @@ def on_finish(self) -> None: | |
register_reporter('linecount', LineCountReporter) | ||
|
||
|
||
class AnyExpressionsReporter(AbstractReporter): | ||
def __init__(self, reports: Reports, output_dir: str) -> None: | ||
super().__init__(reports, output_dir) | ||
self.counts = {} # type: Dict[str, Tuple[int, int]] | ||
stats.ensure_dir_exists(output_dir) | ||
|
||
def on_file(self, | ||
tree: MypyFile, | ||
type_map: Dict[Expression, Type], | ||
options: Options) -> None: | ||
visitor = stats.StatisticsVisitor(inferred=True, typemap=type_map, all_nodes=True) | ||
tree.accept(visitor) | ||
num_total = visitor.num_imprecise + visitor.num_precise + visitor.num_any | ||
if num_total > 0: | ||
self.counts[tree.fullname()] = (visitor.num_any, num_total) | ||
|
||
def on_finish(self) -> None: | ||
total_any = sum(num_any for num_any, _ in self.counts.values()) | ||
total_expr = sum(total for _, total in self.counts.values()) | ||
total_coverage = (float(total_expr - total_any) / float(total_expr)) * 100 | ||
|
||
any_column_name = "Anys" | ||
total_column_name = "Exprs" | ||
file_column_name = "Name" | ||
# find the longest filename all files | ||
name_width = max([len(file) for file in self.counts] + [len(file_column_name)]) | ||
# totals are the largest numbers in their column - no need to look at others | ||
any_width = max(len(str(total_any)) + 3, len(any_column_name)) | ||
exprs_width = max(len(str(total_expr)) + 3, len(total_column_name)) | ||
header = '{:{name_width}} {:>{any_width}} {:>{total_width}} {:>11}'.format( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is some real magic numbers. Should be better now! |
||
file_column_name, any_column_name, total_column_name, "Coverage", | ||
name_width=name_width, any_width=any_width, total_width=exprs_width) | ||
|
||
with open(os.path.join(self.output_dir, 'any-exprs.txt'), 'w') as f: | ||
f.write(header + '\n') | ||
separator = '-' * len(header) + '\n' | ||
f.write(separator) | ||
for file in sorted(self.counts): | ||
(num_any, num_total) = self.counts[file] | ||
coverage = (float(num_total - num_any) / float(num_total)) * 100 | ||
f.write('{:{name_width}} {:{any_width}} {:{total_width}} {:>10.2f}%\n'. | ||
format(file, num_any, num_total, coverage, name_width=name_width, | ||
any_width=any_width, total_width=exprs_width)) | ||
f.write(separator) | ||
f.write('{:{name_width}} {:{any_width}} {:{total_width}} {:>10.2f}%\n'.format('Total', | ||
total_any, total_expr, total_coverage, name_width=name_width, | ||
any_width=any_width, total_width=exprs_width)) | ||
|
||
|
||
register_reporter('any-exprs', AnyExpressionsReporter) | ||
|
||
|
||
class LineCoverageVisitor(TraverserVisitor): | ||
def __init__(self, source: List[str]) -> None: | ||
self.source = source | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the
+ 3
from here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the minimum distance between numbers in two columns.
Hopefully, this is more clear after the new commit.