Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
error code
  • Loading branch information
hauntsaninja committed Nov 29, 2025
commit ea7323771d478374cbe011cf22af8ce7ab013a17
3 changes: 3 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ def __hash__(self) -> int:
"General",
default_enabled=False,
)
STR_UNPACK: Final[ErrorCode] = ErrorCode(
"str-unpack", "Warn about expressions that unpack str", "General"
)
NAME_MATCH: Final = ErrorCode(
"name-match", "Check that type definition has consistent naming", "General"
)
Expand Down
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ def wrong_number_values_to_unpack(
)

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

def type_not_iterable(self, type: Type, context: Context) -> None:
self.fail(f"{format_type(type, self.options)} object is not iterable", context)
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2492,3 +2492,20 @@ x + T # E: Unsupported left operand type for + ("int")
T() # E: "TypeVar" not callable
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]

[case testStringDisallowedUnpacking]
d: dict[str, str]

for a1, b1 in d: # E: Unpacking a string is disallowed
reveal_type(a1) # E: Cannot determine type of "a1" \
# N: Revealed type is "Any"
reveal_type(b1) # E: Cannot determine type of "b1" \
# N: Revealed type is "Any"

s = "foo"
a2, b2 = s # E: Unpacking a string is disallowed
reveal_type(a2) # E: Cannot determine type of "a2" \
# N: Revealed type is "Any"
reveal_type(b2) # E: Cannot determine type of "b2" \
# N: Revealed type is "Any"
[builtins fixtures/dict.pyi]
14 changes: 0 additions & 14 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -729,20 +729,6 @@ reveal_type(x) # N: Revealed type is "Any"
reveal_type(y) # N: Revealed type is "Any"
[out]

[case testStringDisallowedUnpacking]
from typing import Dict

d: Dict[str, str]

for a, b in d: # E: Unpacking a string is disallowed
pass

s = "foo"
a, b = s # E: Unpacking a string is disallowed

[builtins fixtures/dict.pyi]
[out]

[case testUnionAlwaysTooMany]
from typing import Union, Tuple
bad: Union[Tuple[int, int, int], Tuple[str, str, str]]
Expand Down