Skip to content
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

Make Gridliner deprecation warning show up #2387

Merged
merged 2 commits into from
Jun 8, 2024

Conversation

rcomer
Copy link
Member

@rcomer rcomer commented May 19, 2024

Rationale

Follow up to #2330. The deprecation warning is currently not showing for normal use. I thought I had checked but perhaps I only looked in test output. I've now set the stacklevel to 3 for when Gridliner is created via ax.gridlines, and to 2 for the possible (but unlikely?) cases where a Gridliner is created directly.

I have checked locally that this now gives the desired results

In [1]: import matplotlib.pyplot as plt
   ...: import cartopy.crs as ccrs
   ...: fig = plt.figure()
   ...: ax = fig.add_subplot(projection=ccrs.PlateCarree())
   ...: ax.coastlines()
Out[1]: <cartopy.mpl.feature_artist.FeatureArtist at 0x756842d2d1c0>

In [2]: ax.gridlines(draw_labels=True, auto_update=True)
<ipython-input-2-499091cee3ba>:1: DeprecationWarning: The auto_update parameter was deprecated at Cartopy 0.23.  In future the gridlines and labels will always be updated.
  ax.gridlines(draw_labels=True, auto_update=True)
Out[2]: <cartopy.mpl.gridliner.Gridliner at 0x75688bcc5790>

In [3]: from cartopy.mpl.gridliner import Gridliner

In [4]: test = Gridliner(ax, ccrs.PlateCarree(), auto_update=True)
<ipython-input-4-422209944b61>:1: DeprecationWarning: The auto_update parameter was deprecated at Cartopy 0.23.  In future the gridlines and labels will always be updated.
  test = Gridliner(ax, ccrs.PlateCarree(), auto_update=True)

Implications

@rcomer rcomer added this to the Next Release milestone May 19, 2024
@rcomer
Copy link
Member Author

rcomer commented May 25, 2024

#2394 suggests we have a bad interaction between the label placement logic and bbox_inches='tight'. I have added a comment that that should be addressed before we expire the deprecation.

warnings.warn(
"The auto_update parameter was deprecated at Cartopy 0.23. In future "
"the gridlines and labels will always be updated.",
DeprecationWarning)
DeprecationWarning,
stacklevel=(3 if sys._getframemodulename(1) == 'cartopy.mpl.geoaxes'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should be using the private method here. The documentation says it may not be available in all reference Python implementations.
https://docs.python.org/3/library/sys.html#sys._getframemodulename

I think you could possibly use the inspect module instead, but it looks like in other code locations we've just explicitly set the stacklevel to 2, is that enough to handle this and not special case different stack levels?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - I didn't know inspect had something for that. Now updated. If we leave the stacklevel at 2 then the warning won't show for users who are calling ax.gridlines. I was semi-following Matplotlib's warn_external which uses sys._getframe.

@greglucas greglucas merged commit 17c7ef8 into SciTools:main Jun 8, 2024
21 checks passed
@rcomer rcomer deleted the gridliner-warning branch June 23, 2024 09:44
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