Skip to content

Commit 4117589

Browse files
committed
Make gramps.gen.lib.primaryobj typed
1 parent 4bfd494 commit 4117589

File tree

9 files changed

+97
-311
lines changed

9 files changed

+97
-311
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

+9-33
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#
3232
# -------------------------------------------------------------------------
3333
import logging
34+
from typing_extensions import override
3435

3536
# -------------------------------------------------------------------------
3637
#
@@ -197,19 +198,8 @@ def unserialize(self, data):
197198
SrcAttributeBase.unserialize(self, srcattr_list)
198199
return self
199200

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-
"""
201+
@override
202+
def _has_handle_reference(self, classname: str, handle: str) -> bool:
213203
if classname == "Note":
214204
return handle in [ref.ref for ref in self.note_list]
215205
if classname == "Media":
@@ -218,29 +208,15 @@ def _has_handle_reference(self, classname, handle):
218208
return handle == self.get_reference_handle()
219209
return False
220210

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-
"""
211+
@override
212+
def _remove_handle_references(self, classname: str, handle_list: list[str]) -> None:
230213
if classname == "Source" and self.get_reference_handle() in handle_list:
231214
self.set_reference_handle(None)
232215

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-
"""
216+
@override
217+
def _replace_handle_reference(
218+
self, classname: str, old_handle: str, new_handle: str
219+
) -> None:
244220
if classname == "Source" and self.get_reference_handle() == old_handle:
245221
self.set_reference_handle(new_handle)
246222

gramps/gen/lib/event.py

+9-33
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#
3232
# -------------------------------------------------------------------------
3333
import logging
34+
from typing_extensions import override
3435

3536
# -------------------------------------------------------------------------
3637
#
@@ -255,46 +256,21 @@ def unserialize(self, data):
255256
TagBase.unserialize(self, tag_list)
256257
return self
257258

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-
"""
259+
@override
260+
def _has_handle_reference(self, classname: str, handle: str) -> bool:
271261
if classname == "Place":
272262
return self.place == handle
273263
return False
274264

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-
"""
265+
@override
266+
def _remove_handle_references(self, classname: str, handle_list: list[str]) -> None:
284267
if classname == "Place" and self.place in handle_list:
285268
self.place = ""
286269

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-
"""
270+
@override
271+
def _replace_handle_reference(
272+
self, classname: str, old_handle: str, new_handle: str
273+
) -> None:
298274
if classname == "Place" and self.place == old_handle:
299275
self.place = new_handle
300276

gramps/gen/lib/family.py

+9-36
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#
3232
# -------------------------------------------------------------------------
3333
import logging
34+
from typing_extensions import override
3435

3536
# -------------------------------------------------------------------------
3637
#
@@ -111,16 +112,13 @@ def serialize(self):
111112
"""
112113
Convert the data held in the event to a Python tuple that
113114
represents all the data elements.
114-
115115
This method is used to convert the object into a form that can easily
116116
be saved to a database.
117-
118117
These elements may be primitive Python types (string, integers),
119118
complex Python types (lists or tuples, or Python objects. If the
120119
target database cannot handle complex types (such as objects or
121120
lists), the database is responsible for converting the data into
122121
a form that it can use.
123-
124122
:returns: Returns a python tuple containing the data that should
125123
be considered persistent.
126124
:rtype: tuple
@@ -254,19 +252,8 @@ def unserialize(self, data):
254252
TagBase.unserialize(self, tag_list)
255253
return self
256254

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-
"""
255+
@override
256+
def _has_handle_reference(self, classname: str, handle: str) -> bool:
270257
if classname == "Event":
271258
return self._has_event_reference(handle)
272259
if classname == "Person":
@@ -278,15 +265,8 @@ def _has_handle_reference(self, classname, handle):
278265
return handle in [x.place for x in self.lds_ord_list]
279266
return False
280267

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-
"""
268+
@override
269+
def _remove_handle_references(self, classname: str, handle_list: list[str]) -> None:
290270
if classname == "Event":
291271
self._remove_event_references(handle_list)
292272
elif classname == "Person":
@@ -303,17 +283,10 @@ def _remove_handle_references(self, classname, handle_list):
303283
if lds_ord.place in handle_list:
304284
lds_ord.place = None
305285

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-
"""
286+
@override
287+
def _replace_handle_reference(
288+
self, classname: str, old_handle: str, new_handle: str
289+
) -> None:
317290
if classname == "Event":
318291
self._replace_event_references(old_handle, new_handle)
319292
elif classname == "Person":

