-
Notifications
You must be signed in to change notification settings - Fork 214
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 IndexError
in override_log_formatter_context
utility
#4873
Fix IndexError
in override_log_formatter_context
utility
#4873
Conversation
In the `aiida.common.log.override_log_formatter_context` utility function, the formatting of all current AiiDA handlers is temporarily changed for the duration of the yield, and then reset. The problem is that the function assumed that the number of handlers would not change, which is not necessarily the case. Additionally, it also relied on the order of the handlers, which isn't guaranteed either. The solution is to simply cache the handlers' formatters as a dictionary and directly use those handler references to reset the cached formatter. Note that we copy the `handler.formatter` return value because it may be returned by reference and we want to keep the original formatter if it is changed during the yield. Note also that it doesn't matter that the AiiDA logger may have gained additional handlers during the yield, after we recorded the cached formatters, but that is ok, because we didn't temporarily change their formatter either.
@chrisjsewell interested to hear why you chose for your original implementation even though you were aware of this potential problem, as evidenced by your note in the docstring. |
Ah apologies, in my git loggage it looked like you had committed that. Well I will pass on the question to @CasperWA in that case 😅 |
I don't know if this was the reason, but one issue that comes to mind is that if the handlers change then what is the "definition" of setting them back? I'm not familiar enough with this design to make any confident assertion of conceptual problems, but some ideas:
From an ignorant perspective, it seems that we are leaving alone any handler that got "created" in the middle but we are re-creating any handler that was "eliminated" during the yield, which is not the consistency I would expect at least. (EDIT: again, this is an impression I get but I don't know the details of the implementation. Thus, if they are more familiar with this class, I think it'll be better if either @chrisjsewell or @CasperWA approve this PR) |
Codecov Report
@@ Coverage Diff @@
## develop #4873 +/- ##
===========================================
+ Coverage 79.62% 79.62% +0.01%
===========================================
Files 519 519
Lines 37110 37111 +1
===========================================
+ Hits 29544 29545 +1
Misses 7566 7566
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
We are not recreating any handlers actually, we are just temporarily changing the formatters. You can consider the So the behavior after this fix is correct as far as I am concerned. We take existing handlers, temporarily change their behavior, and afterwards revert that. Whether they are still in use by the same logger or not after the yield is irrelevant. |
Anyway, whatever we do. This blocks the v1.6.2 release so we cannot let this linger around for too long |
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.
Let's never talk of this again - go go go
(I think it was an issue of lazyness, lost overview, and a rogue simultaneous rewriting of some logging logic (not by me, though) during the specific PR that led to this poor choice of mine - but I might definitely be wrong - thanks for fixing it, though! ❤️ ).
... although maybe there should be a test for this one? ... 😅 |
Thanks for the explanation! Can this still be consider as an "activation/deactivation" of the handler through the inclusion on that list? It might be weird if I deactivate some handler at some point and I activate it again and it has a different format? |
I'm not exactly sure what you mean here, but this should - as @sphuber also described - just change the formatting, temporarily for the provided loggers for the entirety of the decorated function, and then revert them to their original formatting afterwards. The provided loggers here, being all the loggers present in the Edit: And just to be clear. Nothing is "activated/deactivated". The formatting, i.e., how the logs are written/displayed is the only thing that is temporarily changed. I believe this is implemented to make the |
Mmm ok, then perhaps the problem is that I don't fully understand the purpose of the |
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.
Anyway, whatever we do. This blocks the v1.6.2 release so we cannot let this linger around for too long
I'll approve this so my questions to understand this feature are not mistaken for objections to moving forward. As I said, I would have preferred the final greenlight came from someone more familiar with the class, but from @CasperWA and @chrisjsewell comments I assume they are giving implicit approval and may not have done so explicitly not to cut the ongoing discussion. If you understand the same @sphuber , feel free to merge, the discussion can continue independently of this.
Classic xD |
Like who? It was @CasperWA who wrote the function. Think it is fine to merge this as it fixes the immediate problem. Happy to explain this some other time, preferably face to face over zoom, because in writing takes too much time. |
I totally missed that he already had approved the PR.
I'll probably try to bring it up in the maintainers meeting tomorrow at 11 to see if Chris can explain this to me, but would very much welcome you there if you have 15 mins. |
Fixes #4872
In the
aiida.common.log.override_log_formatter_context
utilityfunction, the formatting of all current AiiDA handlers is temporarily
changed for the duration of the yield, and then reset. The problem is
that the function assumed that the number of handlers would not change,
which is not necessarily the case. Additionally, it also relied on the
order of the handlers, which isn't guaranteed either.
The solution is to simply cache the handlers' formatters as a dictionary
and directly use those handler references to reset the cached formatter.
Note that we copy the
handler.formatter
return value because it may bereturned by reference and we want to keep the original formatter if it
is changed during the yield. Note also that it doesn't matter that the
AiiDA logger may have gained additional handlers during the yield, after
we recorded the cached formatters, but that is ok, because we didn't
temporarily change their formatter either.