facebookincubator / cinder

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

unsound static-to-static field overrides #39

Closed LuKuangChen closed 2 years ago

LuKuangChen commented 2 years ago

What version of Static Python are you using?

9965302 2021-07-15

What program did you run?

# override_field_incompatible_type.py
from typing import ClassVar

class C:
    x: ClassVar[int] = 0

class D(C):
    x: ClassVar[str] = "0"

# Interestingly, `D.x = "hello"` raises a compile-time error
# 
#     compiler.static.TypedSyntaxError: type mismatch: Exact[str] 
#     cannot be assigned to int

What happened?

The program terminates without errors.

What should have happened?

We expected a compile-time error because D override the class variable x with an incompatible type.

LuKuangChen commented 2 years ago

We noticed that instance fields have a similar issue. The following program passes type check. If the last line is uncommented, a compile-time error will be reported. This shows that the x: str in the subclass D is ignored. It might be more sensible to error at the re-declaration, instead of to ignore it silently.

class C:
    x: int

class D(C):
    x: str

# D().x = "foo" # compile-time type error. int is expected.
DinoV commented 2 years ago

Looks like this got fixed with commit 1475ebd1701851cd3e7f6bda6c3b3ce7404953d0