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

Improve some import handling #1928

Merged
merged 2 commits into from
Jun 23, 2021
Merged

Improve some import handling #1928

merged 2 commits into from
Jun 23, 2021

Conversation

dopplershift
Copy link
Member

Description Of Changes

  • Reduce import load of metpy.io.metar. This is currently causing importing metpy.io to trigger imports of metpy.calc and metpy.plots, which can add almost half a second to the total import and currently triggers warnings when Cartopy isn't installed
  • Move handling of our Cartopy map features to only warn (when Cartopy isn't installed) upon access of the relevant attributes. This uses the module-level __getattr__() we have available now that we're only supporting Python >=3.7.

Checklist

)

This avoids importing metpy.calc and metpy.plots in the metar parsing
code, which improves import times for code that's only doing unrelated
I/O, as well as eliminates a warning when CartoPy is not installed.

The wx_code_map and wx_code_to_numeric function should eventually be
moved to metar.py, but that's an API change.
@dopplershift dopplershift added this to the 1.1.0 milestone Jun 22, 2021
@dopplershift dopplershift added Area: Infrastructure Pertains to project infrastructure (e.g. CI, linting) Type: Bug Something is not working like it should Type: Enhancement Enhancement to existing functionality labels Jun 22, 2021
dcamron
dcamron previously approved these changes Jun 23, 2021
Copy link
Member

@dcamron dcamron left a comment

Choose a reason for hiding this comment

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

Good to go. If it doesn't matter for directly-accessed plots.__dir__() to be sorted, you can drop that. dir(plots) and iPython tab-completion already return sorted. Don't envision it hurting to leave in.

@dopplershift
Copy link
Member Author

Thanks for catching that, it was an erroneous copy from this PEP. I fixed, and addressed a couple things the bots pointed out. Let's see now if the failure path for __getattr__() is still covered...

@dopplershift
Copy link
Member Author

This should be fully tested now and ready for final review.

dcamron
dcamron previously approved these changes Jun 23, 2021
@dopplershift
Copy link
Member Author

Ugh, failed on build without CartoPy installed. Fixed.

@dopplershift
Copy link
Member Author

While looking elsewhere, I was inspired and decided that instead of logging a warning about needing Cartopy, it should just be part of the AttributeError we would raise.

Use the module-level __getattr__ support in Python 3.7 to dynamically
handle access to the CartoPy map features and only warn *upon use* if
CartoPy isn't installed.
@dcamron dcamron merged commit 4c1d51b into Unidata:main Jun 23, 2021
@dcamron dcamron mentioned this pull request Jun 23, 2021
1 task
@dopplershift dopplershift deleted the fix-1901 branch June 24, 2021 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Infrastructure Pertains to project infrastructure (e.g. CI, linting) Type: Bug Something is not working like it should Type: Enhancement Enhancement to existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Level3File import is a bit bloated
2 participants