Skip to content
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

Restrict the number of errors shown when there are missing stubs #10579

Merged
merged 4 commits into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 60 additions & 1 deletion mypy/errors.py
Expand Up @@ -10,14 +10,19 @@
from mypy.scope import Scope
from mypy.options import Options
from mypy.version import __version__ as mypy_version
from mypy.errorcodes import ErrorCode
from mypy.errorcodes import ErrorCode, IMPORT
from mypy import errorcodes as codes
from mypy.util import DEFAULT_SOURCE_OFFSET, is_typeshed_file

T = TypeVar('T')
allowed_duplicates = ['@overload', 'Got:', 'Expected:'] # type: Final


# Threshold after which we may filter out most errors to avoid very
# verbose output
MANY_ERRORS_THRESHOLD = 200
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be tunable by user (e.g. with a flag). For example, what if the errors are caused by something else (not missing imports). I always prefer to see all the errors especially when working with the PyCharm plugin.

Finally, testing will be simpler when it is tunable.



class ErrorInfo:
"""Representation of a single error message."""

Expand Down Expand Up @@ -65,6 +70,10 @@ class ErrorInfo:
# Fine-grained incremental target where this was reported
target = None # type: Optional[str]

# If True, don't show this message in output, but still record the error (needed
# by mypy daemon)
hidden = False

def __init__(self,
import_ctx: List[Tuple[str, int]],
file: str,
Expand Down Expand Up @@ -158,6 +167,10 @@ class Errors:
target_module = None # type: Optional[str]
scope = None # type: Optional[Scope]

# Have we seen an import-related error so far? If yes, we filter out other messages
# in some cases to avoid reporting huge numbers of errors.
seen_import_error = False

def __init__(self,
show_error_context: bool = False,
show_column_numbers: bool = False,
Expand Down Expand Up @@ -189,6 +202,7 @@ def initialize(self) -> None:
self.only_once_messages = set()
self.scope = None
self.target_module = None
self.seen_import_error = False

def reset(self) -> None:
self.initialize()
Expand All @@ -207,6 +221,7 @@ def copy(self) -> 'Errors':
new.function_or_member = self.function_or_member[:]
new.target_module = self.target_module
new.scope = self.scope
new.seen_import_error = self.seen_import_error
return new

def total_errors(self) -> int:
Expand Down Expand Up @@ -330,6 +345,8 @@ def _add_error_info(self, file: str, info: ErrorInfo) -> None:
if file not in self.error_info_map:
self.error_info_map[file] = []
self.error_info_map[file].append(info)
if info.code is IMPORT:
self.seen_import_error = True

def add_error_info(self, info: ErrorInfo) -> None:
file, line, end_line = info.origin
Expand All @@ -354,8 +371,49 @@ def add_error_info(self, info: ErrorInfo) -> None:
if info.message in self.only_once_messages:
return
self.only_once_messages.add(info.message)
if self.seen_import_error and info.code is not IMPORT and self.has_many_errors():
# Missing stubs can easily cause thousands of errors about
# Any types, especially when upgrading to mypy 0.900,
# which no longer bundles third-party library stubs. Avoid
# showing too many errors to make it easier to see
# import-related errors.
info.hidden = True
self.report_hidden_errors(info)
self._add_error_info(file, info)

def has_many_errors(self) -> bool:
if len(self.error_info_map) >= MANY_ERRORS_THRESHOLD:
return True
if sum(len(errors) for errors in self.error_info_map.values()) >= MANY_ERRORS_THRESHOLD:
return True
return False

def report_hidden_errors(self, info: ErrorInfo) -> None:
message = (
'(Skipping most remaining errors due to unresolved imports or missing stubs; ' +
'fix these first)'
)
if message in self.only_once_messages:
return
self.only_once_messages.add(message)
new_info = ErrorInfo(
import_ctx=info.import_ctx,
file=info.file,
module=info.module,
typ=None,
function_or_member=None,
line=info.line,
column=info.line,
severity='note',
message=message,
code=None,
blocker=False,
only_once=True,
origin=info.origin,
target=info.target,
)
self._add_error_info(info.origin[0], new_info)

def is_ignored_error(self, line: int, info: ErrorInfo, ignores: Dict[int, List[str]]) -> bool:
if info.blocker:
# Blocking errors can never be ignored
Expand Down Expand Up @@ -453,6 +511,7 @@ def format_messages(self, error_info: List[ErrorInfo],
severity 'error').
"""
a = [] # type: List[str]
error_info = [info for info in error_info if not info.hidden]
errors = self.render_messages(self.sort_messages(error_info))
errors = self.remove_duplicates(errors)
for file, line, column, severity, message, code in errors:
Expand Down
209 changes: 209 additions & 0 deletions test-data/unit/check-modules.test
Expand Up @@ -2982,3 +2982,212 @@ T = TypeVar("T")
class F(M):
x: C
class C: ...

[case testLimitLegacyStubErrorVolume]
# flags: --disallow-any-expr
import certifi # E: Cannot find implementation or library stub for module named "certifi" \
# N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
# Only show 200 errors
certifi.x # N: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
certifi.x
certifi.x
certifi.x
certifi.x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add couple more tests maybe:

  • Checking that new import errors are still shown
  • Checking that limit does not apply if there were no import errors