Skip to content

Commit

Permalink
Defer import inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhollas committed Aug 5, 2024
1 parent 5ddd122 commit 8e66fbc
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import zipp
import email
import types
import inspect
import pathlib
import operator
import textwrap
Expand Down Expand Up @@ -1071,6 +1070,9 @@ def _topmost(name: PackagePath) -> Optional[str]:
return top if rest else None


inspect = None


def _get_toplevel_name(name: PackagePath) -> str:
"""
Infer a possibly importable module name from a name presumed on
Expand All @@ -1089,11 +1091,14 @@ def _get_toplevel_name(name: PackagePath) -> str:
>>> _get_toplevel_name(PackagePath('foo.dist-info'))
'foo.dist-info'
"""
return _topmost(name) or (
# python/typeshed#10328
inspect.getmodulename(name) # type: ignore
or str(name)
)
n = _topmost(name)
if n:
return n

global inspect
if inspect is None:
import inspect
return inspect.getmodulename(name) or str(name)


def _top_level_inferred(dist):
Expand Down

0 comments on commit 8e66fbc

Please sign in to comment.