Skip to content

Commit 153d75e

Browse files
committed
Refactor _TopLevelFinder so it is easier to test
1 parent 150798d commit 153d75e

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

setuptools/command/editable_wheel.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def __init__(self, dist: Distribution, name: str):
505505
self.dist = dist
506506
self.name = name
507507

508-
def __call__(self, wheel: "WheelFile", files: List[str], mapping: Dict[str, str]):
508+
def template_vars(self) -> Tuple[str, str, Dict[str, str], Dict[str, List[str]]]:
509509
src_root = self.dist.src_root or os.curdir
510510
top_level = chain(_find_packages(self.dist), _find_top_level_modules(self.dist))
511511
package_dir = self.dist.package_dir or {}
@@ -519,7 +519,7 @@ def __call__(self, wheel: "WheelFile", files: List[str], mapping: Dict[str, str]
519519
)
520520

521521
legacy_namespaces = {
522-
pkg: find_package_path(pkg, roots, self.dist.src_root or "")
522+
cast(str, pkg): find_package_path(pkg, roots, self.dist.src_root or "")
523523
for pkg in self.dist.namespace_packages or []
524524
}
525525

@@ -530,11 +530,20 @@ def __call__(self, wheel: "WheelFile", files: List[str], mapping: Dict[str, str]
530530

531531
name = f"__editable__.{self.name}.finder"
532532
finder = _normalization.safe_identifier(name)
533+
return finder, name, mapping, namespaces_
534+
535+
def get_implementation(self) -> Iterator[Tuple[str, bytes]]:
536+
finder, name, mapping, namespaces_ = self.template_vars()
537+
533538
content = bytes(_finder_template(name, mapping, namespaces_), "utf-8")
534-
wheel.writestr(f"{finder}.py", content)
539+
yield (f"{finder}.py", content)
535540

536541
content = _encode_pth(f"import {finder}; {finder}.install()")
537-
wheel.writestr(f"__editable__.{self.name}.pth", content)
542+
yield (f"__editable__.{self.name}.pth", content)
543+
544+
def __call__(self, wheel: "WheelFile", files: List[str], mapping: Dict[str, str]):
545+
for file, content in self.get_implementation():
546+
wheel.writestr(file, content)
538547

539548
def __enter__(self):
540549
msg = "Editable install will be performed using a meta path finder.\n"

0 commit comments

Comments
 (0)