Skip to content

Commit ca3f71d

Browse files
committed
Add type hints to db methods
1 parent f14c2f6 commit ca3f71d

File tree

13 files changed

+793
-496
lines changed

13 files changed

+793
-496
lines changed

gramps/gen/db/base.py

Lines changed: 225 additions & 153 deletions
Large diffs are not rendered by default.

gramps/gen/db/dummydb.py

Lines changed: 137 additions & 90 deletions
Large diffs are not rendered by default.

gramps/gen/db/generic.py

Lines changed: 79 additions & 78 deletions
Large diffs are not rendered by default.

gramps/gen/filters/rules/person/_isdefaultperson.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# Standard Python modules
2424
#
2525
# -------------------------------------------------------------------------
26+
from __future__ import annotations
2627
from ....const import GRAMPS_LOCALE as glocale
2728

2829
_ = glocale.translation.gettext

gramps/gen/filters/rules/person/_islessthannthgenerationancestorofdefaultperson.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# Standard Python modules
2424
#
2525
# -------------------------------------------------------------------------
26+
from __future__ import annotations
2627
from ....const import GRAMPS_LOCALE as glocale
2728

2829
_ = glocale.translation.gettext

gramps/gen/proxy/cache.py

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,59 @@
2121
"""
2222
Proxy class for the Gramps databases. Caches lookups from handles.
2323
"""
24+
# -------------------------------------------------------------------------
25+
#
26+
# Standard python modules
27+
#
28+
# -------------------------------------------------------------------------
29+
from __future__ import annotations
2430

31+
# -------------------------------------------------------------------------
32+
#
33+
# Gramps libraries
34+
#
35+
# -------------------------------------------------------------------------
36+
from ..lib import (
37+
Date,
38+
Person,
39+
Name,
40+
Surname,
41+
NameOriginType,
42+
Family,
43+
Source,
44+
Citation,
45+
Event,
46+
Media,
47+
Place,
48+
Repository,
49+
Note,
50+
Tag,
51+
)
52+
from ..types import (
53+
AnyHandle,
54+
PersonHandle,
55+
EventHandle,
56+
FamilyHandle,
57+
PlaceHandle,
58+
PrimaryObject,
59+
SourceHandle,
60+
RepositoryHandle,
61+
CitationHandle,
62+
MediaHandle,
63+
NoteHandle,
64+
TagHandle,
65+
PrimaryObjectHandle,
66+
TableObjectType,
67+
PersonGrampsID,
68+
EventGrampsID,
69+
FamilyGrampsID,
70+
PlaceGrampsID,
71+
SourceGrampsID,
72+
RepositoryGrampsID,
73+
CitationGrampsID,
74+
MediaGrampsID,
75+
NoteGrampsID,
76+
)
2577
from ..utils.lru import LRU
2678

2779

@@ -57,7 +109,7 @@ def __getattr__(self, attr):
57109
"""
58110
return getattr(self.db, attr)
59111

60-
def clear_cache(self, handle=None):
112+
def clear_cache(self, handle: PrimaryObjectHandle | None = None):
61113
"""
62114
Clears all caches if handle is None, or
63115
specific entry.
@@ -67,7 +119,7 @@ def clear_cache(self, handle=None):
67119
else:
68120
self.cache_handle.clear()
69121

70-
def get_person_from_handle(self, handle):
122+
def get_person_from_handle(self, handle: PersonHandle) -> Person:
71123
"""
72124
Gets item from cache if it exists. Converts
73125
handles to string, for uniformity.
@@ -76,7 +128,7 @@ def get_person_from_handle(self, handle):
76128
self.cache_handle[handle] = self.db.get_person_from_handle(handle)
77129
return self.cache_handle[handle]
78130

79-
def get_event_from_handle(self, handle):
131+
def get_event_from_handle(self, handle: EventHandle) -> Event:
80132
"""
81133
Gets item from cache if it exists. Converts
82134
handles to string, for uniformity.
@@ -85,7 +137,7 @@ def get_event_from_handle(self, handle):
85137
self.cache_handle[handle] = self.db.get_event_from_handle(handle)
86138
return self.cache_handle[handle]
87139

88-
def get_family_from_handle(self, handle):
140+
def get_family_from_handle(self, handle: FamilyHandle) -> Family:
89141
"""
90142
Gets item from cache if it exists. Converts
91143
handles to string, for uniformity.
@@ -94,7 +146,7 @@ def get_family_from_handle(self, handle):
94146
self.cache_handle[handle] = self.db.get_family_from_handle(handle)
95147
return self.cache_handle[handle]
96148

97-
def get_repository_from_handle(self, handle):
149+
def get_repository_from_handle(self, handle: RepositoryHandle) -> Repository:
98150
"""
99151
Gets item from cache if it exists. Converts
100152
handles to string, for uniformity.
@@ -103,7 +155,7 @@ def get_repository_from_handle(self, handle):
103155
self.cache_handle[handle] = self.db.get_repository_from_handle(handle)
104156
return self.cache_handle[handle]
105157

106-
def get_place_from_handle(self, handle):
158+
def get_place_from_handle(self, handle: PlaceHandle) -> Place:
107159
"""
108160
Gets item from cache if it exists. Converts
109161
handles to string, for uniformity.
@@ -112,7 +164,7 @@ def get_place_from_handle(self, handle):
112164
self.cache_handle[handle] = self.db.get_place_from_handle(handle)
113165
return self.cache_handle[handle]
114166

115-
def get_citation_from_handle(self, handle):
167+
def get_citation_from_handle(self, handle: CitationHandle) -> Citation:
116168
"""
117169
Gets item from cache if it exists. Converts
118170
handles to string, for uniformity.
@@ -121,7 +173,7 @@ def get_citation_from_handle(self, handle):
121173
self.cache_handle[handle] = self.db.get_citation_from_handle(handle)
122174
return self.cache_handle[handle]
123175

124-
def get_source_from_handle(self, handle):
176+
def get_source_from_handle(self, handle: SourceHandle) -> Source:
125177
"""
126178
Gets item from cache if it exists. Converts
127179
handles to string, for uniformity.
@@ -130,7 +182,7 @@ def get_source_from_handle(self, handle):
130182
self.cache_handle[handle] = self.db.get_source_from_handle(handle)
131183
return self.cache_handle[handle]
132184

133-
def get_note_from_handle(self, handle):
185+
def get_note_from_handle(self, handle: NoteHandle) -> Note:
134186
"""
135187
Gets item from cache if it exists. Converts
136188
handles to string, for uniformity.
@@ -139,7 +191,7 @@ def get_note_from_handle(self, handle):
139191
self.cache_handle[handle] = self.db.get_note_from_handle(handle)
140192
return self.cache_handle[handle]
141193

142-
def get_media_from_handle(self, handle):
194+
def get_media_from_handle(self, handle: MediaHandle) -> Media:
143195
"""
144196
Gets item from cache if it exists. Converts
145197
handles to string, for uniformity.
@@ -148,7 +200,7 @@ def get_media_from_handle(self, handle):
148200
self.cache_handle[handle] = self.db.get_media_from_handle(handle)
149201
return self.cache_handle[handle]
150202

151-
def get_tag_from_handle(self, handle):
203+
def get_tag_from_handle(self, handle: TagHandle) -> Tag:
152204
"""
153205
Gets item from cache if it exists. Converts
154206
handles to string, for uniformity.

gramps/gen/proxy/filter.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#
3131
# -------------------------------------------------------------------------
3232
from __future__ import annotations
33+
from typing import Generator, List
3334

3435
# -------------------------------------------------------------------------
3536
#
@@ -123,7 +124,7 @@ def __init__(
123124
self.flist.update(person.get_family_handle_list())
124125
self.flist.update(person.get_parent_family_handle_list())
125126

126-
def get_person_from_handle(self, handle):
127+
def get_person_from_handle(self, handle: PersonHandle) -> Person | None:
127128
"""
128129
Finds a Person in the database from the passed Gramps ID.
129130
If no such Person exists, None is returned.
@@ -179,7 +180,7 @@ def include_event(self, handle):
179180
def include_note(self, handle):
180181
return handle in self.nlist
181182

182-
def get_source_from_handle(self, handle):
183+
def get_source_from_handle(self, handle: SourceHandle) -> Source | None:
183184
"""
184185
Finds a Source in the database from the passed Gramps ID.
185186
If no such Source exists, None is returned.
@@ -203,7 +204,7 @@ def get_source_from_handle(self, handle):
203204

204205
return source
205206

206-
def get_citation_from_handle(self, handle):
207+
def get_citation_from_handle(self, handle: CitationHandle) -> Citation | None:
207208
"""
208209
Finds a Citation in the database from the passed Gramps ID.
209210
If no such Citation exists, None is returned.
@@ -213,7 +214,7 @@ def get_citation_from_handle(self, handle):
213214
self.sanitize_notebase(citation)
214215
return citation
215216

216-
def get_media_from_handle(self, handle):
217+
def get_media_from_handle(self, handle: MediaHandle) -> Media | None:
217218
"""
218219
Finds a Media in the database from the passed Gramps handle.
219220
If no such Object exists, None is returned.
@@ -230,7 +231,7 @@ def get_media_from_handle(self, handle):
230231

231232
return media
232233

233-
def get_place_from_handle(self, handle):
234+
def get_place_from_handle(self, handle: PlaceHandle) -> Place | None:
234235
"""
235236
Finds a Place in the database from the passed Gramps handle.
236237
If no such Place exists, None is returned.
@@ -250,7 +251,7 @@ def get_place_from_handle(self, handle):
250251

251252
return place
252253

253-
def get_event_from_handle(self, handle):
254+
def get_event_from_handle(self, handle: EventHandle) -> Event | None:
254255
"""
255256
Finds a Event in the database from the passed Gramps ID.
256257
If no such Event exists, None is returned.
@@ -263,7 +264,7 @@ def get_event_from_handle(self, handle):
263264
else:
264265
return None
265266

266-
def get_family_from_handle(self, handle):
267+
def get_family_from_handle(self, handle: FamilyHandle) -> Family | None:
267268
"""
268269
Finds a Family in the database from the passed Gramps ID.
269270
If no such Family exists, None is returned.
@@ -320,7 +321,7 @@ def get_family_from_handle(self, handle):
320321
else:
321322
return None
322323

323-
def get_repository_from_handle(self, handle):
324+
def get_repository_from_handle(self, handle: RepositoryHandle) -> Repository | None:
324325
"""
325326
Finds a Repository in the database from the passed Gramps ID.
326327
If no such Repository exists, None is returned.
@@ -331,7 +332,7 @@ def get_repository_from_handle(self, handle):
331332
self.sanitize_addressbase(repository)
332333
return repository
333334

334-
def get_note_from_handle(self, handle):
335+
def get_note_from_handle(self, handle: NoteHandle) -> Note | None:
335336
"""
336337
Finds a Note in the database from the passed Gramps ID.
337338
If no such Note exists, None is returned.
@@ -444,7 +445,9 @@ def get_note_from_gramps_id(self, gramps_id: NoteGrampsID) -> Note | None:
444445
else:
445446
return None
446447

447-
def get_person_handles(self, sort_handles=False, locale=glocale):
448+
def get_person_handles(
449+
self, sort_handles: bool = False, locale=glocale
450+
) -> List[PersonHandle]:
448451
"""
449452
Return a list of database handles, one handle for each Person in
450453
the database.
@@ -457,7 +460,7 @@ def get_person_handles(self, sort_handles=False, locale=glocale):
457460
# FIXME: plist is not a sorted list of handles
458461
return list(self.plist)
459462

460-
def iter_person_handles(self):
463+
def iter_person_handles(self) -> Generator[PersonHandle]:
461464
"""
462465
Return an iterator over database handles, one handle for each Person in
463466
the database.
@@ -490,7 +493,7 @@ def iter_events(self):
490493
"""
491494
return map(self.get_event_from_handle, self.elist)
492495

493-
def get_family_handles(self, sort_handles=False, locale=glocale):
496+
def get_family_handles(self, sort_handles: bool = False, locale=glocale):
494497
"""
495498
Return a list of database handles, one handle for each Family in
496499
the database.
@@ -516,7 +519,7 @@ def iter_families(self):
516519
"""
517520
return map(self.get_family_from_handle, self.flist)
518521

519-
def get_note_handles(self):
522+
def get_note_handles(self) -> List[NoteHandle]:
520523
"""
521524
Return a list of database handles, one handle for each Note in
522525
the database.
@@ -576,7 +579,7 @@ def has_note_handle(self, handle):
576579
"""
577580
return handle in self.nlist
578581

579-
def find_backlink_handles(self, handle, include_classes=None):
582+
def find_backlink_handles(self, handle: AnyHandle, include_classes=None):
580583
"""
581584
Find all objects that hold a reference to the object handle.
582585
Returns an iterator over a list of (class_name, handle) tuples.

0 commit comments

Comments
 (0)