Skip to content

New error code for slices syntax, refs #10266 #11279

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def __str__(self) -> str:

# Syntax errors are often blocking.
SYNTAX: Final = ErrorCode("syntax", "Report syntax errors", "General")
SLICE_SYNTAX: Final = ErrorCode("slice-syntax", "Report slice syntax errors", "General")

# This is a catch-all for remaining uncategorized errors.
MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")
10 changes: 10 additions & 0 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,16 @@ def add_error_info(self, info: ErrorInfo) -> None:
return
if file in self.ignored_files:
return
if (
not self.ignored_lines
and info.code
and self.is_error_code_enabled(info.code) is False
):
# We also might want to ignore some errors during `fastparse`.
# At that moment `ignored_lines` might not be ready,
# so, we only check for specific error codes.
# For example, `Dict[{str: int}]` can be ignored this way.
return
if info.only_once:
if info.message in self.only_once_messages:
return
Expand Down
14 changes: 10 additions & 4 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,9 +1359,13 @@ def parent(self) -> Optional[AST]:
return None
return self.node_stack[-2]

def fail(self, msg: str, line: int, column: int) -> None:
def fail(self, msg: str, line: int, column: int,
blocker: bool = True,
code: Optional[codes.ErrorCode] = None) -> None:
if code is None:
code = codes.SYNTAX
if self.errors:
self.errors.report(line, column, msg, blocker=True, code=codes.SYNTAX)
self.errors.report(line, column, msg, blocker=blocker, code=code)

def note(self, msg: str, line: int, column: int) -> None:
if self.errors:
Expand Down Expand Up @@ -1562,12 +1566,14 @@ def visit_Subscript(self, n: ast3.Subscript) -> Type:
if (isinstance(sliceval, ast3.Slice) or
(isinstance(sliceval, ast3.Tuple) and
any(isinstance(x, ast3.Slice) for x in sliceval.elts))):
self.fail(INVALID_SLICE_ERROR, self.line, getattr(n, 'col_offset', -1))
self.fail(INVALID_SLICE_ERROR, self.line, getattr(n, 'col_offset', -1),
blocker=False, code=codes.SLICE_SYNTAX)
return AnyType(TypeOfAny.from_error)
else:
# Python 3.8 or earlier use a different AST structure for subscripts
if not isinstance(n.slice, Index):
self.fail(INVALID_SLICE_ERROR, self.line, getattr(n, 'col_offset', -1))
self.fail(INVALID_SLICE_ERROR, self.line, getattr(n, 'col_offset', -1),
blocker=False, code=codes.SLICE_SYNTAX)
return AnyType(TypeOfAny.from_error)
sliceval = n.slice.value

Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -897,3 +897,24 @@ lst: List[int] = []
if lst:
pass
[builtins fixtures/list.pyi]

[case testSliceAnnotatedErrorDisabled39]
# flags: --python-version 3.9 --disable-error-code slice-syntax
a: Annotated[int, 1:2]
b: Dict[int, x:y]
c: Dict[x:y]

reveal_type(a) # N: Revealed type is "Any"
reveal_type(b) # N: Revealed type is "Any"
reveal_type(c) # N: Revealed type is "Any"

[case testSliceAnnotatedErrorDisabled38]
# flags: --python-version 3.8 --disable-error-code slice-syntax

a: Annotated[int, 1:2]
b: Dict[int, x:y]
c: Dict[x:y]

reveal_type(a) # N: Revealed type is "Any"
reveal_type(b) # N: Revealed type is "Any"
reveal_type(c) # N: Revealed type is "Any"
1 change: 1 addition & 0 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ x @= 1

from typing import Dict
x = None # type: Dict[x: y]
[builtins fixtures/dict.pyi]
[out]
main:3: error: Slice usage in type annotation is invalid

Expand Down