Skip to content

Commit c5ce82c

Browse files
implement import-untyped-stubs-available
1 parent 1fc7d81 commit c5ce82c

4 files changed

Lines changed: 16 additions & 9 deletions

File tree

docs/source/running_mypy.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ This is slower than explicitly installing stubs, since it effectively
397397
runs mypy twice -- the first time to find the missing stubs, and
398398
the second time to type check your code properly after mypy has
399399
installed the stubs. It also can make controlling stub versions harder,
400-
resulting in less reproducible type checking.
400+
resulting in less reproducible type checking — it might even install
401+
incompatible versions of your project's non-type dependencies, if the
402+
type stubs require them!
401403

402404
By default, :option:`--install-types <mypy --install-types>` shows a confirmation prompt.
403405
Use :option:`--non-interactive <mypy --non-interactive>` to install all suggested

mypy/build.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,11 +2783,10 @@ def module_not_found(
27832783
msg, notes = reason.error_message_templates(daemon)
27842784
if reason == ModuleNotFoundReason.NOT_FOUND:
27852785
code = codes.IMPORT_NOT_FOUND
2786-
elif (
2787-
reason == ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS
2788-
or reason == ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
2789-
):
2786+
elif reason == ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS:
27902787
code = codes.IMPORT_UNTYPED
2788+
elif reason == ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED:
2789+
code = codes.IMPORT_UNTYPED_STUBS_AVAILABLE
27912790
else:
27922791
code = codes.IMPORT
27932792
errors.report(line, 0, msg.format(module=target), code=code)

mypy/errorcodes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ 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)
39+
3740
def __str__(self) -> str:
3841
return f"<ErrorCode {self.code}>"
3942

@@ -114,7 +117,10 @@ def __hash__(self) -> int:
114117
"import-untyped", "Require that imported module has stubs", "General", sub_code_of=IMPORT
115118
)
116119
IMPORT_UNTYPED_STUBS_AVAILABLE: Final = ErrorCode(
117-
"import-untyped-stubs-available", "Require that imported module (with known stubs) has stubs", "General", sub_code_of=IMPORT
120+
"import-untyped-stubs-available",
121+
"Require that imported module (with known stubs) has stubs",
122+
"General",
123+
sub_code_of=IMPORT,
118124
)
119125
NO_REDEF: Final = ErrorCode("no-redef", "Check that each name is defined once", "General")
120126
FUNC_RETURNS_VALUE: Final = ErrorCode(

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 IMPORT, IMPORT_NOT_FOUND, IMPORT_UNTYPED, ErrorCode, mypy_error_codes
14+
from mypy.errorcodes 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 in (IMPORT, IMPORT_UNTYPED, IMPORT_NOT_FOUND):
586+
if info.code is not None and info.code.is_import_related_code():
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 not in (IMPORT, IMPORT_UNTYPED, IMPORT_NOT_FOUND)
633+
and (info.code is None or (not info.code.is_import_related_code()))
634634
and self.has_many_errors()
635635
):
636636
# Missing stubs can easily cause thousands of errors about

0 commit comments

Comments
 (0)