Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MNT: Define Protocol for Animation.event_source - #30590

Merged
QuLogic merged 1 commit into
matplotlib:mainfrom
timhoffm:eventsource
Oct 2, 2025
Merged

MNT: Define Protocol for Animation.event_source#30590
QuLogic merged 1 commit into
matplotlib:mainfrom
timhoffm:eventsource

Conversation

@timhoffm

Copy link
Copy Markdown
Member

Also, event_source is a mandatory parameter and cannot be None, because Animation.init calls event_source.add_callback().

@timhoffm timhoffm added topic: typing API: consistency Consistency of the matplotlib API, including naming, behavior, defaults, … and removed topic: animation labels Sep 20, 2025
@timhoffm

Copy link
Copy Markdown
Member Author

mypy-stubtest complains with

matplotlib.animation.EventSourceProtocol is not present at runtime

I tought it would be sufficient to define this for type checking. Does mypy-stubtest need runtime definitions? If so does this mean, I have to define the protocol twice, once in the .py file and once in the pyi. file?

@QuLogic

QuLogic commented Sep 24, 2025

Copy link
Copy Markdown
Member

Yes, stubtest checks that both are in sync, so it requires something in both. If you don't want that, you can add an exception to ci/mypy-stubtest-allowlist.txt.

Comment thread lib/matplotlib/backends/qt_editor/figureoptions.py Outdated
Comment thread lib/matplotlib/animation.pyi Outdated
class Animation:
frame_seq: Iterable[Artist]
event_source: Any
event_source: EventSourceProtocol

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.

Animation._stop sets this attribute to None, so this annotation should still have | None, though I don't know if that could be replaced by a plain call to .stop.

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.

Changed to

event_source: EventSourceProtocol | None  # TODO: We should remove None

It's a bit weird that we set this to None. It's a a core property of Animation and setting to None means that the Animation is not usable anymore after _stop(). This looks like early cleanup. Not sure whether that is needed.

It came in via #22561. @greglucas Was there a specific reason to remove the event source.

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.

I think this was to remove continuous timers. I don't recall why I didn't call "stop" on the event_source instead of removing it entirely. I wonder if there was a test / external event source that didn't implement "stop" 🤷

I think your suggestion makes sense and it was likely just overlooked previously with no specific need to clean it up.

@timhoffm timhoffm Sep 30, 2025

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.

Ok. I still think we should do this separately. As is, we do not have any behavioral change in this PR. I'd rather merge as is and handle the logic change in a follow up (tracking issue created: #30622).

Also, event_source is a mandatory parameter and cannot be None,
because Animation.__init__ calls `event_source.add_callback()`.
@QuLogic QuLogic added this to the v3.11.0 milestone Oct 2, 2025
@QuLogic
QuLogic merged commit 652012d into matplotlib:main Oct 2, 2025
49 of 50 checks passed
@timhoffm
timhoffm deleted the eventsource branch October 2, 2025 04:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API: consistency Consistency of the matplotlib API, including naming, behavior, defaults, … topic: animation topic: typing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants