You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Documented pattern for adding contextual information to LogRecord objects fails type checking #7833
However, if you use this pattern and run mypy, you'll get an error like error: "LogRecord" has no attribute "custom_attribute". You can get around this error by using setattr(record, "custom_attribute", 0xdecafbad) instead of record.custom_attribute = 0xdecafbad.
Should the stub for the LogRecord class define a __setattr__ method because this sort of usage of assigning to arbitrary attributes is expected for the LogRecord class? Would that have undesirable side effects?
I think adding __setattr__ is a good idea. You would still get errors if you try to get the values of custom attributes, but I don't think you need to do that very often, because usually you just add the custom attributes to your format string.
I also don't think __setattr__ would introduce many false negatives (i.e. wrong code passing the type check). If you are setting an attribute on a LogRecord, it's much more likely to be a custom attribute than a misspelled non-custom attribute.
Activity
Akuli commentedon May 12, 2022
I think adding
__setattr__is a good idea. You would still get errors if you try to get the values of custom attributes, but I don't think you need to do that very often, because usually you just add the custom attributes to your format string.I also don't think
__setattr__would introduce many false negatives (i.e. wrong code passing the type check). If you are setting an attribute on aLogRecord, it's much more likely to be a custom attribute than a misspelled non-custom attribute.Remove unused ignore in logutils
Remove unused ignore in logutils