Uh oh!
There was an error while loading. Please reload this page.
Add typing to plot methods - #7052
Conversation
headtr1ck
commented
Sep 18, 2022
Puh, this stacking of decorators is quite a brainf*ck... |
headtr1ck
commented
Sep 20, 2022
I could add some return type annotations, but I doubt that mypy can work with the signature hacks and will always use *args, **kwargs. Any thoughts? |
headtr1ck
commented
Sep 20, 2022
The 2D plotfunctions have the correct annotions at runtime ( Does anyone have an idea how to tell mypy that the type is actually the function signature of the "inside" newplotmethod function? Do we need to define a dummy method with the same signature or something? |
max-sixty
commented
Sep 20, 2022
Congrats for doing all these!! This must be (almost?) the last of untyped modules? |
There was a problem hiding this comment.
Looks good to me.
Does anyone have an idea how to tell mypy that the type is actually the function signature of the "inside" newplotmethod function? Do we need to define a dummy method with the same signature or something?
Sorry can't help - a dummy method feels like a bad idea (from a maintenance perspective). But I guess decorators are a runtime feature - not sure if that can be done right statically...
Uh oh!
There was an error while loading. Please reload this page.
| _labels=True, | ||
| **kwargs, | ||
| ): | ||
| darray: DataArray, |
There was a problem hiding this comment.
Is it now common to also type the real function when having overloads?
There was a problem hiding this comment.
I guess if your goal is to have typing support for people using the function it is useless.
But it helps if you want to typecheck the function :)
There was a problem hiding this comment.
Also, in modern python the type annotations are stored in the signature, but only of the "real" function and not the overloads.
headtr1ck
commented
Sep 22, 2022
Finally it is a much larger PR than initially expected. I am now stuck in trying to resolve the broken test. |
headtr1ck
commented
Sep 23, 2022
Wow, to fix the overloads I had to copy-paste the complete function signature for each overload and again for the accessor methods. Are there any objections to leaving it like this? |
Ok found another interesting problem: Does anyone know how we can force it to use the module imshow instead of the overload wrapper? |
Illviljan
commented
Sep 24, 2022
Maybe there's something to learn from how pandas does it: https://github.com/pandas-dev/pandas/tree/main/pandas/plotting ? I'm a little skeptical if all the arguments in scatter are necessary and maybe they can be hidden in I've been working on moving all plots to the DataArray side starting with scatter in #6778. It should also remove the |
Just had a quick look and I think their typing of the accessor is wrong, haha. They claim to return a PlotAccessor instance when calling e.g.
I saw that, that's why I left the DataArray scatter untouched for now. |
For now I see two options:
In principle also a combination of the two is possible :) |
I'd opt for (1) & a comment on why it needs a separate module. |
headtr1ck
commented
Sep 25, 2022
I have tried to solve it with ParamSpec and writing a custom wraps decorator, but it did not work, presumably due to python/mypy#13540 |
headtr1ck
commented
Oct 13, 2022
Next time I should split such a PR into smaller ones, haha. |
Illviljan
commented
Oct 13, 2022
This crashes now. Maybe you'll find the bug faster than me. importxarrayasxrds=xr.tutorial.scatter_example_dataset(seed=42)
fg=ds.plot.scatter("A", "B", z="z", hue="y", row="x", col="w")
Traceback (mostrecentcalllast):
File"C:\Users\J.W\anaconda3\envs\xarray-tests\lib\site-packages\spyder_kernels\py3compat.py", line356, incompat_execexec(code, globals, locals)
File"g:\program\dropbox\python\xarray_line_plot.py", line122, in<module>fg=ds.plot.scatter("A", "B", z="z", hue="y", row="x", col="w")
File"C:\Users\J.W\Documents\GitHub\xarray\xarray\plot\accessor.py", line975, inscatterreturndataset_plot.scatter(self._ds, *args, **kwargs)
File"C:\Users\J.W\Documents\GitHub\xarray\xarray\plot\dataset_plot.py", line234, innewplotfuncreturn_easy_facetgrid(kind="dataset", **allargs, **kwargs)
File"C:\Users\J.W\Documents\GitHub\xarray\xarray\plot\facetgrid.py", line770, in_easy_facetgridreturng.map_dataset(plotfunc, x, y, **kwargs)
File"C:\Users\J.W\Documents\GitHub\xarray\xarray\plot\facetgrid.py", line370, inmap_datasetmaybe_mappable=func(
File"C:\Users\J.W\Documents\GitHub\xarray\xarray\plot\dataset_plot.py", line262, innewplotfuncprimitive=plotfunc(
File"C:\Users\J.W\Documents\GitHub\xarray\xarray\plot\dataset_plot.py", line541, inscatterprimitive=ax.scatter(
File"C:\Users\J.W\anaconda3\envs\xarray-tests\lib\site-packages\matplotlib\__init__.py", line1423, ininnerreturnfunc(ax, *map(sanitize_sequence, args), **kwargs)
File"C:\Users\J.W\anaconda3\envs\xarray-tests\lib\site-packages\matplotlib\axes\_axes.py", line4626, inscattercollection._internal_update(kwargs)
File"C:\Users\J.W\anaconda3\envs\xarray-tests\lib\site-packages\matplotlib\artist.py", line1186, in_internal_updatereturnself._update_props(
File"C:\Users\J.W\anaconda3\envs\xarray-tests\lib\site-packages\matplotlib\artist.py", line1160, in_update_propsraiseAttributeError(
AttributeError: PathCollection.set() gotanunexpectedkeywordargument'z' |
headtr1ck
commented
Oct 13, 2022
Ok, good question. I have to look into it. Anyway, you should use kwargs for x and y as well :) |
Illviljan
commented
Oct 13, 2022
Hah, unfortunately my random test scripts I copy/pasted from can't keep up with branches. :D |
This comment was marked as outdated.
This comment was marked as outdated.
For me this code works? |
Yeah, I had some strange issues there. One was that the branch wasn't up to date and another one that I can't replicate anymore... Agreed on the labels, I haven't figured how to avoid that overlap yet. ds=xr.tutorial.scatter_example_dataset(seed=42)
fg=ds.plot.scatter(x="A", y="B", hue="y", row="x", col="w")
fg=ds.plot.scatter(x="A", y="B", z="z", hue="y", row="x", col="w")Not how it's done in plots2d (yet) though. |
headtr1ck
commented
Oct 13, 2022
Illviljan
commented
Oct 13, 2022
There was a problem hiding this comment.
Very well done @headtr1ck ! This is looking good to me. Just a few minor comments below.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Illviljan
commented
Oct 16, 2022
Thanks @headtr1ck ! :) |
* main: Add import ASV benchmark (pydata#7176) Rework docs about scatter plots (pydata#7169) Fix some scatter plot issues (pydata#7167) Fix doctest warnings, enable errors in CI (pydata#7166) fix broken test (pydata#7168) Add typing to plot methods (pydata#7052) Fix warning in doctest (pydata#7165) dev whats-new (pydata#7161) v2022.10.0 whats-new (pydata#7160)
* main: Add import ASV benchmark (pydata#7176) Rework docs about scatter plots (pydata#7169) Fix some scatter plot issues (pydata#7167) Fix doctest warnings, enable errors in CI (pydata#7166) fix broken test (pydata#7168) Add typing to plot methods (pydata#7052) Fix warning in doctest (pydata#7165)




whats-new.rst