summaryrefslogtreecommitdiff
path: root/dedup/filemagic.py
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2023-05-09 15:10:25 +0200
committerHelmut Grohne <helmut@subdivi.de>2023-05-09 15:12:01 +0200
commit924f0c734a7accb87e2ac911cee6e24dd463f237 (patch)
treeeb1bcaa2f25933374d28905bcb56e2e8aabeec62 /dedup/filemagic.py
parent8a05a6d8bacea0643a4967eed4cd67019ee0b6d7 (diff)
downloaddebian-dedup-924f0c734a7accb87e2ac911cee6e24dd463f237.tar.gz
add type annotations to most of the codeHEADmaster
Diffstat (limited to 'dedup/filemagic.py')
-rw-r--r--dedup/filemagic.py20
1 files changed, 12 insertions, 8 deletions
diff --git a/dedup/filemagic.py b/dedup/filemagic.py
index b71c276..a6d09ba 100644
--- a/dedup/filemagic.py
+++ b/dedup/filemagic.py
@@ -1,6 +1,8 @@
"""A very strange "hash" that uses the magic module (python3-magic) to guess
the file type."""
+import typing
+
import magic
# It changed API a few times...
@@ -13,36 +15,38 @@ class FileDigester:
"""A hashlib-like class to guess a filetype using the magic module."""
FILE_BYTES_MAX = 1024 * 1024 # copied from file source
- def __init__(self):
- self.buff = b""
- self.identification = None
+ def __init__(self) -> None:
+ self.buff: typing.Optional[bytes] = b""
+ self.identification: typing.Optional[str] = None
- def _compute_identification(self):
+ def _compute_identification(self) -> str:
+ assert self.buff is not None
try:
return _magic_identify(self.buff)
except UnicodeDecodeError:
return "magic identification is not valid UTF-8"
- def update(self, buff):
+ def update(self, buff: bytes) -> None:
if self.identification:
return
+ assert self.buff is not None
self.buff += buff
if len(self.buff) >= self.FILE_BYTES_MAX:
self.identification = self._compute_identification()
self.buff = None
- def identify(self):
+ def identify(self) -> str:
"""Return the guessed file magic identification."""
if self.identification:
return self.identification
return self._compute_identification()
- def hexdigest(self):
+ def hexdigest(self) -> str:
"""Compatibility with hashlib. An alias of identify. Doesn't return
hex."""
return self.identify()
- def copy(self):
+ def copy(self) -> "FileDigester":
new = FileDigester()
new.buff = self.buff
new.identification = self.identification