8000 TYP: Fix type hints in animation by ksunden · Pull Request #679 · has2k1/plotnine · GitHub
[go: up one dir, main page]

Skip to content

TYP: Fix type hints in animation #679

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

Merged
merged 1 commit into from
Apr 19, 2023
Merged

Conversation

ksunden
Copy link
Contributor
@ksunden ksunden commented Apr 18, 2023

Actual return is in fact list[list[artist]], which is what mpl.ArtistAnimation actually expects, just the type hints were incorrect

Test case (adapted from tests):

from plotnine.animation import PlotnineAnimation
from plotnine import labs, lims, qplot, theme_minimal, theme
import matplotlib.pyplot as plt

_theme = theme(subplots_adjust={"right": 0.8})

x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
colors = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]

def plot(i):
    return (
        qplot(x, y, color=colors[i], xlab="x", ylab="y")
        + lims(color=(1, 7))
        + labs(color="color")
        + theme_minimal()
        + _theme
    )

plots = [plot(i) for i in range(3)]
anim = PlotnineAnimation(plots, interval=100, repeat_delay=500)

fig, artists = anim._draw_plots(plots)
print(artists)
print(type(artists[0]))

Outputs (formatted for readability):

[[<matplotlib.collections.PathCollection object at ...>, <matplotlib.offsetbox.AnchoredOffsetbox object at ...>],
 [<matplotlib.collections.PathCollection object at ...>, <matplotlib.offsetbox.AnchoredOffsetbox object at ...>],
 [<matplotlib.collections.PathCollection object at ...>, <matplotlib.offsetbox.AnchoredOffsetbox object at ...>]]
<class 'list'>

Note that _draw_plots returns a nested list.

We (matplotlib) recently introduced type hints (matplotlib/matplotlib#24976), this was discovered in my survey of some of our downstream packages to see how our type hints do.
There are a couple more things that I have flagged that either I have opened or intend to open fixing things on our end, I think there may be some clarifications for the type checker here still even so (though mostly fairly easy e.g. "add a type hint for this variable which the typechecker just sees as Unknown" or "make this list a tuple so the typechecker can validate length and each element type")
We would invite you to test it out early so that you are not surprised when mpl v3.8 is released in a couple months.
If you have any questions or are unsure of how to satisfy the typechecker for mpl related things, feel free to ping me.

Actual return is in fact list[list[artist]], which is what mpl.ArtistAnimation actually expects, just the type hints were incorrect
@ksunden ksunden changed the title TYP: Fix type hints in anmation TYP: Fix type hints in animation Apr 18, 2023
@codecov
Copy link
codecov bot commented Apr 19, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (47cc23d) 0.00% compared to head (9b22aa7) 0.00%.

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #679   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files        159     159           
  Lines      10355   10355           
=====================================
  Misses     10355   10355           
Impacted Files Coverage Δ
plotnine/animation.py 0.00% <ø> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@has2k1 has2k1 merged commit 28f26dd into has2k1:main Apr 19, 2023
@has2k1
Copy link
Owner
has2k1 commented Apr 19, 2023

Thank you for the heads up and PR. Type hinting in matplotlib is quite welcome and I will keep track ahead of the 3.8 release.

has2k1 added a commit that referenced this pull request Apr 19, 2023
< 6A97 div class="AvatarStack flex-self-start " >
ref: #679
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0