Skip to content

Commit 03e2e38

Browse files
Refactor the special ErrorCode function into a static method.
I think this also fixes a bug.
1 parent 77a5cbb commit 03e2e38

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

mypy/errorcodes.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@ def __init__(
3434
sub_code_map[sub_code_of.code].add(code)
3535
error_codes[code] = self
3636

37-
def is_import_related_code(self) -> bool:
38-
return IMPORT in (self.code, self.sub_code_of)
37+
@staticmethod
38+
def is_code_or_sub_code_of(
39+
possible_child_code: ErrorCode | None, possible_parent_code: ErrorCode
40+
) -> bool:
41+
"""Check if the first code ⊆ the second code, so to speak.
42+
If None is supplied as the first argument, this is always false.
43+
Again, to quote the assert in ErrorCode above, "Nested subcategories are not supported"."""
44+
if possible_child_code is None:
45+
return False
46+
else:
47+
return possible_parent_code in (possible_child_code, possible_child_code.sub_code_of)
3948

4049
def __str__(self) -> str:
4150
return f"<ErrorCode {self.code}>"

mypy/errors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from mypy import errorcodes as codes
1313
from mypy.error_formatter import ErrorFormatter
14-
from mypy.errorcodes import ErrorCode, mypy_error_codes
14+
from mypy.errorcodes import IMPORT, ErrorCode, mypy_error_codes
1515
from mypy.nodes import Context
1616
from mypy.options import Options
1717
from mypy.scope import Scope
@@ -583,7 +583,7 @@ def _add_error_info(self, file: str, info: ErrorInfo) -> None:
583583
self.error_info_map[file].append(info)
584584
if info.blocker:
585585
self.has_blockers.add(file)
586-
if info.code is not None and info.code.is_import_related_code():
586+
if ErrorCode.is_code_or_sub_code_of(info.code, IMPORT):
587587
self.seen_import_error = True
588588

589589
def get_watchers(self) -> Iterator[ErrorWatcher]:
@@ -630,7 +630,7 @@ def add_error_info(self, info: ErrorInfo) -> None:
630630
self.only_once_messages.add(info.message)
631631
if (
632632
self.seen_import_error
633-
and (info.code is None or (not info.code.is_import_related_code()))
633+
and ErrorCode.is_code_or_sub_code_of(info.code, IMPORT)
634634
and self.has_many_errors()
635635
):
636636
# Missing stubs can easily cause thousands of errors about

0 commit comments

Comments
 (0)