-
Notifications
You must be signed in to change notification settings - Fork 65
Component equality check refactor #2769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
1 file reviewed, no comments
I guess this is too strict, it causes some existing tests to fail. There could be other ways to approach this, ideally I shouldn't be addressing the very specific bug that caused it though ( |
I actually reworked the equality check on the pydantic v2 branch and it includes a similar type check: tidy3d/tidy3d/components/base.py Lines 1118 to 1164 in f8c1f78
tests are passing there. might be good to compare these and perhaps just update the implementation here too? |
I"m a little confused, so this should work >>> 1 == (4,3)
False what was the error exactly? What's a bit worrisome maybe about this is whether we want to eg consider a |
Without this change, it gets here and it enters that if statement because one of them is a tuple. But the other one is not and the error is on |
52e1ea6
to
394c79c
Compare
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
|
So this has been kind of annoying. A naive addition like
would then make some tests error on comparisons between a I used AI to port @yaugenst-flex 's pydantic v2 version. A direct port was failing on some tests because of the different handling of lits/numpy arrays. I've brought the changes to a state that works (using AI) but I don't know if we want to go for such a big change here, given that at some point we'll be getting pydantic v2 in. On the other hand, there might be something here that could improve the pydantic v2 implementation? I guess if we want to go with this, (A)I should add tests to complete the code coverage. |
This got buried @tylerflex @yaugenst-flex |
@momchil-flex the current implementation (with AI, that makes a lot of changes) actually scares me quite a bit. If it's consistent with what @yaugenst-flex is doing for pydantic v2 maybe that's fine. But perhaps we can just fix the failing case for now, add more tests, and then wait for the v2 merge to do this more deeply? Just think with the equality checking there is a lot of opportunity for subtle bugs so I'd feel a bit more conservative about this and my preference would be for one of the earlier iterations where the immediate bug is fixed. even if it's not as general as we'd like. Thoughts? |
Fix it how?
Take this approach but add even more logic? |
I'm talking with @yaugenst-flex in DM who's reassuring me that it's not AI changes specifically, I might have missed some context. Ultimately it comes down to @yaugenst-flex judgement I think. If there's a way to fix this in 1 line and then do these changes in Yannick's v2 PR maybe that's ideal? if not, I'm ok with what you two decide |
Hm, I think I see what you mean. I think the "proper" way to handle this might be to just declare |
so a minimal fix would probably be to flip i don't really feel strongly either way. i don't see a problem with switching to the new implementation (since it already exists on the v2 branch and will be merged anyways) but it's also not a big deal to just do a "surgical" fix here. |
I think I'd also prefer a surgical fix because your code couldn't be ported exactly (some tests would fail, probably because of pydantic version difference), so it will definitely conflict with the code here.
|
Another thing to try would be to just define one format that we want to perform equality checking on. For example, if it's a tuple of length 1, we just strip out the single element for equality checking? |
i'm relatively confident it should work, but yes the current implementation just lends itself to leading to some other strange errors 😄 |
when comparing tuples we should just use python's built-in |
generally I agree, except by allowing |
but |
Right, we don't want them to be equal. We just want it not to error. |
ah ok I misunderstood. |
6b40342
to
d8d4c8f
Compare
It does work for my test, let's see if all other tests also pass. |
This caused a validator error when comparing two semiconductor media since the
N_d
andN_a
values can be both a float and a tuple (so when one medium hadN_d
a float and the other - a tuple, it was erroring).Greptile Summary
This PR adds a type check to the component equality comparison function in
tidy3d/components/base.py
. The change addresses a specific bug where comparing two semiconductor media objects would fail with validator errors when they had different types for the same field (e.g., one havingN_d
as a float and another as a tuple).The fix introduces an early type check using
type(val1) is not type(val2)
that returnsFalse
immediately if the types don't match, preventing the comparison from proceeding to validation steps that would cause errors. This change integrates well with the existing equality comparison logic in the base component system, which is fundamental to how Tidy3D objects are compared throughout the codebase.The modification follows sound equality semantics - objects with different types for the same field should be considered unequal. This is a defensive programming approach that prevents downstream validation errors while maintaining logical correctness.
Important Files Changed
Files Modified
tidy3d/components/base.py
Confidence score: 4/5
Sequence Diagram