Skip to content

Commit

Permalink
fix: get_path() mutated ZipBasedPackage object to FastLookup
Browse files Browse the repository at this point in the history
calling  `zipfile.Path` mutates the objects type
  • Loading branch information
janbritz committed Nov 21, 2024
1 parent 8efa02d commit 43e0990
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions questionpy_server/worker/runtime/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,29 @@ def init(self, env: Environment) -> QPyPackageInterface:
return main_module.init(*(self, env)[: len(signature.parameters)])


class ZipBasedPackage(ZipFile, ImportablePackage):
class ZipBasedPackage(ImportablePackage):
"""A 'regular', zip-formatted QuestionPy package."""

def __init__(self, path: Path):
super().__init__(path, "r")
self.path = path
self._path = path
self._zip_file = ZipFile(path)

@cached_property
def manifest(self) -> Manifest:
"""Load QuestionPy manifest from package."""
data = self.read(f"{DIST_DIR}/{MANIFEST_FILENAME}")
data = self._zip_file.read(f"{DIST_DIR}/{MANIFEST_FILENAME}")
return Manifest.model_validate_json(data)

@property
def path(self) -> Path:
return self._path

def get_path(self, path: str) -> Traversable:
# Intuitively, a path beginning with '/' should be absolute within the package, but ZipFile behaves differently.
path = path.lstrip("/")

# According to the docs, zipfile.Path implements Traversable.
return cast(Traversable, zipfile.Path(self, path))
return cast(Traversable, zipfile.Path(self._zip_file, path))

def setup_imports(self) -> None:
for new_path in (
Expand Down

0 comments on commit 43e0990

Please sign in to comment.