Fixed typechecking with mypy

This commit is contained in:
Stijn Van Campenhout
2019-09-20 15:28:26 +02:00
parent a748bf9dd5
commit 5d27b5b3c4
6 changed files with 106 additions and 129 deletions

View File

@@ -1,9 +1,9 @@
from .document import Document
from .folder import Folder
from typing import NoReturn, TypeVar, List
from typing import NoReturn, List, Union
from .exceptions import FolderNotFound
DocOrFolder = TypeVar('DocumentOrFolder', Document, Folder)
DocumentOrFolder = Union[Document, Folder]
class Collection(object):
@@ -15,13 +15,13 @@ class Collection(object):
items: A list containing the items.
"""
items = []
items: List[DocumentOrFolder] = []
def __init__(self, *items):
for i in items:
self.items.append(i)
def add(self, docdict: dict) -> NoReturn:
def add(self, docdict: dict) -> None:
"""Add an item to the collection.
It wraps it in the correct class based on the Type parameter of the
dict.
@@ -31,14 +31,14 @@ class Collection(object):
"""
if docdict.get("Type", None) == "DocumentType":
return self.add_document(docdict)
self.add_document(docdict)
elif docdict.get("Type", None) == "CollectionType":
return self.add_folder(docdict)
self.add_folder(docdict)
else:
raise TypeError("Unsupported type: {_type}"
.format(_type=docdict.get("Type", None)))
def add_document(self, docdict: dict) -> NoReturn:
def add_document(self, docdict: dict) -> None:
"""Add a document to the collection
Args:
@@ -47,7 +47,7 @@ class Collection(object):
self.items.append(Document(**docdict))
def add_folder(self, dirdict: dict) -> NoReturn:
def add_folder(self, dirdict: dict) -> None:
"""Add a document to the collection
Args:
@@ -56,7 +56,7 @@ class Collection(object):
self.items.append(Folder(**dirdict))
def parent(self, docorfolder: DocOrFolder) -> Folder:
def parent(self, docorfolder: DocumentOrFolder) -> Folder:
"""Returns the paren of a Document or Folder
Args:
@@ -67,12 +67,12 @@ class Collection(object):
"""
results = [i for i in self.items if i.ID == docorfolder.ID]
if len(results) > 0:
if len(results) > 0 and isinstance(results[0], Folder):
return results[0]
else:
raise FolderNotFound("Could not found the parent of the document.")
def children(self, folder: Folder = None) -> List[DocOrFolder]:
def children(self, folder: Folder = None) -> List[DocumentOrFolder]:
"""Get all the childern from a folder
Args:
@@ -90,5 +90,5 @@ class Collection(object):
def __len__(self) -> int:
return len(self.items)
def __getitem__(self, position: int) -> DocOrFolder:
def __getitem__(self, position: int) -> DocumentOrFolder:
return self.items[position]