@@ -673,11 +673,25 @@ def _valid_syntax_repr(strategy):
673673KNOWN_FUNCTION_LOCATIONS : Dict [object , str ] = {}
674674
675675
676+ def _get_module_helper (obj ):
677+ # Get the __module__ attribute of the object, and return the first ancestor module
678+ # which contains the object; falling back to the literal __module__ if none do.
679+ # The goal is to show location from which obj should usually be accessed, rather
680+ # than what we assume is an internal submodule which defined it.
681+ module_name = obj .__module__
682+ dots = [i for i , c in enumerate (module_name ) if c == "." ] + [None ]
683+ for idx in dots :
684+ if getattr (sys .modules .get (module_name [:idx ]), obj .__name__ , None ) is obj :
685+ KNOWN_FUNCTION_LOCATIONS [obj ] = module_name [:idx ]
686+ return module_name [:idx ]
687+ return module_name
688+
689+
676690def _get_module (obj ):
677691 if obj in KNOWN_FUNCTION_LOCATIONS :
678692 return KNOWN_FUNCTION_LOCATIONS [obj ]
679693 try :
680- return obj . __module__
694+ return _get_module_helper ( obj )
681695 except AttributeError :
682696 if not _is_probably_ufunc (obj ):
683697 raise
@@ -936,7 +950,7 @@ def magic(
936950 functions .add (f )
937951 if getattr (thing , "__name__" , None ):
938952 if inspect .isclass (thing ):
939- KNOWN_FUNCTION_LOCATIONS [f ] = thing . __module__
953+ KNOWN_FUNCTION_LOCATIONS [f ] = _get_module_helper ( thing )
940954 else :
941955 KNOWN_FUNCTION_LOCATIONS [f ] = thing .__name__
942956 except (TypeError , ValueError ):
0 commit comments