Skip to content

Commit

Permalink
Fix crashes with unpacking SyntaxError (#11499)
Browse files Browse the repository at this point in the history
In general, mypy doesn't promise to be able to check the remainder of
your code in the presence of syntax errors, so just make this a blocking
error.

Fixes #9137
Fixes #3825 (most of the reports in this issue were fixed by #8827)

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja authored Nov 15, 2022
1 parent dd0503e commit e0a37fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,8 @@ class Foo(Bar, Generic[T]): ...
declared_tvars: TypeVarLikeList = []
is_protocol = False
for i, base_expr in enumerate(base_type_exprs):
if isinstance(base_expr, StarExpr):
base_expr.valid = True
self.analyze_type_expr(base_expr)

try:
Expand Down Expand Up @@ -4539,8 +4541,7 @@ def visit_dict_expr(self, expr: DictExpr) -> None:

def visit_star_expr(self, expr: StarExpr) -> None:
if not expr.valid:
# XXX TODO Change this error message
self.fail("Can use starred expression only as assignment target", expr)
self.fail("Can use starred expression only as assignment target", expr, blocker=True)
else:
expr.expr.accept(self)

Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,17 @@ b = (1, 'x')
a = (0, *b, '')
[builtins fixtures/tuple.pyi]

[case testUnpackSyntaxError]
*foo # E: Can use starred expression only as assignment target
[builtins fixtures/tuple.pyi]

[case testUnpackBases]
class A: ...
class B: ...
bases = (A, B)
class C(*bases): ... # E: Invalid base class
[builtins fixtures/tuple.pyi]

[case testTupleMeetTupleAny]
from typing import Union, Tuple
class A: pass
Expand Down

0 comments on commit e0a37fa

Please sign in to comment.