Skip to content

Commit

Permalink
feat: support for namespace packages
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Feb 11, 2024
1 parent 41eabb7 commit 5c9ba1f
Show file tree
Hide file tree
Showing 2 changed files with 638 additions and 566 deletions.
20 changes: 15 additions & 5 deletions src/lazy_imports_lite/_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def scan_distributions():
metadata = dist.metadata

if metadata is None:
continue
continue # pragma: no cover

if metadata["Keywords"] is None:
continue
Expand All @@ -56,14 +56,21 @@ def _top_level_declared(dist):
def _top_level_inferred(dist):
files = dist.files
if files is None:
return {}
return {} # pragma: no cover

return {
f.parts[0] if len(f.parts) > 1 else f.with_suffix("").name
parts = {
f.parts[:-1] if len(f.parts) > 1 else f.with_suffix("").name
for f in files
if f.suffix == ".py"
}

is_namespace = min(len(p) for p in parts) == 2

if is_namespace:
return {".".join(p) for p in parts if len(p) == 2}
else:
return {".".join(p) for p in parts if len(p) == 1}


class LazyLoader(importlib.abc.Loader, importlib.machinery.PathFinder):
def find_spec(self, fullname, path=None, target=None):
Expand All @@ -83,8 +90,11 @@ def find_spec(self, fullname, path=None, target=None):
return None # pragma: no cover

name = spec.name.split(".")[0]
namespace_name = ".".join(spec.name.split(".")[:2])

if name in enabled_packages and spec.origin.endswith(".py"):
if (
name in enabled_packages or namespace_name in enabled_packages
) and spec.origin.endswith(".py"):
spec.loader = self
return spec

Expand Down
Loading

0 comments on commit 5c9ba1f

Please sign in to comment.