Skip to content

Commit af9525b

Browse files
Merge branch 'master' into update-imports
2 parents 96f7367 + ece4d41 commit af9525b

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

mypy/argmap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def map_actuals_to_formals(
3636
The result contains a list of caller argument indexes mapping to each
3737
callee argument index, indexed by callee index.
3838
39-
The caller_arg_type argument should evaluate to the type of the actual
40-
argument type with the given index.
39+
The actual_arg_type argument should evaluate to the type of the actual
40+
argument with the given index.
4141
"""
4242
nformals = len(formal_kinds)
4343
formal_to_actual: list[list[int]] = [[] for i in range(nformals)]

mypy/checker.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5307,16 +5307,22 @@ def get_types_from_except_handler(self, typ: Type, n: Expression) -> list[Type]:
53075307
"""Helper for check_except_handler_test to retrieve handler types."""
53085308
typ = get_proper_type(typ)
53095309
if isinstance(typ, TupleType):
5310-
return typ.items
5310+
merged_type = make_simplified_union(typ.items)
5311+
if isinstance(merged_type, UnionType):
5312+
return merged_type.relevant_items()
5313+
return [merged_type]
5314+
elif is_named_instance(typ, "builtins.tuple"):
5315+
# variadic tuple
5316+
merged_type = make_simplified_union((typ.args[0],))
5317+
if isinstance(merged_type, UnionType):
5318+
return merged_type.relevant_items()
5319+
return [merged_type]
53115320
elif isinstance(typ, UnionType):
53125321
return [
53135322
union_typ
53145323
for item in typ.relevant_items()
53155324
for union_typ in self.get_types_from_except_handler(item, n)
53165325
]
5317-
elif is_named_instance(typ, "builtins.tuple"):
5318-
# variadic tuple
5319-
return [typ.args[0]]
53205326
else:
53215327
return [typ]
53225328

mypy/fastparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def parse_type_ignore_tag(tag: str | None) -> list[str] | None:
286286
if m is None:
287287
# Invalid "# type: ignore" comment.
288288
return None
289-
return [code.strip() for code in m.group(1).split(",")]
289+
return [stripped_code for code in m.group(1).split(",") if (stripped_code := code.strip())]
290290

291291

292292
def parse_type_comment(

test-data/unit/check-errorcodes.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ x # type: ignore[name-defined] # E: Unused "type: ig
118118
"x" # type: ignore[xyz, unused-ignore]
119119
x # type: ignore[name-defined, unused-ignore]
120120

121+
[case testErrorCodeWarnUnusedIgnores9_SloppyFormattingNotEmptyBrackets]
122+
# flags: --warn-unused-ignores
123+
1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined]
124+
1 + "ok" + "ok".foo # type: ignore[ ,operator,attr-defined]
125+
1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined,]
126+
1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined,,]
127+
128+
[case testErrorCodeWarnUnusedIgnores10_SloppyFormattingEmptyBrackets]
129+
# flags: --warn-unused-ignores
130+
1+1 # type: ignore # E: Unused "type: ignore" comment [unused-ignore]
131+
1+1 # type: ignore[] # E: Unused "type: ignore" comment [unused-ignore]
132+
1+1 # type: ignore[ ] # E: Unused "type: ignore" comment [unused-ignore]
133+
1+1 # type: ignore[,,, ] # E: Unused "type: ignore" comment [unused-ignore]
134+
121135
[case testErrorCodeMissingWhenRequired]
122136
# flags: --enable-error-code ignore-without-code
123137
"x" # type: ignore # E: "type: ignore" comment without error code [ignore-without-code]

test-data/unit/check-statements.test

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,57 @@ def error_in_variadic(exc: Tuple[int, ...]) -> None:
801801

802802
[builtins fixtures/tuple.pyi]
803803

804+
[case testExceptWithMultipleTypes5]
805+
from typing import Tuple, Type, Union
806+
807+
class E1(BaseException): pass
808+
class E2(BaseException): pass
809+
class E3(BaseException): pass
810+
811+
def union_in_variadic(exc: Tuple[Union[Type[E1], Type[E2]], ...]) -> None:
812+
try:
813+
pass
814+
except exc as e:
815+
reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2]"
816+
817+
def nested_union_in_variadic(exc: Tuple[Union[Type[E1], Union[Type[E2], Type[E3]]], ...]) -> None:
818+
try:
819+
pass
820+
except exc as e:
821+
reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2, __main__.E3]"
822+
823+
def union_in_tuple(exc: Tuple[Union[Type[E1], Type[E2]], Type[E3]]) -> None:
824+
try:
825+
pass
826+
except exc as e:
827+
reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2, __main__.E3]"
828+
829+
def error_in_variadic_union(exc: Tuple[Union[Type[E1], int], ...]) -> None:
830+
try:
831+
pass
832+
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
833+
pass
834+
835+
def error_in_variadic_nested_union(exc: Tuple[Union[Type[E1], Union[Type[E2], int]], ...]) -> None:
836+
try:
837+
pass
838+
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
839+
pass
840+
841+
def error_in_tuple_inside_variadic_nested_union(exc: Tuple[Union[Type[E1], Union[Type[E2], Tuple[Type[E3]]]], ...]) -> None:
842+
try:
843+
pass
844+
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
845+
pass
846+
847+
def error_in_tuple_union(exc: Tuple[Union[Type[E1], Type[E2]], Union[Type[E3], int]]) -> None:
848+
try:
849+
pass
850+
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
851+
pass
852+
853+
[builtins fixtures/tuple.pyi]
854+
804855
[case testExceptWithAnyTypes]
805856
from typing import Any
806857

0 commit comments

Comments
 (0)