Skip to content

👌 Improve signature and type annotations for SphinxLoggerAdapter.warning - #12154

Merged
chrisjsewell merged 10 commits into
sphinx-doc:masterfrom
chrisjsewell:add-typing-to-logger.warning
Mar 21, 2024
Merged

chrisjsewell merged 10 commits into
sphinx-doc:masterfrom
chrisjsewell:add-typing-to-logger.warning

Conversation

@chrisjsewell

Copy link
Copy Markdown
Member

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 👌

image

@danieleades danieleades left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest this should be closed, as there are already upstream type annotations available

Comment thread sphinx/util/logging.py Outdated
def handle(self, record: logging.LogRecord) -> None:
self.logger.handle(record)

if TYPE_CHECKING:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are changing the signature of the method, I would suggest an explicit addition which calls the super method.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

https://github.com/python/typeshed/blob/3d26992d1562a5175fb8fd91ca63817e55feee56/stdlib/logging/__init__.pyi#L396-L405

@picnixz picnixz Mar 21, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]).

@danieleades danieleades Mar 21, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because we are documenting keyword arguments that do not exist upstream

surely that means the upstream type annotations need to be improved?

@chrisjsewell chrisjsewell Mar 21, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are changing the signature of the method, I would suggest an explicit addition which calls the super method.

done

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@chrisjsewell chrisjsewell Mar 21, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread sphinx/util/logging.py Resolved
Comment thread sphinx/util/logging.py Outdated
def handle(self, record: logging.LogRecord) -> None:
self.logger.handle(record)

if TYPE_CHECKING:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are changing the signature of the method, I would suggest an explicit addition which calls the super method.

@chrisjsewell

chrisjsewell commented Mar 21, 2024 •

Copy link
Copy Markdown
Member Author

suggest this should be closed, as there are already upstream type annotations available

I feel you have misunderstood the purpose; these are type annotation specific to the SphinxLoggerAdapter (I've updated the PR title to reflect this)

@chrisjsewell chrisjsewell changed the title 👌 Add type annotations for logger.warning 👌 Improve signature and type annotations for SphinxLoggerAdapter.warning Mar 21, 2024
Comment thread sphinx/util/logging.py Resolved
Co-authored-by: Bénédikt Tran <[email protected]>
Comment thread sphinx/util/logging.py Resolved
Comment thread sphinx/util/logging.py Resolved
chrisjsewell and others added 2 commits March 21, 2024 14:26
@chrisjsewell
chrisjsewell merged commit ace9d97 into sphinx-doc:master Mar 21, 2024
@chrisjsewell
chrisjsewell deleted the add-typing-to-logger.warning branch March 21, 2024 14:41
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 21, 2024
@AA-Turner AA-Turner added this to the 7.3.0 milestone Jul 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants