Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuple assignment and list * unpacking crashes mypy #3825

Closed
Sterbic opened this issue Aug 14, 2017 · 9 comments · Fixed by #11499
Closed

Tuple assignment and list * unpacking crashes mypy #3825

Sterbic opened this issue Aug 14, 2017 · 9 comments · Fixed by #11499

Comments

@Sterbic
Copy link

Sterbic commented Aug 14, 2017

Ran mypy over the following code:

a = [1, 2, 3]
first, *other = [*a, None]

Interestingly enough, first, *other = [None, *a] works correctly. Happy to give it a shot at fixing this myself is someone can give me some pointers on where to start.

Exception:

(.venv3) luka-mbp:typeshed luka$ mypy test.py --show-traceback
test.py:2: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.530-dev-82cb95cb2cf4680bde12a341ce0f4354c79b3dcd
Traceback (most recent call last):
  File "/Users/luka/Desktop/typeshed/.venv3/bin/mypy", line 11, in <module>
    load_entry_point('mypy===0.530-dev-82cb95cb2cf4680bde12a341ce0f4354c79b3dcd', 'console_scripts', 'mypy')()
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/main.py", line 97, in type_check_only
    options=options)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/build.py", line 196, in build
    graph = dispatch(sources, manager)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/build.py", line 1801, in dispatch
    process_graph(graph, manager)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/build.py", line 2044, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/build.py", line 2147, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/build.py", line 1716, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/checker.py", line 189, in check_first_pass
    self.accept(d)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/checker.py", line 277, in accept
    stmt.accept(self)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/nodes.py", line 859, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/checker.py", line 1236, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/checker.py", line 1263, in check_assignment
    infer_lvalue_type)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/checker.py", line 1505, in check_assignment_to_multiple_lvalues
    self.check_assignment(lv, rv, infer_lvalue_type)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/checker.py", line 1328, in check_assignment
    rvalue)
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/checker.py", line 1716, in infer_variable_type
    elif not is_valid_inferred_type(init_type):
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/checker.py", line 3146, in is_valid_inferred_type
    if is_same_type(typ, NoneTyp()):
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/sametypes.py", line 28, in is_same_type
    return left.accept(SameTypeVisitor(right))
  File "/Users/luka/Desktop/typeshed/.venv3/lib/python3.6/site-packages/mypy/types.py", line 1033, in accept
    assert isinstance(visitor, SyntheticTypeVisitor)
AssertionError:
test.py:2: note: use --pdb to drop into pdb
@emmatyping
Copy link
Collaborator

Based on first, *other = [None, *a] working correctly I'm guessing with first, *other = [*a, None] we are trying to unpack *a into first, and None into *other, which is wrong.

@ilevkivskyi
Copy link
Member

(Bumping priority to high since this appeared again in the context of mypyc)

@refi64
Copy link
Contributor

refi64 commented Jul 8, 2019

I think the cop-out way to fix this would be to have check_assignment_to_multiple_lvalues not recurse into a ListExpr/TupleExpr if the rhs has a star expression, or maybe as more of a compromise, check the lvalues up to the right-hand-side's star individually, then check the rest via check_multi_assignment.

@goodmami
Copy link
Contributor

I came across this error when working on #7779. I think both issues will be fixed by the same solution.

@refi64 I don't think getting the index of the starred expression will work the same as it does for lvalues, because the star does really different things on an lvalue vs an rvalue, and there can be more than one:

a = (1, 2, 3)
first, *other = *a, 4        # mypy: crash
print(other)                 # python: [2, 3, 4]
first, *other = *(*a, 4), 5  # mypy: crash
print(other)                 # python: [2, 3, 4, 5]
first, *other = *a, *a       # mypy: crash
print(other)                 # python: [2, 3, 1, 2, 3]

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 9, 2020

Another similar looking example reported in #8635:

a, b = *(1, 2)

@arseniiv
Copy link

This ends up in an internal error too:

a, b = *(1,), *(2,)

Maybe it would be useful to add this one too to future tests to be completely sure.

(Just ran into this issue for the first time and ended up with this version. Good luck brave fixers!)

@Askaholic
Copy link

Just ran into this issue on 0.8.12 trying to do essentially this:

tup = (1, (2, 3))
a, b = *tup

Looks like flake8 does not pick up on this being a syntax error.

hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue Nov 8, 2021
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 python#9137
Fixes python#3825 (most of the reports in this issue were fixed by python#8827)
@thejcannon
Copy link
Contributor

And *foo = [1, 2, 3] as well

@hauntsaninja
Copy link
Collaborator

Most of the reports in this issue were fixed by #8827

We'll use #9137 to track remaining issue, including thejcannon's example

hauntsaninja added a commit that referenced this issue Nov 15, 2022
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 <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.