Skip to content

Conversation

@hauntsaninja
Copy link
Collaborator

Fixes #13823. See also #6406

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
- tests/test_frame.py:2001: error: Unpacking a string is disallowed  [misc]
- tests/test_frame.py:2007: error: Unpacking a string is disallowed  [misc]
- tests/test_series.py:606: error: Unpacking a string is disallowed  [misc]
- tests/test_series.py:612: error: Unpacking a string is disallowed  [misc]

operator (https://github.com/canonical/operator)
- ops/framework.py:166: error: Unpacking a string is disallowed  [misc]

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/client_reqrep.py:350: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_log.py:206: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_log.py:207: error: Unused "type: ignore[has-type]" comment  [unused-ignore]
+ aiohttp/web_log.py:208: error: Unused "type: ignore[has-type]" comment  [unused-ignore]
+ aiohttp/web_log.py:209: error: Unused "type: ignore[has-type]" comment  [unused-ignore]

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it makes sense to make this an opt-in check, since it can generate false positives. Left some minor comments, looks good overall.

.. _code-str-unpacking:

Check that ``str`` is explicitly unpacked [str-unpacking]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title is a bit unclear. What about something like "Check that 'str' is not unpacked?". Or "... is not unpacked implicitly", though I'm not sure what's the difference between explicit and implicit unpacking here.

Bikeshedding: what about renaming the error code to str-unpack, which would be a bit shorter?

Check that ``str`` is explicitly unpacked [str-unpacking]
---------------------------------------------------------

It can sometimes be surprising that ``str`` is iterable, especially when unpacking.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly enumerate the contexts where this matters (for loop, assignment)?

)
elif isinstance(rvalue_type, Instance) and rvalue_type.type.fullname == "builtins.str":
elif (
self.msg.errors.is_error_code_enabled(codes.STR_UNPACKING)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this included in --strict, and should it be?

elif (
self.msg.errors.is_error_code_enabled(codes.STR_UNPACKING)
and isinstance(rvalue_type, Instance)
and rvalue_type.type.fullname == "builtins.str"
Copy link
Member

@emmatyping emmatyping Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this check unpacking on Literal strings? Can you add a testcase for that please?

@bfontaine
Copy link

Any idea when this could be merged?

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Nov 29, 2025

This is superseded by #20325 , which also fixes type propagation

The new PR keeps this enabled by default and addresses Jukka's feedback.

Emma's feedback relates to preexisting behaviour in mypy. We do issue a warning there, but there is an unrelated improvement to be made to unpacking of Literals, which I will make in a separate PR

hauntsaninja added a commit that referenced this pull request Dec 2, 2025
Resolves #13823. Supersedes #15511. See also #6406

To avail of this, try `--disable-error-code str-unpack`
hauntsaninja added a commit to hauntsaninja/mypy that referenced this pull request Dec 2, 2025
The extra condition that excluded LiteralType was introduced in python#14827
I see no particular reason to have an instance check at all

I was looking at this because of this comment from Emma
python#15511 (comment)
Previously we errored with `"Literal['xy']" object is not iterable`
which is of course totally false

Now I issue the same error as in the str case, but restrict to cases
where the unpack length does not match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow string unpacking again.

4 participants