|
66 | 66 | import itertools |
67 | 67 | import re |
68 | 68 | import sys |
69 | | -from typing import Any, Iterator, Optional, Pattern |
| 69 | +from typing import Any, Dict, Iterator, Optional, Pattern, Union |
70 | 70 |
|
71 | 71 | import astroid |
72 | 72 | from astroid import nodes |
|
81 | 81 | is_property_setter, |
82 | 82 | ) |
83 | 83 | from pylint.reporters.ureports import nodes as reporter_nodes |
| 84 | +from pylint.typing import CheckerStats |
84 | 85 |
|
85 | 86 |
|
86 | 87 | class NamingStyle: |
@@ -386,36 +387,42 @@ def _has_abstract_methods(node): |
386 | 387 | return len(utils.unimplemented_abstract_methods(node)) > 0 |
387 | 388 |
|
388 | 389 |
|
389 | | -def report_by_type_stats(sect, stats, old_stats): |
| 390 | +def report_by_type_stats( |
| 391 | + sect, |
| 392 | + stats: CheckerStats, |
| 393 | + old_stats: CheckerStats, |
| 394 | +): |
390 | 395 | """make a report of |
391 | 396 |
|
392 | 397 | * percentage of different types documented |
393 | 398 | * percentage of different types with a bad name |
394 | 399 | """ |
395 | 400 | # percentage of different types documented and/or with a bad name |
396 | | - nice_stats = {} |
| 401 | + nice_stats: Dict[str, Dict[str, str]] = {} |
397 | 402 | for node_type in ("module", "class", "method", "function"): |
398 | 403 | try: |
399 | | - total = stats[node_type] |
| 404 | + total: int = stats[node_type] # type: ignore |
400 | 405 | except KeyError as e: |
401 | 406 | raise exceptions.EmptyReportError() from e |
402 | 407 | nice_stats[node_type] = {} |
403 | 408 | if total != 0: |
404 | 409 | try: |
405 | | - documented = total - stats["undocumented_" + node_type] |
| 410 | + undocumented_node: int = stats["undocumented_" + node_type] # type: ignore |
| 411 | + documented = total - undocumented_node |
406 | 412 | percent = (documented * 100.0) / total |
407 | 413 | nice_stats[node_type]["percent_documented"] = f"{percent:.2f}" |
408 | 414 | except KeyError: |
409 | 415 | nice_stats[node_type]["percent_documented"] = "NC" |
410 | 416 | try: |
411 | | - percent = (stats["badname_" + node_type] * 100.0) / total |
| 417 | + badname_node: int = stats["badname_" + node_type] # type: ignore |
| 418 | + percent = (badname_node * 100.0) / total |
412 | 419 | nice_stats[node_type]["percent_badname"] = f"{percent:.2f}" |
413 | 420 | except KeyError: |
414 | 421 | nice_stats[node_type]["percent_badname"] = "NC" |
415 | 422 | lines = ["type", "number", "old number", "difference", "%documented", "%badname"] |
416 | 423 | for node_type in ("module", "class", "method", "function"): |
417 | 424 | new = stats[node_type] |
418 | | - old = old_stats.get(node_type, None) |
| 425 | + old: Optional[Union[str, int]] = old_stats.get(node_type, None) # type: ignore |
419 | 426 | if old is not None: |
420 | 427 | diff_str = lint_utils.diff_string(old, new) |
421 | 428 | else: |
@@ -1082,7 +1089,7 @@ class BasicChecker(_BasicChecker): |
1082 | 1089 |
|
1083 | 1090 | def __init__(self, linter): |
1084 | 1091 | _BasicChecker.__init__(self, linter) |
1085 | | - self.stats = None |
| 1092 | + self.stats: CheckerStats = {} |
1086 | 1093 | self._tryfinallys = None |
1087 | 1094 |
|
1088 | 1095 | def open(self): |
@@ -1159,13 +1166,13 @@ def _check_using_constant_test(self, node, test): |
1159 | 1166 |
|
1160 | 1167 | def visit_module(self, _: nodes.Module) -> None: |
1161 | 1168 | """check module name, docstring and required arguments""" |
1162 | | - self.stats["module"] += 1 |
| 1169 | + self.stats["module"] += 1 # type: ignore |
1163 | 1170 |
|
1164 | 1171 | def visit_classdef(self, _: nodes.ClassDef) -> None: |
1165 | 1172 | """check module name, docstring and redefinition |
1166 | 1173 | increment branch counter |
1167 | 1174 | """ |
1168 | | - self.stats["class"] += 1 |
| 1175 | + self.stats["class"] += 1 # type: ignore |
1169 | 1176 |
|
1170 | 1177 | @utils.check_messages( |
1171 | 1178 | "pointless-statement", "pointless-string-statement", "expression-not-assigned" |
@@ -1304,7 +1311,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None: |
1304 | 1311 | """check function name, docstring, arguments, redefinition, |
1305 | 1312 | variable names, max locals |
1306 | 1313 | """ |
1307 | | - self.stats["method" if node.is_method() else "function"] += 1 |
| 1314 | + self.stats["method" if node.is_method() else "function"] += 1 # type: ignore |
1308 | 1315 | self._check_dangerous_default(node) |
1309 | 1316 |
|
1310 | 1317 | visit_asyncfunctiondef = visit_functiondef |
@@ -2040,7 +2047,7 @@ def _raise_name_warning( |
2040 | 2047 | ) |
2041 | 2048 |
|
2042 | 2049 | self.add_message(warning, node=node, args=args, confidence=confidence) |
2043 | | - self.stats["badname_" + node_type] += 1 |
| 2050 | + self.stats["badname_" + node_type] += 1 # type: ignore |
2044 | 2051 |
|
2045 | 2052 | def _name_allowed_by_regex(self, name: str) -> bool: |
2046 | 2053 | return name in self.config.good_names or any( |
|
0 commit comments