mypyc / mypyc

Compile type annotated Python to fast C extensions
1.74k stars 46 forks source link

Support inner / nested classes #897

Open GBeauregard opened 2 years ago

GBeauregard commented 2 years ago

Using latest mypy git (mypy 0.920+dev.df827c9d8926155b5a47d44f02a02b39823975f5) on Linux with gcc 11.2.0 and Python 3.9 the following fails to compile giving ../nesting.py:2: error: Unsupported statement in class body

class Outer:
    class Inner:
        pass

Presumably this is entirely known, but I couldn't find any mention of it in the documentation or any existing issue so I thought I'd open one.

dosisod commented 1 year ago

Related, Mypyc crashes if Outer is replaced with a function instead of a class:

def f() -> None:
    class C:
        pass
$ mypyc x.py
Traceback (most recent call last):
  File "mypy/build/setup.py", line 5, in <module>
    ext_modules=mypycify(['x.py'], opt_level="3", debug_level="1"),
  File "mypy/mypyc/build.py", line 502, in mypycify
    groups, group_cfilenames = mypyc_build(
  File "mypy/mypyc/build.py", line 412, in mypyc_build
    group_cfiles, ops_text = generate_c(
  File "mypy/mypyc/build.py", line 220, in generate_c
    modules, ctext = emitmodule.compile_modules_to_c(
  File "mypy/mypyc/codegen/emitmodule.py", line 425, in compile_modules_to_c
    modules = compile_modules_to_ir(result, mapper, compiler_options, errors)
  File "mypy/mypyc/codegen/emitmodule.py", line 269, in compile_modules_to_ir
    scc_ir = compile_scc_to_ir(trees, result, mapper, compiler_options, errors)
  File "mypy/mypyc/codegen/emitmodule.py", line 225, in compile_scc_to_ir
    modules = build_ir(scc, result.graph, result.types, mapper, compiler_options, errors)
  File "/usr/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "mypy/mypyc/irbuild/main.py", line 95, in build_ir
    transform_mypy_file(builder, module)
  File "mypy/mypyc/irbuild/main.py", line 140, in transform_mypy_file
    builder.accept(node)
  File "mypy/mypyc/irbuild/builder.py", line 261, in accept
    node.accept(self.visitor)
  File "mypy/mypy/nodes.py", line 789, in accept
    return visitor.visit_func_def(self)
  File "mypy/mypyc/irbuild/visitor.py", line 179, in visit_func_def
    transform_func_def(self.builder, fdef)
  File "mypy/mypyc/irbuild/function.py", line 98, in transform_func_def
    func_ir, func_reg = gen_func_item(builder, fdef, fdef.name, builder.mapper.fdef_to_sig(fdef))
  File "mypy/mypyc/irbuild/function.py", line 314, in gen_func_item
    builder.accept(fitem.body)
  File "mypy/mypyc/irbuild/builder.py", line 261, in accept
    node.accept(self.visitor)
  File "mypy/mypy/nodes.py", line 1219, in accept
    return visitor.visit_block(self)
  File "mypy/mypyc/irbuild/visitor.py", line 188, in visit_block
    transform_block(self.builder, block)
  File "mypy/mypyc/irbuild/statement.py", line 114, in transform_block
    builder.accept(stmt)
  File "mypy/mypyc/irbuild/builder.py", line 261, in accept
    node.accept(self.visitor)
  File "mypy/mypy/nodes.py", line 1138, in accept
    return visitor.visit_class_def(self)
  File "mypy/mypyc/irbuild/visitor.py", line 167, in visit_class_def
    transform_class_def(self.builder, cdef)
  File "mypy/mypyc/irbuild/classdef.py", line 88, in transform_class_def
    ir = builder.mapper.type_to_ir[cdef.info]
x.py:2: KeyError: <TypeInfo x.C@2>

Version:

$ mypyc --version
mypy 1.1.0+dev.786c7b07040515ccdc440005b8ff054fc6c0ebce (compiled: no)
dosisod commented 1 year ago

Just saw that there is already an issue for this: https://github.com/mypyc/mypyc/issues/864