Repository: Unidata/MetPy

Issue:
Set __module__ on exported functions, etc.
Functions and classes have a `__module__` attribute, which IPython and Jupyter use when printing out a name:
```python
from metpy.calc import geostrophic_wind
geostrophic_wind
> <function metpy.calc.kinematics.geostrophic_wind(heights, f, dx, dy)>
```
It would be nice if the printed name didn't include the implementation detail of the function's original home.

We should add a helper function that each of the top-level modules calls, passing in `__all__` and the module's `__name__`; this function can reset the module on all the items in `__all__`.

Discussion:
dopplershift: :rage: I was running flake8 regularly here to try to avoid this. And apparently did not do it on the last iteration.
dopplershift: Alright, cleaned up.
dopplershift: Weird. Importing numpy within the `__init__.py` for the sub packages in question fixes it up--`dockets` must be doing some magic to determine the `globals` for running the doc examples. This has become a bit trickier than I want right now, so I'm going to punt this from the 0.10 release.

Patch:
diff --git a/src/metpy/calc/__init__.py b/src/metpy/calc/__init__.py
index 5c1f22aaff..faf6b112c5 100644
--- a/src/metpy/calc/__init__.py
+++ b/src/metpy/calc/__init__.py
@@ -10,6 +10,7 @@ from .kinematics import *  # noqa: F403
 from .thermo import *  # noqa: F403
 from .tools import *  # noqa: F403
 from .turbulence import *  # noqa: F403
+from ..package_tools import set_module
 
 __all__ = basic.__all__[:]  # pylint: disable=undefined-variable
 __all__.extend(cross_sections.__all__)  # pylint: disable=undefined-variable
@@ -18,3 +19,5 @@ __all__.extend(kinematics.__all__)  # pylint: disable=undefined-variable
 __all__.extend(thermo.__all__)  # pylint: disable=undefined-variable
 __all__.extend(tools.__all__)  # pylint: disable=undefined-variable
 __all__.extend(turbulence.__all__)  # pylint: disable=undefined-variable
+
+set_module(globals())
diff --git a/src/metpy/interpolate/__init__.py b/src/metpy/interpolate/__init__.py
index 9161bcf697..59f8788f69 100644
--- a/src/metpy/interpolate/__init__.py
+++ b/src/metpy/interpolate/__init__.py
@@ -8,9 +8,12 @@ from .one_dimension import *  # noqa: F403
 from .points import *  # noqa: F403
 from .slices import *  # noqa: F403
 from .tools import *  # noqa: F403
+from ..package_tools import set_module
 
 __all__ = grid.__all__[:]  # pylint: disable=undefined-variable
 __all__.extend(one_dimension.__all__)  # pylint: disable=undefined-variable
 __all__.extend(points.__all__)  # pylint: disable=undefined-variable
 __all__.extend(slices.__all__)  # pylint: disable=undefined-variable
 __all__.extend(tools.__all__)  # pylint: disable=undefined-variable
+
+set_module(globals())
diff --git a/src/metpy/io/__init__.py b/src/metpy/io/__init__.py
index 6cbf28c20c..aae7c90c0c 100644
--- a/src/metpy/io/__init__.py
+++ b/src/metpy/io/__init__.py
@@ -12,6 +12,11 @@ from .gini import *  # noqa: F403
 from .metar import *  # noqa: F403
 from .nexrad import *  # noqa: F403
 from .station_data import *  # noqa: F403
+from ..package_tools import set_module
 
 __all__ = gini.__all__[:]  # pylint: disable=undefined-variable
+__all__.extend(metar.__all__)  # pylint: disable=undefined-variable
 __all__.extend(nexrad.__all__)  # pylint: disable=undefined-variable
