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

metpy.calc.tools imports metpy.calc.basic which creates potential circular import #1211

Closed
akrherz opened this issue Oct 18, 2019 · 5 comments · Fixed by #1213
Closed

metpy.calc.tools imports metpy.calc.basic which creates potential circular import #1211

akrherz opened this issue Oct 18, 2019 · 5 comments · Fixed by #1213
Labels
Area: Calc Pertains to calculations Type: Bug Something is not working like it should
Milestone

Comments

@akrherz
Copy link
Contributor

akrherz commented Oct 18, 2019

Whilst attempting to add a calculation helper method within metpy.calc.tools to be used in metpy.calc.basic, I realized metpy.calc.tools imports metpy.calc.basic creating a circular import situation.

Far be it for me to make any authoritative API design statement, but it seems that if metpy.calc.tools is meant to include shared helpers useful for other metpy.calc functions, like it already is being used for thermo, kinematics, indices and cross_sections, then this potential circular import should be avoided.

For example with this code change:

diff --git a/src/metpy/calc/basic.py b/src/metpy/calc/basic.py
index b05b8d40..280f5ff6 100644
--- a/src/metpy/calc/basic.py
+++ b/src/metpy/calc/basic.py
@@ -18,6 +18,7 @@ import numpy as np
 from scipy.ndimage import gaussian_filter
 
 from .. import constants as mpconsts
+from .tools import first_derivative
 from ..deprecation import deprecated
 from ..package_tools import Exporter
 from ..units import atleast_1d, check_units, masked_array, units

we get breakage

>>> from metpy.calc import wind_direction
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/akrherz/projects/MetPy/src/metpy/calc/__init__.py", line 6, in <module>
    from .basic import *  # noqa: F403
  File "/home/akrherz/projects/MetPy/src/metpy/calc/basic.py", line 21, in <module>
    from .tools import first_derivative
  File "/home/akrherz/projects/MetPy/src/metpy/calc/tools.py", line 22, in <module>
    from .basic import height_to_pressure_std, pressure_to_height_std
ImportError: cannot import name 'height_to_pressure_std'

Thanks for your consideration on this.

@akrherz akrherz added the Type: Bug Something is not working like it should label Oct 18, 2019
@zbruick
Copy link
Contributor

zbruick commented Oct 18, 2019

What version are you using? I'm unable to recreate on v0.11 or master currently.

@akrherz
Copy link
Contributor Author

akrherz commented Oct 18, 2019

I am on master. You are attempting to recreate this with the hypothetical patch included?

@zbruick
Copy link
Contributor

zbruick commented Oct 18, 2019

Ah no, I see what you mean now. I think it's fair to move those imports in tools.py down into the function where they're used, since they're only currently used in a single function. Do you want to do that in addition to whatever helper you're creating?

@akrherz
Copy link
Contributor Author

akrherz commented Oct 18, 2019

@zbruick sorry for the confusion, yes you are tracking what I am saying now. Since the usage appears to be limited to the tools._get_bound_pressure_height function, which is unclear to me how it is even being used in MetPy? Will boggle this some more.

@zbruick
Copy link
Contributor

zbruick commented Oct 18, 2019

Looks like that helper is only used in get_layer within tools, so it makes sense to keep it where it is. I think moving the line 'from . import height_to_pressure_std, pressure_to_height_std' within the helper would be the easiest solution so that things in tools can be used in basic now and in the future.

@dopplershift dopplershift added this to the 0.12 milestone Oct 21, 2019
@dopplershift dopplershift added the Area: Calc Pertains to calculations label Oct 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Calc Pertains to calculations Type: Bug Something is not working like it should
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants