-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
👌 Add CoreEvent
enum as argument for Sphinx.callback
#12259
Conversation
Yay typesafety! |
CoreEvent
enumCoreEvent
enum as argument for Sphinx.callback
@@ -64,8 +171,10 @@ def add(self, name: str) -> None: | |||
raise ExtensionError(__('Event %r already present') % name) | |||
self.events[name] = '' | |||
|
|||
def connect(self, name: str, callback: Callable, priority: int) -> int: | |||
def connect(self, name: str | CoreEvent, callback: Callable, priority: int) -> int: |
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.
we could take this further and make the CoreEvent
enum the preferred approach (without breaking existing code)
def connect(self, name: str | CoreEvent, callback: Callable, priority: int) -> int: | |
@deprecated(reason="...") | |
@overload | |
def connect(self, name: str, callback: Callable, priority: int) -> int: ... | |
@overload | |
def connect(self, name: CoreEvent, callback: Callable, priority: int) -> int: ... | |
def connect(self, name: str | CoreEvent, callback: Callable, priority: int) -> int: |
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.
The deprecated decorator is only a 3.13 feature :(
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.
(In addition, you still want to connect with your own events)
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.
I think it's fine but the issue is that people would need to import the events module if they want to use the enumeration. So you should put it in a standalone module to avoid possible circular imports.
On the other hand, you could instead use Literal strings maybe? it could probably help if you want overloads since enumerations + literals are not happy together sometimes (I had an issue with that but I don't remember whether it's fixed or not).
We've significantly improved typing and documentation for A |
Would it solve the issue if we could use |
When creating sphinx extensions (or looking at existing ones), it is always a source of annoyance/confusion trying to remember what callback events are available, what their purpose is, and also what their signatures should be.
Here I add the
sphinx.events.CoreEvent
enum, and allow this to be passed toSphinx.connect
, as an alternative to astr
name.This makes it easier to write and introspect extensions, and also more type safe.
I could also add
@overload
functions to theEventManager.callback
, with specific function signatures for each of the items,but I will wait to get feedback first.
It would also be interesting to consider using this to "auto-generate" some of https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx-core-events, but that is likely beyond the scope of this PR.
(I am definitely a fan of co-locating API documentation close to where it is implemented 😄 )