facebookincubator / cinder

Cinder is Meta's internal performance-oriented production version of CPython.
https://trycinder.com
Other
3.49k stars 121 forks source link

Overriding a method with a field raises an unexpected error #47

Closed LuKuangChen closed 3 years ago

LuKuangChen commented 3 years ago

What version of Static Python are you using?

4f1b313 2021-07-27

What program did you run?

# override_instance_method_with_field.py
class C:
    def x(self):
        pass
class D(C):
    x: int

What happened?

The program raised a seemly irrelevant error. AttributeError: 'ContainerTypeRef' object has no attribute 'module'

What should have happened?

We expected a compile-time type error complaining that one cannot override a method with a field. Actually, the trace back, especially the second line of the following quote, shows that StaticPython was trying to report such an error.

    f"class cannot hide inherited member: {value!r}"
  File "/vol/Lib/compiler/static/types.py", line 2245, in __repr__
    return f"<{self.name} '{self.name}' instance, args={self.args}>"
  File "/vol/Lib/compiler/static/types.py", line 1415, in __repr__
    f"<Parameter name={self.name}, ref={self.type_ref}, "
  File "/vol/Lib/compiler/static/types.py", line 260, in __repr__
    return f"TypeRef({self.module.name}, {ast.dump(self.ref)})"
AttributeError: 'ContainerTypeRef' object has no attribute 'module'
DinoV commented 3 years ago

I think you might get a different error after a6a0dcbc588de4cea63e8d20d976bfda0fd7a55a - ContainerTypeRef needed to override __repr__, I'm hoping you'll get a syntax error here with the string in the stack trace "class cannot hide inherited member: "

LuKuangChen commented 3 years ago

Thanks, I confirmed that the bug has been fixed. Here is the new error.

compiler.static.errors.TypedSyntaxError: class cannot hide inherited member: <function __main__.C.x 'function __main__.C.x' instance, args=[<Parameter name=self, ref=ContainerTypeRef(<function __main__.C.x 'function __main__.C.x' instance, args=[...]>), index=0, has_default=False>]>