Skip to content

Commit

Permalink
fix include mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
pubpub-zz committed Nov 2, 2023
1 parent a0ee1a4 commit ab96331
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
Any,
Callable,
Dict,
Generator,
Iterable,
Iterator,
List,
Mapping,
Optional,
Expand Down Expand Up @@ -1848,7 +1848,7 @@ def get_from_file_specification(_a: DictionaryObject) -> PdfObject:
)


class AttachmentBytesDictionary(dict):
class AttachmentBytesDictionary(Mapping[str, AttachmentBytes]):
"""
Dict[str, AttachmentBytes]
Ease access to Dictionary of Object
Expand All @@ -1858,24 +1858,28 @@ class AttachmentBytesDictionary(dict):
names: List[str]

def __init__(
self, root: Optional[Union[NameTree, DictionaryObject, IndirectObject]]
):
dict.__init__(self)
self, root: Optional[Union[NameTree, DictionaryObject]] = None
) -> None:
# super().__init__(self)
if isinstance(root, IndirectObject):
root = cast(DictionaryObject, root.get_object())

Check warning on line 1865 in pypdf/generic/_data_structures.py

View check run for this annotation

Codecov / codecov/patch

pypdf/generic/_data_structures.py#L1865

Added line #L1865 was not covered by tests
if root is not None:
self.root = (
root if isinstance(root, NameTree) else NameTree(root)
)
self.root = root if isinstance(root, NameTree) else NameTree(root)
self.names = list(self.root.list_keys())
else:
self.root = None
self.names = []

def keys(self) -> List[str]:
def keys(self) -> List[str]: # type: ignore[override]
return self.names

def items(self) -> Generator[str, AttachmentBytes]:
def __len__(self) -> int:
return len(self.names)

Check warning on line 1877 in pypdf/generic/_data_structures.py

View check run for this annotation

Codecov / codecov/patch

pypdf/generic/_data_structures.py#L1877

Added line #L1877 was not covered by tests

def __iter__(self) -> Iterator[str]: # type: ignore
yield from self.names

Check warning on line 1880 in pypdf/generic/_data_structures.py

View check run for this annotation

Codecov / codecov/patch

pypdf/generic/_data_structures.py#L1880

Added line #L1880 was not covered by tests

def items(self) -> Iterable[Tuple[str, AttachmentBytes]]: # type: ignore[override]
if self.root is None:
return []
else:
Expand All @@ -1891,8 +1895,12 @@ def items(self) -> Generator[str, AttachmentBytes]:

def __getitem__(self, k: str) -> AttachmentBytes:
if k not in self.names:
raise KeyError("KeyError: k")
raise KeyError(f"KeyError: {k}")

Check warning on line 1898 in pypdf/generic/_data_structures.py

View check run for this annotation

Codecov / codecov/patch

pypdf/generic/_data_structures.py#L1898

Added line #L1898 was not covered by tests
if self.root is None:
raise ValueError("Empty Object")

Check warning on line 1900 in pypdf/generic/_data_structures.py

View check run for this annotation

Codecov / codecov/patch

pypdf/generic/_data_structures.py#L1900

Added line #L1900 was not covered by tests
v = self.root.list_get(k)
if v is None:
raise KeyError(f"KeyError: {k}")

Check warning on line 1903 in pypdf/generic/_data_structures.py

View check run for this annotation

Codecov / codecov/patch

pypdf/generic/_data_structures.py#L1903

Added line #L1903 was not covered by tests
return AttachmentBytes(cast(DictionaryObject, v.get_object()))

def __repr__(self) -> str:
Expand Down

0 comments on commit ab96331

Please sign in to comment.