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

Using TypedDict in function triggers AssertionError during compilation #869

Closed
twoertwein opened this issue Jun 8, 2021 · 1 comment · Fixed by python/mypy#14747
Closed
Assignees
Labels
bug crash Failing compile-time asserts, unhandled exceptions, or generated C code that's blowing up. priority-0-high

Comments

@twoertwein
Copy link

twoertwein commented Jun 8, 2021

from typing import TypedDict

class Movie(TypedDict):
    name: str
    year: int

def test(dictionary: Movie) -> None:
    for key, value in dictionary.items():
        print(key, value)

with mypy 0.900 and python 3.9

$ mypyc typed_dict.py
Traceback (most recent call last):
  File "mypyc/irbuild/builder.py", line 169, in accept
  File "mypy/nodes.py", line 1140, in accept
  File "mypyc/irbuild/visitor.py", line 148, in visit_for_stmt
  File "mypyc/irbuild/statement.py", line 252, in transform_for_stmt
  File "mypyc/irbuild/for_helpers.py", line 60, in for_loop_helper
  File "mypyc/irbuild/for_helpers.py", line 387, in make_for_loop_generator
  File "mypyc/irbuild/builder.py", line 719, in get_dict_item_type
  File "mypyc/irbuild/builder.py", line 711, in get_dict_key_type
  File "mypyc/irbuild/builder.py", line 705, in get_dict_base_type
typed_dict.py:8: AssertionError: 
@bibajz
Copy link

bibajz commented Sep 15, 2021

Hello, I can confirm this error is still present on

Python 3.9.7
mypy 0.920+dev.d263a9698fb59d7a1ae0c7817ef53bb272c8d875

Here is the full traceback:

Traceback (most recent call last):
  File "build_dir/py_plus_c/build/setup.py", line 5, in <module>
    ext_modules=mypycify(['typed_dict.py'], opt_level="3"),
  File "build_dir/mypy/mypyc/build.py", line 485, in mypycify
    groups, group_cfilenames = mypyc_build(
  File "build_dir/mypy/mypyc/build.py", line 398, in mypyc_build
    group_cfiles, ops_text = generate_c(all_sources, options, groups, fscache,
  File "build_dir/mypy/mypyc/build.py", line 200, in generate_c
    modules, ctext = emitmodule.compile_modules_to_c(
  File "build_dir/mypy/mypyc/codegen/emitmodule.py", line 414, in compile_modules_to_c
    modules = compile_modules_to_ir(result, mapper, compiler_options, errors)
  File "build_dir/mypy/mypyc/codegen/emitmodule.py", line 258, in compile_modules_to_ir
    scc_ir = compile_scc_to_ir(trees, result, mapper, compiler_options, errors)
  File "build_dir/mypy/mypyc/codegen/emitmodule.py", line 209, in compile_scc_to_ir
    modules = build_ir(
  File "/usr/local/lib/python3.9/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "build_dir/mypy/mypyc/irbuild/main.py", line 82, in build_ir
    transform_mypy_file(builder, module)
  File "build_dir/mypy/mypyc/irbuild/main.py", line 125, in transform_mypy_file
    builder.accept(node)
  File "build_dir/mypy/mypyc/irbuild/builder.py", line 175, in accept
    node.accept(self.visitor)
  File "build_dir/mypy/mypy/nodes.py", line 696, in accept
    return visitor.visit_func_def(self)
  File "build_dir/mypy/mypyc/irbuild/visitor.py", line 116, in visit_func_def
    transform_func_def(self.builder, fdef)
  File "build_dir/mypy/mypyc/irbuild/function.py", line 69, in transform_func_def
    func_ir, func_reg = gen_func_item(builder, fdef, fdef.name, builder.mapper.fdef_to_sig(fdef))
  File "build_dir/mypy/mypyc/irbuild/function.py", line 306, in gen_func_item
    builder.accept(fitem.body)
  File "build_dir/mypy/mypyc/irbuild/builder.py", line 175, in accept
    node.accept(self.visitor)
  File "build_dir/mypy/mypy/nodes.py", line 1024, in accept
    return visitor.visit_block(self)
  File "build_dir/mypy/mypyc/irbuild/visitor.py", line 125, in visit_block
    transform_block(self.builder, block)
  File "build_dir/mypy/mypyc/irbuild/statement.py", line 46, in transform_block
    builder.accept(stmt)
  File "build_dir/mypy/mypyc/irbuild/builder.py", line 175, in accept
    node.accept(self.visitor)
  File "build_dir/mypy/mypy/nodes.py", line 1150, in accept
    return visitor.visit_for_stmt(self)
  File "build_dir/mypy/mypyc/irbuild/visitor.py", line 148, in visit_for_stmt
    transform_for_stmt(self.builder, stmt)
  File "build_dir/mypy/mypyc/irbuild/statement.py", line 254, in transform_for_stmt
    for_loop_helper(builder, s.index, s.expr, body,
  File "build_dir/mypy/mypyc/irbuild/for_helpers.py", line 60, in for_loop_helper
    for_gen = make_for_loop_generator(builder, index, expr, body_block, normal_loop_exit, line)
  File "build_dir/mypy/mypyc/irbuild/for_helpers.py", line 389, in make_for_loop_generator
    target_type = builder.get_dict_item_type(expr.callee.expr)
  File "build_dir/mypy/mypyc/irbuild/builder.py", line 761, in get_dict_item_type
    key_type = self.get_dict_key_type(expr)
  File "build_dir/mypy/mypyc/irbuild/builder.py", line 753, in get_dict_key_type
    dict_base_type = self.get_dict_base_type(expr)
  File "build_dir/mypy/mypyc/irbuild/builder.py", line 747, in get_dict_base_type
    assert isinstance(target_type, Instance)
typed_dict.py:8: AssertionError: 

I have also noted that wrapping the dictionary variable with dict makes the compilation successful:

from typing import TypedDict

class Movie(TypedDict):
    name: str
    year: int

def test(dictionary: Movie) -> None:
    for key, value in dict(dictionary).items():
        print(key, value)

@97littleleaf11 97littleleaf11 added crash Failing compile-time asserts, unhandled exceptions, or generated C code that's blowing up. priority-0-high labels Feb 23, 2022
@ichard26 ichard26 self-assigned this Feb 16, 2023
@ichard26 ichard26 added the bug label Feb 20, 2023
JukkaL pushed a commit to python/mypy that referenced this issue Mar 20, 2023
An optimization to make iterating over dict.keys(), dict.values() and
dict.items() faster caused mypyc to crash while compiling a TypedDict.
This commit fixes `Builder.get_dict_base_type` to properly handle
`TypedDictType`.

Fixes mypyc/mypyc#869.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug crash Failing compile-time asserts, unhandled exceptions, or generated C code that's blowing up. priority-0-high
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants