Merge pull request #392 from agentydragon/notetype

More type annotations for notes.py
This commit is contained in:
Damien Elmes 2019-12-28 07:05:57 +10:00 committed by GitHub
commit c91b1377d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,9 +2,10 @@
# Copyright: Ankitects Pty Ltd and contributors # Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from typing import Any, List, Optional, Tuple from typing import Any, Dict, List, Optional, Tuple
import anki # pylint: disable=unused-import import anki # pylint: disable=unused-import
from anki.types import NoteType
from anki.utils import ( from anki.utils import (
fieldChecksum, fieldChecksum,
guid64, guid64,
@ -17,12 +18,23 @@ from anki.utils import (
class Note: class Note:
col: "anki.storage._Collection"
newlyAdded: bool
id: int
guid: str
_model: NoteType
mid: int
tags: List[str] tags: List[str]
fields: List[str]
flags: int
data: str
_fmap: Dict[str, Tuple[Any, Any]]
scm: int
def __init__( def __init__(
self, self,
col: "anki.storage._Collection", col: "anki.storage._Collection",
model: Optional[Any] = None, model: Optional[NoteType] = None,
id: Optional[int] = None, id: Optional[int] = None,
) -> None: ) -> None:
assert not (model and id) assert not (model and id)
@ -111,16 +123,16 @@ insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)""",
) )
] ]
def model(self) -> Any: def model(self) -> Optional[NoteType]:
return self._model return self._model
# Dict interface # Dict interface
################################################## ##################################################
def keys(self) -> List: def keys(self) -> List[str]:
return list(self._fmap.keys()) return list(self._fmap.keys())
def values(self) -> Any: def values(self) -> List[str]:
return self.fields return self.fields
def items(self) -> List[Tuple[Any, Any]]: def items(self) -> List[Tuple[Any, Any]]:
@ -132,14 +144,14 @@ insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)""",
except: except:
raise KeyError(key) raise KeyError(key)
def __getitem__(self, key: str) -> Any: def __getitem__(self, key: str) -> str:
return self.fields[self._fieldOrd(key)] return self.fields[self._fieldOrd(key)]
def __setitem__(self, key: str, value: str) -> None: def __setitem__(self, key: str, value: str) -> None:
self.fields[self._fieldOrd(key)] = value self.fields[self._fieldOrd(key)] = value
def __contains__(self, key) -> bool: def __contains__(self, key) -> bool:
return key in list(self._fmap.keys()) return key in self._fmap
# Tags # Tags
################################################## ##################################################
@ -150,10 +162,10 @@ insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)""",
def stringTags(self) -> Any: def stringTags(self) -> Any:
return self.col.tags.join(self.col.tags.canonify(self.tags)) return self.col.tags.join(self.col.tags.canonify(self.tags))
def setTagsFromStr(self, str) -> None: def setTagsFromStr(self, tags: str) -> None:
self.tags = self.col.tags.split(str) self.tags = self.col.tags.split(tags)
def delTag(self, tag) -> None: def delTag(self, tag: str) -> None:
rem = [] rem = []
for t in self.tags: for t in self.tags:
if t.lower() == tag.lower(): if t.lower() == tag.lower():