Skip to content

Commit ee27bd4

Browse files
committed
Make gramps.gen.lib.primaryobj typed
1 parent ee111ce commit ee27bd4

File tree

10 files changed

+109
-304
lines changed

10 files changed

+109
-304
lines changed

.github/workflows/gramps-ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
sudo apt-get install libxml2-utils
5555
sudo apt-get install python3-lxml
5656
sudo apt-get install python3-libxml2
57+
sudo apt-get install python3-typing-extensions
5758
sudo apt-get install zlib1g-dev
5859
sudo apt-get install python3-setuptools
5960
python3 -m pip install orjson

gramps/gen/lib/citation.py

+11-33
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#
3232
# -------------------------------------------------------------------------
3333
import logging
34+
from collections.abc import Collection
35+
36+
from typing_extensions import override
3437

3538
# -------------------------------------------------------------------------
3639
#
@@ -197,19 +200,8 @@ def unserialize(self, data):
197200
SrcAttributeBase.unserialize(self, srcattr_list)
198201
return self
199202

200-
def _has_handle_reference(self, classname, handle):
201-
"""
202-
Return True if the object has reference to a given handle of given
203-
primary object type.
204-
205-
:param classname: The name of the primary object class.
206-
:type classname: str
207-
:param handle: The handle to be checked.
208-
:type handle: str
209-
:returns: Returns whether the object has reference to this handle of
210-
this object type.
211-
:rtype: bool
212-
"""
203+
@override
204+
def _has_handle_reference(self, classname: str, handle: str) -> bool:
213205
if classname == "Note":
214206
return handle in [ref.ref for ref in self.note_list]
215207
if classname == "Media":
@@ -218,29 +210,15 @@ def _has_handle_reference(self, classname, handle):
218210
return handle == self.get_reference_handle()
219211
return False
220212

221-
def _remove_handle_references(self, classname, handle_list):
222-
"""
223-
Remove all references in this object to object handles in the list.
224-
225-
:param classname: The name of the primary object class.
226-
:type classname: str
227-
:param handle_list: The list of handles to be removed.
228-
:type handle_list: str
229-
"""
213+
@override
214+
def _remove_handle_references(self, classname: str, handle_list: Collection[str]) -> None:
230215
if classname == "Source" and self.get_reference_handle() in handle_list:
231216
self.set_reference_handle(None)
232217

233-
def _replace_handle_reference(self, classname, old_handle, new_handle):
234-
"""
235-
Replace all references to old handle with those to the new handle.
236-
237-
:param classname: The name of the primary object class.
238-
:type classname: str
239-
:param old_handle: The handle to be replaced.
240-
:type old_handle: str
241-
:param new_handle: The handle to replace the old one with.
242-
:type new_handle: str
243-
"""
218+
@override
219+
def _replace_handle_reference(
220+
self, classname: str, old_handle: str, new_handle: str
221+
) -> None:
244222
if classname == "Source" and self.get_reference_handle() == old_handle:
245223
self.set_reference_handle(new_handle)
246224

gramps/gen/lib/event.py

+11-33
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#
3232
# -------------------------------------------------------------------------
3333
import logging
34+
from collections.abc import Collection
35+
36+
from typing_extensions import override
3437

3538
# -------------------------------------------------------------------------
3639
#
@@ -255,46 +258,21 @@ def unserialize(self, data):
255258
TagBase.unserialize(self, tag_list)
256259
return self
257260

258-
def _has_handle_reference(self, classname, handle):
259-
"""
260-
Return True if the object has reference to a given handle of given
261-
primary object type.
262-
263-
:param classname: The name of the primary object class.
264-
:type classname: str
265-
:param handle: The handle to be checked.
266-
:type handle: str
267-
:returns: Returns whether the object has reference to this handle of
268-
this object type.
269-
:rtype: bool
270-
"""
261+
@override
262+
def _has_handle_reference(self, classname: str, handle: str) -> bool:
271263
if classname == "Place":
272264
return self.place == handle
273265
return False
274266

275-
def _remove_handle_references(self, classname, handle_list):
276-
"""
277-
Remove all references in this object to object handles in the list.
278-
279-
:param classname: The name of the primary object class.
280-
:type classname: str
281-
:param handle_list: The list of handles to be removed.
282-
:type handle_list: str
283-
"""
267+
@override
268+
def _remove_handle_references(self, classname: str, handle_list: Collection[str]) -> None:
284269
if classname == "Place" and self.place in handle_list:
285270
self.place = ""
286271

287-
def _replace_handle_reference(self, classname, old_handle, new_handle):
288-
"""
289-
Replace all references to old handle with those to the new handle.
290-
291-
:param classname: The name of the primary object class.
292-
:type classname: str
293-
:param old_handle: The handle to be replaced.
294-
:type old_handle: str
295-
:param new_handle: The handle to replace the old one with.
296-
:type new_handle: str
297-
"""
272+
@override
273+
def _replace_handle_reference(
274+
self, classname: str, old_handle: str, new_handle: str
275+
) -> None:
298276
if classname == "Place" and self.place == old_handle:
299277
self.place = new_handle
300278

gramps/gen/lib/eventbase.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""
2424
EventBase class for Gramps.
2525
"""
26+
from collections.abc import Collection
2627

2728
# -------------------------------------------------------------------------
2829
#
@@ -135,13 +136,12 @@ def _has_event_reference(self, event_handle):
135136
"""
136137
return event_handle in [event_ref.ref for event_ref in self.event_ref_list]
137138

138-
def _remove_event_references(self, handle_list):
139+
def _remove_event_references(self, handle_list: Collection[str]):
139140
"""
140141
Remove any references to the given event handles from the instance's
141142
:class:`~.eventref.EventRef` list.
142143
143144
:param handle_list: List of valid event handles to remove
144-
:type handle_list: list
145145
"""
146146
new_list = [
147147
event_ref

gramps/gen/lib/family.py

+10-33
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#
3232
# -------------------------------------------------------------------------
3333
import logging
34+
from collections.abc import Collection
35+
from typing_extensions import override
3436

3537
# -------------------------------------------------------------------------
3638
#
@@ -254,19 +256,8 @@ def unserialize(self, data):
254256
TagBase.unserialize(self, tag_list)
255257
return self
256258

257-
def _has_handle_reference(self, classname, handle):
258-
"""
259-
Return True if the object has reference to a given handle of given
260-
primary object type.
261-
262-
:param classname: The name of the primary object class.
263-
:type classname: str
264-
:param handle: The handle to be checked.
265-
:type handle: str
266-
:returns: Returns whether the object has reference to this handle of
267-
this object type.
268-
:rtype: bool
269-
"""
259+
@override
260+
def _has_handle_reference(self, classname: str, handle: str) -> bool:
270261
if classname == "Event":
271262
return self._has_event_reference(handle)
272263
if classname == "Person":
@@ -278,15 +269,8 @@ def _has_handle_reference(self, classname, handle):
278269
return handle in [x.place for x in self.lds_ord_list]
279270
return False
280271

281-
def _remove_handle_references(self, classname, handle_list):
282-
"""
283-
Remove all references in this object to object handles in the list.
284-
285-
:param classname: The name of the primary object class.
286-
:type classname: str
287-
:param handle_list: The list of handles to be removed.
288-
:type handle_list: str
289-
"""
272+
@override
273+
def _remove_handle_references(self, classname: str, handle_list: Collection[str]) -> None:
290274
if classname == "Event":
291275
self._remove_event_references(handle_list)
292276
elif classname == "Person":
@@ -303,17 +287,10 @@ def _remove_handle_references(self, classname, handle_list):
303287
if lds_ord.place in handle_list:
304288
lds_ord.place = None
305289

306-
def _replace_handle_reference(self, classname, old_handle, new_handle):
307-
"""
308-
Replace all references to old handle with those to the new handle.
309-
310-
:param classname: The name of the primary object class.
311-
:type classname: str
312-
:param old_handle: The handle to be replaced.
313-
:type old_handle: str
314-
:param new_handle: The handle to replace the old one with.
315-
:type new_handle: str
316-
"""
290+
@override
291+
def _replace_handle_reference(
292+
self, classname: str, old_handle: str, new_handle: str
293+
) -> None:
317294
if classname == "Event":
318295
self._replace_event_references(old_handle, new_handle)
319296
elif classname == "Person":

gramps/gen/lib/note.py

+11-39
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"""
2525
Note class for Gramps.
2626
"""
27+
from collections.abc import Collection
28+
29+
from typing_extensions import override
2730

2831
# -------------------------------------------------------------------------
2932
#
@@ -172,22 +175,8 @@ def get_referenced_handles(self):
172175
reflist.extend(self.get_referenced_tag_handles())
173176
return reflist
174177

175-
def has_handle_reference(self, classname, handle):
176-
"""
177-
Return True if the object has reference to a given handle of given
178-
primary object type.
179-
180-
:param classname: The name of the primary object class.
181-
:type classname: str
182-
:param handle: The handle to be checked.
183-
:type handle: str
184-
185-
:returns:
186-
Returns whether the object has reference to this handle of
187-
this object type.
188-
189-
:rtype: bool
190-
"""
178+
@override
179+
def has_handle_reference(self, classname: str, handle: str) -> bool:
191180
for dom, obj, prop, hndl in self.get_links():
192181
if (
193182
dom == "gramps"
@@ -198,18 +187,8 @@ def has_handle_reference(self, classname, handle):
198187
return True
199188
return False
200189

201-
def remove_handle_references(self, classname, handle_list):
202-
"""
203-
Remove all references in this object to object handles in the list.
204-
205-
:param classname: The name of the primary object class.
206-
:type classname: str
207-
:param handle_list: The list of handles to be removed.
208-
:type handle_list: str
209-
210-
If the link is in the styled text, we just remove the style for that
211-
link.
212-
"""
190+
@override
191+
def remove_handle_references(self, classname: str, handle_list: Collection[str]) -> None:
213192
tags = []
214193
for styledtext_tag in self.text.get_tags():
215194
if (
@@ -222,17 +201,10 @@ def remove_handle_references(self, classname, handle_list):
222201
tags.append(styledtext_tag)
223202
self.text.set_tags(tags)
224203

225-
def replace_handle_reference(self, classname, old_handle, new_handle):
226-
"""
227-
Replace all references to old handle with those to the new handle.
228-
229-
:param classname: The name of the primary object class.
230-
:type classname: str
231-
:param old_handle: The handle to be replaced.
232-
:type old_handle: str
233-
:param new_handle: The handle to replace the old one with.
234-
:type new_handle: str
235-
"""
204+
@override
205+
def replace_handle_reference(
206+
self, classname: str, old_handle: str, new_handle: str
207+
) -> None:
236208
for styledtext_tag in self.text.get_tags():
237209
if (
238210
styledtext_tag.name == StyledTextTagType.LINK

gramps/gen/lib/person.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"""
2525
Person object for Gramps.
2626
"""
27+
from collections.abc import Collection
28+
from typing_extensions import override
2729

2830
# -------------------------------------------------------------------------
2931
#
@@ -342,19 +344,8 @@ def set_object_state(self, attr_dict):
342344
self.__gender = attr_dict.pop("gender")
343345
super().set_object_state(attr_dict)
344346

345-
def _has_handle_reference(self, classname, handle):
346-
"""
347-
Return True if the object has reference to a given handle of given
348-
primary object type.
349-
350-
:param classname: The name of the primary object class.
351-
:type classname: str
352-
:param handle: The handle to be checked.
353-
:type handle: str
354-
:returns: Returns whether the object has reference to this handle of
355-
this object type.
356-
:rtype: bool
357-
"""
347+
@override
348+
def _has_handle_reference(self, classname: str, handle: str) -> bool:
358349
if classname == "Event":
359350
return self._has_event_reference(handle)
360351
if classname == "Person":
@@ -370,7 +361,8 @@ def _has_handle_reference(self, classname, handle):
370361
return any(ordinance.place == handle for ordinance in self.lds_ord_list)
371362
return False
372363

373-
def _remove_handle_references(self, classname, handle_list):
364+
@override
365+
def _remove_handle_references(self, classname: str, handle_list: Collection[str]) -> None:
374366
if classname == "Event":
375367
# Keep a copy of the birth and death references
376368
birth_ref = self.get_birth_ref()
@@ -422,7 +414,10 @@ def _remove_handle_references(self, classname, handle_list):
422414
if ordinance.place in handle_list:
423415
ordinance.place = None
424416

425-
def _replace_handle_reference(self, classname, old_handle, new_handle):
417+
@override
418+
def _replace_handle_reference(
419+
self, classname: str, old_handle: str, new_handle: str
420+
) -> None:
426421
if classname == "Event":
427422
refs_list = [ref.ref for ref in self.event_ref_list]
428423
new_ref = None

0 commit comments

Comments
 (0)