From 5c858b347e65020642c79e71c3abe1879c17c9ca Mon Sep 17 00:00:00 2001 From: c0llab0rat0r <78339685+c0llab0rat0r@users.noreply.github.com> Date: Fri, 9 Apr 2021 10:41:56 -0500 Subject: [PATCH] Instead of type annotating in __init__, uncomment the type annotations on the class member variables. --- ipfshttpclient/filescanner.py | 42 ++++++++++++++++++++++++----------- ipfshttpclient/multipart.py | 28 +++++++++++++---------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/ipfshttpclient/filescanner.py b/ipfshttpclient/filescanner.py index f9bb0c38..9c6e9f3d 100644 --- a/ipfshttpclient/filescanner.py +++ b/ipfshttpclient/filescanner.py @@ -35,8 +35,10 @@ class Matcher(ty.Generic[ty.AnyStr], metaclass=abc.ABCMeta): should be included in some file scanning/adding operation""" __slots__ = ("is_binary",) + is_binary: bool + def __init__(self, is_binary: bool = False) -> None: - self.is_binary: bool = is_binary + self.is_binary = is_binary @abc.abstractmethod def should_descend(self, path: ty.AnyStr) -> bool: @@ -127,6 +129,12 @@ class emulates. If your are accustomed the globing on real Unix shells """ __slots__ = ("period_special", "_sep", "_pat", "_dir_only") + period_special: bool + + _sep: ty.AnyStr + _pat: "ty.List[ty.Optional[re_pattern_t[ty.AnyStr]]]" + _dir_only: bool + def __init__(self, pat: ty.AnyStr, *, period_special: bool = True): """ Arguments @@ -140,9 +148,9 @@ def __init__(self, pat: ty.AnyStr, *, period_special: bool = True): """ super().__init__(isinstance(pat, bytes)) - self.period_special: bool = period_special + self.period_special = period_special - self._sep: ty.AnyStr = utils.maybe_fsencode(os.path.sep, pat) + self._sep = utils.maybe_fsencode(os.path.sep, pat) dblstar = utils.maybe_fsencode("**", pat) dot = utils.maybe_fsencode(".", pat) pat_ndot = utils.maybe_fsencode(r"(?![.])", pat) @@ -160,9 +168,9 @@ def __init__(self, pat: ty.AnyStr, *, period_special: bool = True): # (TBH, I find it hard to see how that is useful, but everybody does it # and it keeps things consistent overall – something to only match files # would be nice however.) - self._dir_only: bool = pat.endswith(self._sep) + self._dir_only = pat.endswith(self._sep) - self._pat: ty.List[ty.Optional[re_pattern_t[ty.AnyStr]]] = [] + self._pat = [] for label in pat.split(self._sep): # Skip over useless path components if len(label) < 1 or label == dot: @@ -187,8 +195,7 @@ def __init__(self, pat: ty.AnyStr, *, period_special: bool = True): if period_special and not label.startswith(dot): re_expr = pat_ndot + re_expr self._pat.append(re.compile(re_expr)) - - + def should_descend(self, path: ty.AnyStr) -> bool: for idx, label in enumerate(path.split(self._sep)): # Always descend into any directory below a recursive pattern as we @@ -292,8 +299,10 @@ class ReMatcher(Matcher[ty.AnyStr], ty.Generic[ty.AnyStr]): """ __slots__ = ("_pat",) + _pat: "re_pattern_t[ty.AnyStr]" + def __init__(self, pat: ty.Union[ty.AnyStr, "re_pattern_t[ty.AnyStr]"]): - self._pat: "re_pattern_t[ty.AnyStr]" = re.compile(pat) + self._pat = re.compile(pat) super().__init__(not (self._pat.flags & re.UNICODE)) @@ -309,11 +318,13 @@ class MetaMatcher(Matcher[ty.AnyStr], ty.Generic[ty.AnyStr]): """Match files and directories by delegating to other matchers""" __slots__ = ("_children",) + _children: ty.List[Matcher[ty.AnyStr]] + def __init__(self, children: ty.List[Matcher[ty.AnyStr]]): assert len(children) > 0 super().__init__(children[0].is_binary) - self._children: ty.List[Matcher[ty.AnyStr]] = children + self._children = children def should_descend(self, path: ty.AnyStr) -> bool: return any(m.should_descend(path) for m in self._children) @@ -332,10 +343,12 @@ class NoRecusionAdapterMatcher(Matcher[ty.AnyStr], ty.Generic[ty.AnyStr]): """ __slots__ = ("_child",) + _child: Matcher[ty.AnyStr] + def __init__(self, child: Matcher[ty.AnyStr]): super().__init__(child.is_binary) - self._child: Matcher[ty.AnyStr] = child + self._child = child def should_descend(self, path: ty.AnyStr) -> bool: return False @@ -423,6 +436,9 @@ class FSNodeType(enum.Enum): class walk(ty.Generator[FSNodeEntry, ty.Any, None], ty.Generic[ty.AnyStr]): __slots__ = ("_generator", "_close_fd") + _generator: ty.Generator[FSNodeEntry, None, None] + _close_fd: ty.Optional[int] + def __init__( self, directory: ty.Union[ty.AnyStr, utils.PathLike[ty.AnyStr], int], @@ -465,7 +481,7 @@ def __init__( :class:`NoRecusionAdapterMatcher` and hence prevent the scanner from doing any recursion. """ - self._close_fd: ty.Optional[int] = None + self._close_fd = None # Create matcher object matcher = matcher_from_spec( # type: ignore[type-var] @@ -480,7 +496,7 @@ def __init__( raise NotImplementedError("Passing a file descriptor as directory is " "not supported on this platform") - self._generator: ty.Generator[FSNodeEntry, None, None] = self._walk( + self._generator = self._walk( directory, None, matcher, # type: ignore[arg-type] @@ -678,6 +694,6 @@ def _walk( if HAVE_FWALK: # pragma: no cover - supports_fd = frozenset({walk}) # type: ty.FrozenSet[ty.Callable[..., ty.Any]] + supports_fd: ty.FrozenSet[ty.Callable[..., ty.Any]] = frozenset({walk}) else: # pragma: no cover supports_fd = frozenset() diff --git a/ipfshttpclient/multipart.py b/ipfshttpclient/multipart.py index 13eaf038..f06f95b4 100644 --- a/ipfshttpclient/multipart.py +++ b/ipfshttpclient/multipart.py @@ -122,10 +122,12 @@ class StreamBase(metaclass=abc.ABCMeta): The maximum size that any single file chunk may have in bytes """ __slots__ = ("chunk_size", "name", "_boundary", "_headers") - #chunk_size: int - #name: str - #_boundry: str - #_headers: ty.Dict[str, str] + + chunk_size: int + name: str + + _boundry: str + _headers: ty.Dict[str, str] def __init__(self, name: str, chunk_size: int = default_chunk_size) -> None: self.chunk_size = chunk_size @@ -371,9 +373,10 @@ class DirectoryStream(StreamBase, StreamFileMixin, ty.Generic[ty.AnyStr]): shells allow one to disable this behaviour """ __slots__ = ("abspath", "follow_symlinks", "scanner") - #abspath: ty.Optional[ty.AnyStr] - #follow_symlinks: bool - #scanner: filescanner.walk[ty.AnyStr] + + abspath: ty.Optional[ty.AnyStr] + follow_symlinks: bool + scanner: filescanner.walk[ty.AnyStr] def __init__(self, directory: ty.Union[ty.AnyStr, utils.PathLike[ty.AnyStr], int], *, chunk_size: int = default_chunk_size, @@ -387,7 +390,7 @@ def __init__(self, directory: ty.Union[ty.AnyStr, utils.PathLike[ty.AnyStr], int directory = utils.convert_path(directory) # Create file scanner from parameters - self.scanner: filescanner.walk[ty.AnyStr] = filescanner.walk( + self.scanner = filescanner.walk( directory, patterns, follow_symlinks=follow_symlinks, @@ -396,13 +399,13 @@ def __init__(self, directory: ty.Union[ty.AnyStr, utils.PathLike[ty.AnyStr], int ) # Figure out the absolute path of the directory added - self.abspath = None # type: ty.Optional[ty.AnyStr] + self.abspath = None if not isinstance(directory, int): self.abspath = os.path.abspath(utils.convert_path(directory)) # Figure out basename of the containing directory # (normpath is an acceptable approximation here) - basename = "_" # type: ty.Union[str, bytes] + basename = "_" if not isinstance(directory, int): basename = os.fsdecode(os.path.basename(os.path.normpath(directory))) super().__init__(os.fsdecode(basename), chunk_size=chunk_size) @@ -462,14 +465,15 @@ class BytesFileStream(FilesStream): The maximum size of a single data chunk """ __slots__ = ("data",) - #data: ty.Iterable[bytes] + + data: ty.Iterable[bytes] def __init__(self, data: ty.Union[bytes, gen_bytes_t], name: str = "bytes", *, chunk_size: int = default_chunk_size) -> None: super().__init__([], name=name, chunk_size=chunk_size) if not isinstance(data, bytes): - self.data = data # type: ty.Iterable[bytes] + self.data = data else: self.data = (data,)