Repository navigation
👌 Improve signature and type annotations for SphinxLoggerAdapter.warning - #12154
Conversation
danieleades
left a comment
There was a problem hiding this comment.
suggest this should be closed, as there are already upstream type annotations available
| def handle(self, record: logging.LogRecord) -> None: | ||
| self.logger.handle(record) | ||
|
|
||
| if TYPE_CHECKING: |
There was a problem hiding this comment.
this is a bit of a weird pattern.
Also the type annotations for this method are already available in typeshed, so should already be available to your IDE
can confirm that vscode with default python plugins shows type annotations for the warning method for me
There was a problem hiding this comment.
Since you are changing the signature of the method, I would suggest an explicit addition which calls the super method.
There was a problem hiding this comment.
yeah i'm a bit confused as to the intention of this change.
I'm guessing since it has no method body that it's meant to just be providing a kind of hacky 'type stub', is that correct?
if that's the case than any deviation from the upstream type stubs should be treated as a bug.
but this is all redundant anyway, because there are already type annotations in typeshed:
There was a problem hiding this comment.
I'm guessing since it has no method body that it's meant to just be providing a kind of hacky 'type stub', is that correct?
Yes.
but this is all redundant anyway, because there are already type annotations in typeshed:
Actually, it's not because of a bug. It's because we are documenting keyword arguments that do not exist upstream or remove the 'extra' keyword (hence the # type: ignore[override]).
There was a problem hiding this comment.
It's because we are documenting keyword arguments that do not exist upstream
surely that means the upstream type annotations need to be improved?
There was a problem hiding this comment.
this is a bit of a weird pattern.
This trick is taken from sqlalchemy which, IMO, is one of the best python libraries out there 😉
https://github.com/sqlalchemy/sqlalchemy/blob/7c70ab8c6b7b9ce1c566862c4ca0438e0b0e9131/lib/sqlalchemy/ext/mutable.py#L797
yeah i'm a bit confused as to the intention of this change.
this is all redundant anyway, because there are already type annotations in typeshed
the point is that the upstream type just chucks everything into kwargs, and has no knowledge of the specific adapter key-word arguments that are actually used for Sphinx warnings.
This makes the current type absolutely useless when trying to write sphinx warnings, providing none of the information you actually need to make a good warning (like location, type, subtype).
The amount of extensions I see omit these and end up having poor warnings is not good
There was a problem hiding this comment.
Since you are changing the signature of the method, I would suggest an explicit addition which calls the super method.
done
There was a problem hiding this comment.
Actually, I was expecting you to remove the TYPE_CHECKING (if you keep it, no need for a super call, but I'd prefer not having a TYPE_CHECKING in the middle of the file).
There was a problem hiding this comment.
Actually, I was expecting you to remove the TYPE_CHECKING (if you keep it, no need for a super call, but I'd prefer not having a TYPE_CHECKING in the middle of the file).
ok done 👍
the TYPE_CHECKING was a"cute" trick, to make it a little less "invasive",
but at the end of the day this is the "correct" signature for sphinx warnings,
as I hope you'll agree.
(The explicit exc_info, stack_info, stacklevel, extra key-word arguments of the "upstream" type are not practically useful/good to document, since they are not actually used by sphinx)
There was a problem hiding this comment.
I think I would even go so far as to add this in the docstring, if you agree?
.. tip::
It is recommended to always include a ``type`` and ``subtype`` for your warning.
These can be displayed to the user, using the :confval:`show_warning_types` configuration,
and allow for suppression of specific warning, using the :confval:`suppress_warnings` configuration.
It is also recommended to include a ``location``, where possible,
to aide users in correcting the warning.| def handle(self, record: logging.LogRecord) -> None: | ||
| self.logger.handle(record) | ||
|
|
||
| if TYPE_CHECKING: |
There was a problem hiding this comment.
Since you are changing the signature of the method, I would suggest an explicit addition which calls the super method.
I feel you have misunderstood the purpose; these are type annotation specific to the |
logger.warningSphinxLoggerAdapter.warning
Co-authored-by: Bénédikt Tran <[email protected]>
Co-authored-by: danieleades <[email protected]>
Co-authored-by: danieleades <[email protected]>
this is something that has always annoyed me when writing extensions: you get no editor help / type checking when writing logger warnings.
Now you get a lovely detailed docstring / type annotations 👌