has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
4k stars 213 forks source link

TYP: Fix type hints in animation #679

Closed ksunden closed 1 year ago

ksunden commented 1 year ago

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.

codecov[bot] commented 1 year ago

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 ```diff @@ Coverage Diff @@ ## main #679 +/- ## ===================================== Coverage 0.00% 0.00% ===================================== Files 159 159 Lines 10355 10355 ===================================== Misses 10355 10355 ``` | [Impacted Files](https://codecov.io/gh/has2k1/plotnine/pull/679?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Hassan+Kibirige) | Coverage Δ | | |---|---|---| | [plotnine/animation.py](https://codecov.io/gh/has2k1/plotnine/pull/679?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Hassan+Kibirige#diff-cGxvdG5pbmUvYW5pbWF0aW9uLnB5) | `0.00% <ø> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Hassan+Kibirige). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Hassan+Kibirige)

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

has2k1 commented 1 year ago

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.