+__all__.extend(station_data.__all__)  # pylint: disable=undefined-variable
+
+set_module(globals())
diff --git a/src/metpy/package_tools.py b/src/metpy/package_tools.py
index 3b8faf9d0a..a423844f1d 100644
--- a/src/metpy/package_tools.py
+++ b/src/metpy/package_tools.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2015,2019 MetPy Developers.
+# Copyright (c) 2015,2018,2019 MetPy Developers.
 # Distributed under the terms of the BSD 3-Clause License.
 # SPDX-License-Identifier: BSD-3-Clause
 """Collection of tools for managing the package."""
@@ -37,3 +37,25 @@ class Exporter:
         """Exit the instance tracking block."""
         self.exports.extend(set(self.globls) - self.start_vars)
         del self.start_vars
+
+
+def set_module(globls):
+    """Set the module for all functions in ``__all__``.
+
+    This sets the ``__module__`` attribute of all items within the ``__all__`` list
+    for the calling module.
+
+    This supports our hoisting of functions out of individual modules, which are
+    considered implementation details, into the namespace of the top-level subpackage.
+
+    Parameters
+    ----------
+    globls : Dict[str, object]
+        Mapping of all global variables for the module. This contains all needed
+        python special ("dunder") variables needed to be modified.
+
+    """
+    for item in globls['__all__']:
+        obj = globls[item]
+        if hasattr(obj, '__module__'):
+            obj.__module__ = globls['__name__']
diff --git a/src/metpy/plots/__init__.py b/src/metpy/plots/__init__.py
index 7058aa6aab..1af06cce64 100644
--- a/src/metpy/plots/__init__.py
+++ b/src/metpy/plots/__init__.py
@@ -14,6 +14,7 @@ from .declarative import *  # noqa: F403
 from .skewt import *  # noqa: F403
 from .station_plot import *  # noqa: F403
 from .wx_symbols import *  # noqa: F403
+from ..package_tools import set_module
 
 logger = logging.getLogger(__name__)
 
@@ -29,3 +30,5 @@ try:
     __all__.extend(['USCOUNTIES', 'USSTATES'])
 except ImportError:
     logger.warning('Cannot import USCOUNTIES and USSTATES without Cartopy installed.')
+
+set_module(globals())


Test patch:
diff --git a/conftest.py b/conftest.py
index 14c1fe7502..472e8d19f4 100644
--- a/conftest.py
+++ b/conftest.py
@@ -17,6 +17,7 @@ import traitlets
 import xarray
 
 import metpy.calc
+import metpy.units
 
 # Need to disable fallback before importing pint
 os.environ['PINT_ARRAY_PROTOCOL_FALLBACK'] = '0'
@@ -42,7 +43,9 @@ def doctest_available_modules(doctest_namespace):
     """Make modules available automatically to doctests."""
     doctest_namespace['metpy'] = metpy
     doctest_namespace['metpy.calc'] = metpy.calc
+    doctest_namespace['np'] = numpy
     doctest_namespace['plt'] = matplotlib.pyplot
+    doctest_namespace['units'] = metpy.units.units
 
 
 @pytest.fixture()
diff --git a/tests/test_packaging.py b/tests/test_packaging.py
new file mode 100644
index 0000000000..9a447e4d03
--- /dev/null
+++ b/tests/test_packaging.py
@@ -0,0 +1,17 @@
+# Copyright (c) 2018 MetPy Developers.
+# Distributed under the terms of the BSD 3-Clause License.
+# SPDX-License-Identifier: BSD-3-Clause
+"""Test packaging details."""
+
+from metpy.calc import tke
+from metpy.interpolate import interpolate_to_grid
+from metpy.io import Level2File
+from metpy.plots import StationPlot
+
+
+def test_modules_set():
+    """Test that functions from each subpackage have correct module set."""
+    assert Level2File.__module__ == 'metpy.io'
+    assert StationPlot.__module__ == 'metpy.plots'
+    assert interpolate_to_grid.__module__ == 'metpy.interpolate'
+    assert tke.__module__ == 'metpy.calc'