Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This weird
__init__is to make sure that if the class has been already initialized in__new__, it is not initialized again.If this is not made, multiple things can happen:
_build_balanced_treebuild anOrinstance correctly, Python would call againOrinitializer with the original parametersleftandrightresulting inOr(left, right), ignoring the createdOrinstance from_build_balanced_treeAlwaysTrueit will get overriden and the instance will be initialized asOr(left, right)AlwaysFalseit will get overriden and the instance will be initialized asOr(left, right)I do not know why exactly but it gotta be something from Pydantic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spent several hours debugging this, and it is pretty weird. Pydantic seems not to work well with
__new__. I came up with the following:I see what direction you were going, but if we leave out the
obj.left = leftthen we don't have the attributes and we have an emptyOrobject. The test is still failing, but I think that's because the UnaryPredicates are not yet serializable. If you update the tests to use LiteralPredicates, then it should work 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot test it now, but I am pretty sure that I left out
obj.left = leftbecause of how Python works with__new__. If you return a class of the same type that happens:So that is why I purposely left the object uninitialized, so that when the raw obj is returned it is initialized by
__init__.If the object has already being initialized by
_build_balanced_treeI just leave it as is.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thing, the tests are passing in my implementation, as far as I know, so you know that Or is being instantiated correctly, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Earlier today I had failing tests, but I was still at an older commit of the branch. Let me check again