Skip to content

Commit

Permalink
Instead of type annotating in __init__, uncomment the type annotation…
Browse files Browse the repository at this point in the history
…s on the class member variables.
  • Loading branch information
c0llab0rat0r committed Apr 9, 2021
1 parent bdba112 commit 5c858b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
42 changes: 29 additions & 13 deletions ipfshttpclient/filescanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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))

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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()
28 changes: 16 additions & 12 deletions ipfshttpclient/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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,)

Expand Down

0 comments on commit 5c858b3

Please sign in to comment.