gramps/gen/lib/note.py

+9-39
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"""
2525
Note class for Gramps.
2626
"""
27+
from typing_extensions import override
2728

2829
# -------------------------------------------------------------------------
2930
#
@@ -172,22 +173,8 @@ def get_referenced_handles(self):
172173
reflist.extend(self.get_referenced_tag_handles())
173174
return reflist
174175

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-
"""
176+
@override
177+
def has_handle_reference(self, classname: str, handle: str) -> bool:
191178
for dom, obj, prop, hndl in self.get_links():
192179
if (
193180
dom == "gramps"
@@ -198,18 +185,8 @@ def has_handle_reference(self, classname, handle):
198185
return True
199186
return False
200187

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-
"""
188+
@override
189+
def remove_handle_references(self, classname: str, handle_list: list[str]) -> None:
213190
tags = []
214191
for styledtext_tag in self.text.get_tags():
215192
if (
@@ -222,17 +199,10 @@ def remove_handle_references(self, classname, handle_list):
222199
tags.append(styledtext_tag)
223200
self.text.set_tags(tags)
224201

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-
"""
202+
@override
203+
def replace_handle_reference(
204+
self, classname: str, old_handle: str, new_handle: str
205+
) -> None:
236206
for styledtext_tag in self.text.get_tags():
237207
if (
238208
styledtext_tag.name == StyledTextTagType.LINK

gramps/gen/lib/person.py

+9-18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"""
2525
Person object for Gramps.
2626
"""
27+
from typing_extensions import override
2728

2829
# -------------------------------------------------------------------------
2930
#
@@ -130,16 +131,13 @@ def serialize(self):
130131
"""
131132
Convert the data held in the Person to a Python tuple that
132133
represents all the data elements.
133-
134134
This method is used to convert the object into a form that can easily
135135
be saved to a database.
136-
137136
These elements may be primitive Python types (string, integers),
138137
complex Python types (lists or tuples, or Python objects. If the
139138
target database cannot handle complex types (such as objects or
140139
lists), the database is responsible for converting the data into
141140
a form that it can use.
142-
143141
:returns: Returns a python tuple containing the data that should
144142
be considered persistent.
145143
:rtype: tuple
@@ -342,19 +340,8 @@ def set_object_state(self, attr_dict):
342340
self.__gender = attr_dict.pop("gender")
343341
super().set_object_state(attr_dict)
344342

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-
"""
343+
@override
344+
def _has_handle_reference(self, classname: str, handle: str) -> bool:
358345
if classname == "Event":
359346
return self._has_event_reference(handle)
360347
if classname == "Person":
@@ -370,7 +357,8 @@ def _has_handle_reference(self, classname, handle):
370357
return any(ordinance.place == handle for ordinance in self.lds_ord_list)
371358
return False
372359

373-
def _remove_handle_references(self, classname, handle_list):
360+
@override
361+
def _remove_handle_references(self, classname: str, handle_list: list[str]) -> None:
374362
if classname == "Event":
375363
# Keep a copy of the birth and death references
376364
birth_ref = self.get_birth_ref()
@@ -422,7 +410,10 @@ def _remove_handle_references(self, classname, handle_list):
422410
if ordinance.place in handle_list:
423411
ordinance.place = None
424412

425-
def _replace_handle_reference(self, classname, old_handle, new_handle):
413+
@override
414+
def _replace_handle_reference(
415+
self, classname: str, old_handle: str, new_handle: str
416+
) -> None:
426417
if classname == "Event":
427418
refs_list = [ref.ref for ref in self.event_ref_list]
428419
new_ref = None

0 commit comments

Comments
 (0)