Skip to content

Commit ea73237

Browse files
committed
error code
1 parent ece4d41 commit ea73237

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

mypy/errorcodes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ def __hash__(self) -> int:
215215
"General",
216216
default_enabled=False,
217217
)
218+
STR_UNPACK: Final[ErrorCode] = ErrorCode(
219+
"str-unpack", "Warn about expressions that unpack str", "General"
220+
)
218221
NAME_MATCH: Final = ErrorCode(
219222
"name-match", "Check that type definition has consistent naming", "General"
220223
)

mypy/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ def wrong_number_values_to_unpack(
11441144
)
11451145

11461146
def unpacking_strings_disallowed(self, context: Context) -> None:
1147-
self.fail("Unpacking a string is disallowed", context)
1147+
self.fail("Unpacking a string is disallowed", context, code=codes.STR_UNPACK)
11481148

11491149
def type_not_iterable(self, type: Type, context: Context) -> None:
11501150
self.fail(f"{format_type(type, self.options)} object is not iterable", context)

test-data/unit/check-expressions.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,3 +2492,20 @@ x + T # E: Unsupported left operand type for + ("int")
24922492
T() # E: "TypeVar" not callable
24932493
[builtins fixtures/tuple.pyi]
24942494
[typing fixtures/typing-full.pyi]
2495+
2496+
[case testStringDisallowedUnpacking]
2497+
d: dict[str, str]
2498+
2499+
for a1, b1 in d: # E: Unpacking a string is disallowed
2500+
reveal_type(a1) # E: Cannot determine type of "a1" \
2501+
# N: Revealed type is "Any"
2502+
reveal_type(b1) # E: Cannot determine type of "b1" \
2503+
# N: Revealed type is "Any"
2504+
2505+
s = "foo"
2506+
a2, b2 = s # E: Unpacking a string is disallowed
2507+
reveal_type(a2) # E: Cannot determine type of "a2" \
2508+
# N: Revealed type is "Any"
2509+
reveal_type(b2) # E: Cannot determine type of "b2" \
2510+
# N: Revealed type is "Any"
2511+
[builtins fixtures/dict.pyi]

test-data/unit/check-unions.test

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -729,20 +729,6 @@ reveal_type(x) # N: Revealed type is "Any"
729729
reveal_type(y) # N: Revealed type is "Any"
730730
[out]
731731

732-
[case testStringDisallowedUnpacking]
733-
from typing import Dict
734-
735-
d: Dict[str, str]
736-
737-
for a, b in d: # E: Unpacking a string is disallowed
738-
pass
739-
740-
s = "foo"
741-
a, b = s # E: Unpacking a string is disallowed
742-
743-
[builtins fixtures/dict.pyi]
744-
[out]
745-
746732
[case testUnionAlwaysTooMany]
747733
from typing import Union, Tuple
748734
bad: Union[Tuple[int, int, int], Tuple[str, str, str]]

0 commit comments

Comments
 (0)