Open AntonLydike opened 1 year ago
Hmm yeah that is nasty. I guess it comes from the dataclass decorator on operation...
This particular problem should be solved, once we actually move to using the init constructor for operations, but it is certainly good to keep track here :)
As a quick fix, one could certainly add some verification to the post_init of Operation. At least that is what I would do :)
I'm confused that the dataclass
operator even allows assigning a 1
to something annotated Block | None
...
Welcome to Python!
I personally genuinely think there will be a moment where I will be annoyed enough by annotations not being enforced that I will hack into the python interpreter and give it an execution model that adds assertions on the types at the start of all functions with type hints. But that day is not today.
Suggestion to solve this:
Add a __post_init__
method to the Operation
class that checks that the object was initialized correctly. This should be relatively straight forward, so I'm adding the good first issue label to this.
Hey all, I have encountered this issue often enough to know what's the problem when I see this error message, but for beginners it's probably devastating:
We should probably make sure that the
__init__
of an Operation fails when it is constructed in such a way.Explanation for everyone who's confused by this:
arith.Constant
Operation (the constructor, lol). We should instead use some of the static methods attached to the class such asfrom_int_and_width
.__init__
method with the wrong arguments doesn't actually raise an error though. The first argument (1
) will be assigned to the.parent
attribute of the class though.parent
is not None, and therefore the code thinks we are adding something that is already added, when in fact we have erroneously constructed the operation.