JSAbrahams / mamba

🐍 The Mamba programming language, because we care about safety
MIT License
85 stars 3 forks source link

If class field not assigned to in Python constructor, should be nullable #317

Open JSAbrahams opened 2 years ago

JSAbrahams commented 2 years ago

Description of Bug

If class field not assigned to in Python constructor, it should be nullable.

How to Reproduce

If we have have class:

class MyClass:
    a
    def __init__(self): pass

When building a Context, we have a class MyClass with field a, which is an integer.

Expected behavior

Field a should be type Optional[Int], not Int. This is because it is never actually assigned to.

We could also just default to making it optional if it is never assigned to at the top level, of course. Then afterwards we can do a check within the constructor to see if it is assigned to.

Additional context

This is tied to #311 , if we have implemented that logic, we can also use that to check whether class fields are actually assigned to. If not, then we make it optional (if it is not already optional)

JSAbrahams commented 1 year ago

In general we need to think about how we call unsafe python code. See #160

JSAbrahams commented 1 year ago

Actually in the above, this is not valid Python code. Instead of adding the field to the class, the field should have no field a. It should only be if: