-
Notifications
You must be signed in to change notification settings - Fork 49
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
fix: fix name collisions between dataclass fields and guiclass container attributes #688
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #688 +/- ##
==========================================
- Coverage 88.86% 88.85% -0.02%
==========================================
Files 39 40 +1
Lines 4761 4809 +48
==========================================
+ Hits 4231 4273 +42
- Misses 530 536 +6 ☔ View full report in Codecov by Sentry. |
@hanjinliu, could you have a look at the magic-class error and see what i've done here that is selectively breaking magic-class? thanks! |
Hi @tlambert03 , I found that the older version and this version returns inconsistent result when a Container widget has multiple widgets with the same name. from magicgui import __version__
from magicgui.widgets import Container, LineEdit
c = Container()
c.append(LineEdit(name="a", value="1"))
c.append(LineEdit(name="a", value="2"))
c["a"].value This code returns |
ok, that makes sense... it's caused by this change: I'd say that the implicit expectation of getting the first matching widget in the case of a name collision rather than the last matching widget was probably always more bug-like and not any sort of formal protocol here, but if that's all it takes not to break magic-class I can go back to that behavior. Perhaps after this PR, you can also have a look at places where you have behavior (either explicit or implicit) that might change depending on whether someone has added two widgets with the same In the case of a dataclass (which precipitated this PR), i think it is safe to assume that there will never be two widgets with the same name, but that is probably a unique case |
Yes, I definitely have to avoid adding widget of the same name on my side. |
fixes #684
When a container has a widget whose
name
attribute matches an attribute on theWidget
class itself, then the overloaded__getattr__
method becomes a problem. (reminder: that getattr is what makes it possible to retrieve a magicgui function parameter (for example) as an attribute on the magicgui container widget).Here I add a
get_widget(self, name:str)
method on Container that is guaranteed to either return a widget instance or None, avoiding conflicts with other attributes no the Widget instance itself