-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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 bugged singleton implementation #32218
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.
Oh, very interesting. TIL!
looks like we had "ALMOST singletons"
It is unclear to me why a metaclass is needed. From what I can tell this can be most simply solved by calling |
I think there are various approaches, I actually like the metaclass approach, it's cleaner and it also allows to avoid duplication. |
I also really like the explicitness of it. Previously we had to make a comment "Hey this class is singleton". now we do not have to - it's obvious from the class definition that it's singleton. |
And we have a test covering the expected behaviour as well. I see only positives. |
26371cc
to
79e1c46
Compare
Interestingly - seems that this approach has an interesting (apparently unrelated) side-effect of the www test failing. I wonder what's the reason ? |
@potiuk Trying to debug this, but this is totally foreign part of Airflow for me 🙂 |
Can you show what your code would look like @uranusjr (i.e. code for your |
I am not sure I am familiar with it, but well might be time to excercise some puzzle solving skills :) |
Fix to the failing tests here: #32248 Very interesting edge-case. I think it has not been triggered before because effectively singleton was not really used and each entry was only accessed once. With fixing the singleton, the "invalid" entry was accessed twice during the same test and the second time it returned functools.partial object instead of None. |
Signed-off-by: Maciej Obuchowski <[email protected]>
I rebased it after merging the fix in #32248 |
Signed-off-by: Maciej Obuchowski <[email protected]>
3235dcf
to
9487fbc
Compare
Signed-off-by: Maciej Obuchowski <[email protected]>
* fix bugged singleton implementation Signed-off-by: Maciej Obuchowski <[email protected]> (cherry picked from commit e2e707c)
Singleton implementation using overloaded
__new__
did not work - it always called init on instance creation.But it is an cls instance in this case.
This changes it to metaclass-based approach that does not have this bug.
I would prefer to have singletons implemented as modules, as in some other places, but in this case those are already classes - I think metaclass based approach it does not change their API while fixing the issue.