GeezerVison is showing github.com — Use standard formatting for note on unexpected keyword by ilevkivskyi · Pull Request #20808 · python/mypy
Use standard formatting for note on unexpected keyword
#20808
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.py
(7)
.test
(17)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -90,14 +90,7 @@
WORKER_START_TIMEOUT,
)
from mypy.error_formatter import OUTPUT_CHOICES, ErrorFormatter
from mypy.errors import (
CompileError,
ErrorInfo,
Errors,
ErrorTuple,
ErrorTupleRaw,
report_internal_error,
)
from mypy.errors import CompileError, ErrorInfo, Errors, ErrorTuple, report_internal_error
from mypy.graph_utils import prepare_sccs, strongly_connected_components, topsort
from mypy.indirection import TypeIndirectionVisitor
from mypy.ipc import (
Expand All
@@ -111,21 +104,11 @@
send,
)
from mypy.messages import MessageBuilder
from mypy.nodes import (
ClassDef,
Context,
Import,
ImportAll,
ImportBase,
ImportFrom,
MypyFile,
SymbolTable,
)
from mypy.nodes import Import, ImportAll, ImportBase, ImportFrom, MypyFile, SymbolTable
from mypy.options import OPTIONS_AFFECTING_CACHE_NO_PLATFORM
from mypy.partially_defined import PossiblyUndefinedVariableVisitor
from mypy.semanal import SemanticAnalyzer
from mypy.semanal_pass1 import SemanticAnalyzerPreAnalysis
from mypy.traverser import find_definitions
from mypy.util import (
DecodeError,
decode_python_encoding,
Expand Down
Expand Up
@@ -908,10 +891,6 @@ def __init__(
self.queue_order: int = 0
# Is this an instance used by a parallel worker?
self.parallel_worker = parallel_worker
# Cache for ASTs created during error message generation. Note these are
# raw parsed trees not analyzed with mypy. We use these to find absolute
# location of a symbol used as a location for an error message.
self.extra_trees: dict[str, MypyFile] = {}
# Snapshot of import-related options per module. We record these even for
# suppressed imports, since they can affect errors in the callers. Bytes
# value is opaque but can be compared to detect changes in options.
Expand Down
Expand Up
@@ -1080,60 +1059,6 @@ def report_file(
if self.reports is not None and self.source_set.is_source(file):
self.reports.file(file, self.modules, type_map, options)
def resolve_location(self, graph: dict[str, State], fullname: str) -> Context | None:
"""Resolve an absolute location of a symbol with given fullname."""
rest = []
head = fullname
while True:
# TODO: this mimics the logic in lookup.py but it is actually wrong.
# This is because we don't distinguish between submodule and a local symbol
# with the same name.
head, tail = head.rsplit(".", maxsplit=1)
rest.append(tail)
if head in graph:
state = graph[head]
break
if "." not in head:
return None
*prefix, name = reversed(rest)
# If this happens something is wrong, but it is better to give slightly
# less helpful error message than crash.
if state.path is None:
return None
if state.tree is not None and state.tree.defs:
# We usually free ASTs after processing, but reuse an existing AST if
# it is still available.
tree = state.tree
elif state.id in self.extra_trees:
tree = self.extra_trees[state.id]
else:
if state.source is not None:
# Sources are usually discarded after processing as well, check
# if we still have one just in case.
source = state.source
else:
path = state.manager.maybe_swap_for_shadow_path(state.path)
source = decode_python_encoding(state.manager.fscache.read(path))
tree = parse(source, state.path, state.id, state.manager.errors, state.options)
# TODO: run first pass of semantic analysis on freshly parsed trees,
# we need this to get correct reachability information.
self.extra_trees[state.id] = tree
statements = tree.defs
while prefix:
part = prefix.pop(0)
for statement in statements:
defs = find_definitions(statement, part)
if not defs or not isinstance((defn := defs[0]), ClassDef):
continue
statements = defn.defs.body
break
else:
return None
for statement in statements:
if defs := find_definitions(statement, name):
return defs[0]
return None
def verbosity(self) -> int:
return self.options.verbosity
Expand Down
Expand Up
@@ -3999,9 +3924,7 @@ def find_stale_sccs(
path = manager.errors.simplify_path(graph[id].xpath)
formatted = manager.errors.format_messages(
path,
transform_error_tuples(
manager, graph, deserialize_codes(graph[id].error_lines)
),
deserialize_codes(graph[id].error_lines),
formatter=manager.error_formatter,
)
manager.flush_errors(path, formatted, False)
Expand Down
Expand Up
@@ -4271,9 +4194,7 @@ def process_stale_scc(
if graph[id].xpath not in manager.errors.ignored_files:
errors = manager.errors.file_messages(graph[id].xpath)
formatted = manager.errors.format_messages(
graph[id].xpath,
transform_error_tuples(manager, graph, errors),
formatter=manager.error_formatter,
graph[id].xpath, errors, formatter=manager.error_formatter
)
manager.flush_errors(manager.errors.simplify_path(graph[id].xpath), formatted, False)
errors_by_id[id] = errors
Expand Down
Expand Up
@@ -4455,37 +4376,14 @@ def write_undocumented_ref_info(
metastore.write(ref_info_file, json_dumps(deps_json))
def transform_error_tuples(
manager: BuildManager, graph: dict[str, State], error_tuples_rel: list[ErrorTupleRaw]
) -> list[ErrorTuple]:
"""Transform raw error tuples by resolving relative error locations."""
error_tuples = []
for e in error_tuples_rel:
file, line_rel, column, end_line, end_column, severity, message, code = e
if isinstance(line_rel, int):
line = line_rel
else:
assert file is not None
loc = manager.resolve_location(graph, line_rel)
if loc is not None:
line = loc.line
column = loc.column
end_line = loc.end_line or -1
end_column = loc.end_column or -1
else:
line = -1
error_tuples.append((file, line, column, end_line, end_column, severity, message, code))
return error_tuples
def serialize_codes(errs: list[ErrorTupleRaw]) -> list[SerializedError]:
def serialize_codes(errs: list[ErrorTuple]) -> list[SerializedError]:
return [
(path, line, column, end_line, end_column, severity, message, code.code if code else None)
for path, line, column, end_line, end_column, severity, message, code in errs
]
def deserialize_codes(errs: list[SerializedError]) -> list[ErrorTupleRaw ]:
def deserialize_codes(errs: list[SerializedError]) -> list[ErrorTuple ]:
return [
(
path,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -69,9 +69,9 @@
from mypy_extensions import u8
# High-level cache layout format
CACHE_VERSION: Final = 4
CACHE_VERSION: Final = 5
SerializedError: _TypeAlias = tuple[str | None, int | str , int, int, int, str, str, str | None]
SerializedError: _TypeAlias = tuple[str | None, int, int, int, int, str, str, str | None]
class CacheMeta:
Expand Down
Expand Up
@@ -504,10 +504,7 @@ def write_errors(data: WriteBuffer, errs: list[SerializedError]) -> None:
for path, line, column, end_line, end_column, severity, message, code in errs:
write_tag(data, TUPLE_GEN)
write_str_opt(data, path)
if isinstance(line, str):
write_str(data, line)
else:
write_int(data, line)
write_int(data, line)
write_int(data, column)
write_int(data, end_line)
write_int(data, end_column)
Expand All
@@ -521,17 +518,10 @@ def read_errors(data: ReadBuffer) -> list[SerializedError]:
result = []
for _ in range(read_int_bare(data)):
assert read_tag(data) == TUPLE_GEN
path = read_str_opt(data)
tag = read_tag(data)
if tag == LITERAL_STR:
line: str | int = read_str_bare(data)
else:
assert tag == LITERAL_INT
line = read_int_bare(data)
result.append(
(
path ,
line ,
read_str_opt(data) ,
read_int(data) ,
read_int(data),
read_int(data),
read_int(data),
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -130,7 +130,6 @@ def __init__(
target: str | None = None,
priority: int = 0,
parent_error: ErrorInfo | None = None,
location_ref: str | None = None,
) -> None:
self.import_ctx = import_ctx
self.file = file
Expand All
@@ -152,18 +151,12 @@ def __init__(
if parent_error is not None:
assert severity == "note", "Only notes can specify parent errors"
self.parent_error = parent_error
self.location_ref = location_ref
# Type used internally to represent errors:
# (path, line, column, end_line, end_column, severity, message, code)
ErrorTuple: _TypeAlias = tuple[str | None, int, int, int, int, str, str, ErrorCode | None]
# A raw version of the above that can refer to either absolute or relative location.
# If the location is relative, the first item (line) is a string with a symbol fullname,
# and three other values (column, end_line, end_column) are set to -1.
ErrorTupleRaw: _TypeAlias = tuple[str | None, int | str, int, int, int, str, str, ErrorCode | None]
class ErrorWatcher:
"""Context manager that can be used to keep track of new errors recorded
Expand Down
Expand Up
@@ -569,14 +562,12 @@ def report(
*,
blocker: bool = False,
severity: str = "error",
file: str | None = None,
only_once: bool = False,
origin_span: Iterable[int] | None = None,
offset: int = 0,
end_line: int | None = None,
end_column: int | None = None,
parent_error: ErrorInfo | None = None,
location_ref: str | None = None,
) -> ErrorInfo:
"""Report message at the given line using the current error context.
Expand All
@@ -587,7 +578,6 @@ def report(
code: error code (defaults to 'misc'; not shown for notes)
blocker: if True, don't continue analysis after this error
severity: 'error' or 'note'
file: if non-None, override current file as context
only_once: if True, only report this exact message once per build
origin_span: if non-None, override current context as origin
(type: ignores have effect here)
Expand All
@@ -611,8 +601,6 @@ def report(
else:
end_column = column + 1
if file is None:
file = self.file
if offset:
message = " " * offset + message
Expand All
@@ -627,7 +615,7 @@ def report(
info = ErrorInfo(
import_ctx=self.import_context(),
file=file,
file=self. file,
module=self.current_module(),
typ=type,
function_or_member=function,
Expand All
@@ -643,7 +631,6 @@ def report(
origin=(self.file, origin_span),
target=self.current_target(),
parent_error=parent_error,
location_ref=location_ref,
)
self.add_error_info(info)
return info
Expand Down
Expand Up
@@ -1019,8 +1006,6 @@ def raise_error(self, use_stdout: bool = True) -> NoReturn:
"""
# self.new_messages() will format all messages that haven't already
# been returned from a file_messages() call.
# TODO: pass resolve_location callback here.
# This will be needed if we are going to use relative locations in blocker errors.
raise CompileError(
self.new_messages(), use_stdout=use_stdout, module_with_blocker=self.blocker_module()
)
Expand Down
Expand Up
@@ -1083,7 +1068,7 @@ def format_messages_default(
a.append(" " * (DEFAULT_SOURCE_OFFSET + column) + marker)
return a
def file_messages(self, path: str) -> list[ErrorTupleRaw ]:
def file_messages(self, path: str) -> list[ErrorTuple ]:
"""Return an error tuple list of new error messages from a given file."""
if path not in self.error_info_map:
return []
Expand Down
Expand Up
@@ -1126,9 +1111,7 @@ def find_shadow_file_mapping(self, path: str) -> str | None:
return i[1]
return None
def new_messages(
self, resolve_location: Callable[[str], Context | None] | None = None
) -> list[str]:
def new_messages(self) -> list[str]:
"""Return a string list of new error messages.
Use a form suitable for displaying to the user.
Expand All
@@ -1138,29 +1121,7 @@ def new_messages(
msgs = []
for path in self.error_info_map.keys():
if path not in self.flushed_files:
error_tuples_rel = self.file_messages(path)
error_tuples = []
for e in error_tuples_rel:
# This has a bit of code duplication with build.py, but it is hard
# to avoid without either an import cycle or a performance penalty.
file, line_rel, column, end_line, end_column, severity, message, code = e
if isinstance(line_rel, int):
line = line_rel
elif resolve_location is not None:
assert file is not None
loc = resolve_location(line_rel)
if loc is not None:
line = loc.line
column = loc.column
end_line = loc.end_line or -1
end_column = loc.end_column or -1
else:
line = -1
else:
line = -1
error_tuples.append(
(file, line, column, end_line, end_column, severity, message, code)
)
error_tuples = self.file_messages(path)
msgs.extend(self.format_messages(path, error_tuples))
return msgs
Expand All
@@ -1172,15 +1133,15 @@ def targets(self) -> set[str]:
info.target for errs in self.error_info_map.values() for info in errs if info.target
}
def render_messages(self, errors: list[ErrorInfo]) -> list[ErrorTupleRaw ]:
def render_messages(self, errors: list[ErrorInfo]) -> list[ErrorTuple ]:
"""Translate the messages into a sequence of tuples.
Each tuple is of form (path, line, col, severity, message, code).
The rendered sequence includes information about error contexts.
The path item may be None. If the line item is negative, the
line number is not defined for the tuple.
"""
result: list[ErrorTupleRaw ] = []
result: list[ErrorTuple ] = []
prev_import_context: list[tuple[str, int]] = []
prev_function_or_member: str | None = None
prev_type: str | None = None
Expand Down
Expand Up
@@ -1255,21 +1216,9 @@ def render_messages(self, errors: list[ErrorInfo]) -> list[ErrorTupleRaw]:
else:
result.append((file, -1, -1, -1, -1, "note", f'In class "{e.type}":', None))
if e.location_ref is not None:
result.append((file, e.location_ref, -1, -1, -1, e.severity, e.message, e.code))
else:
result.append(
(
file,
e.line,
e.column,
e.end_line,
e.end_column,
e.severity,
e.message,
e.code,
)
)
result.append(
(file, e.line, e.column, e.end_line, e.end_column, e.severity, e.message, e.code)
)
prev_import_context = e.import_ctx
prev_function_or_member = e.function_or_member
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.