From 3b604970828a61abceb80d8a90eed26037887a90 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Thu, 14 Apr 2022 08:58:36 +0100 Subject: [PATCH] fix mypy --- my/core/kompress.py | 20 +++++++++++++++----- tests/core/test_kompress.py | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/my/core/kompress.py b/my/core/kompress.py index 6285442..c56de73 100644 --- a/my/core/kompress.py +++ b/my/core/kompress.py @@ -5,7 +5,7 @@ from __future__ import annotations import pathlib from pathlib import Path -from typing import Union, IO +from typing import Union, IO, Sequence import io PathIsh = Union[Path, str] @@ -121,13 +121,23 @@ def kexists(path: PathIsh, subpath: str) -> bool: import zipfile class ZipPath(zipfile.Path): + # seems that at/root are not exposed in the docs, so might be an implementation detail + at: str + root: zipfile.ZipFile + + @property + def filename(self) -> str: + res = self.root.filename + assert res is not None # make mypy happy + return res + def absolute(self) -> ZipPath: - return ZipPath(Path(self.root.filename).absolute(), self.at) + return ZipPath(Path(self.filename).absolute(), self.at) def exists(self) -> bool: if self.at == '': # special case, the base class returns False in this case for some reason - return Path(self.root.filename).exists() + return Path(self.filename).exists() return super().exists() def rglob(self, glob: str) -> Sequence[ZipPath]: @@ -141,7 +151,7 @@ class ZipPath(zipfile.Path): assert self.root == other.root, (self.root, other.root) return Path(self.at).relative_to(Path(other.at)) - @property + @property # type: ignore[misc] def __class__(self): return Path @@ -149,4 +159,4 @@ class ZipPath(zipfile.Path): # hmm, super class doesn't seem to treat as equals unless they are the same object if not isinstance(other, ZipPath): return False - return self.root.filename == other.root.filename and self.at == other.at + return self.filename == other.filename and self.at == other.at diff --git a/tests/core/test_kompress.py b/tests/core/test_kompress.py index 193b6cf..9819f1d 100644 --- a/tests/core/test_kompress.py +++ b/tests/core/test_kompress.py @@ -71,7 +71,7 @@ def test_zippath() -> None: matched = list(zp.rglob('*')) assert len(matched) > 0 - assert all(p.root.filename == str(target) for p in matched), matched + assert all(p.filename == str(target) for p in matched), matched rpaths = [str(p.relative_to(zp)) for p in matched] assert rpaths == [