Debian Codegraph @

docs/devguide/override_guidelines.rst

raw
1 ==========================
2 Python Override Guidelines
3 ==========================
4
5 This document serves as a guide for developers creating new PyGObject
6 overrides or modifying existing ones. This document is not intended as hard
7 rules as there may always be pragmatic exceptions to what is listed here. It
8 is also a good idea to study the Zen of Python by Tim Peters in :pep:`20`.
9
10 In general, overrides should be minimized and preference should always be
11 placed on updating the underlying API to be more bindable, adding features to
12 GI to support the requirement, or adding mechanical features to PyGObject
13 which can apply generically to all overrides (:bzbug:`721226` and
14 :bzbug:`640812`).
15
16 If a GI feature or more bindable API for a library is in the works, it is a
17 good idea to avoid the temptation to add temporary short term workarounds in
18 overrides. The reason is this can creaste unnecessary conflicts when the
19 bindable API becomes a reality (:bzbug:`707280`).
20
21 * Minimize class overrides when possible.
22
23 *Reason*: Class overrides incur a load time performance penalty because
24 they require the classes GType and all of the Python method bindings to be
25 created. See :bzbug:`705810`
26
27 * Prefer monkey patching methods on repository classes over inheritance.
28
29 *Reason*: Class overrides add an additional level to the method
30 resolution order (mro) which has a performance penalty. Since overrides are
31 designed for specific repository library APIs, monkey patching is
32 reasonable because it is utilized in a controlled manner by the API
33 designer (as opposed to monkey patching a third-party library which is more
34 fragile).
35
36 * Avoid overriding ``__init__``
37 *Reason*: Sub-classing the overridden class then becomes challenging and
38 has the potential to cause bugs (see :bzbug:`711487` and reasoning
39 listed in :doc:`initializer_deprecations`).
40
41 * Unbindable functions which take variadic arguments are generally ok to add
42 Python implementations, but keep in mind the prior noted guidelines. A lot
43 of times adding bindable versions of the functions to the underlying library
44 which take a list is acceptable. For example: :bzbug:`706119`. Another
45 problem here is if an override is added, then later a bindable version of
46 the API is added which takes a list, there is a good chance we have to live
47 with the override forever which masks a working version implemented by GI.
48
49 * Avoid side effects beyond the intended repositories API in function/method
50 overrides.
51
52 *Reason*: This conflates the original API and adds a documentation burden
53 on the override maintainer.
54
55 * Don't change function signatures from the original API and don't add default
56 values.
57
58 *Reason*: This turns into a documentation discrepancy between the libraries
59 API and the Python version of the API. Default value work should focus on
60 bug :bzbug:`558620`, not cherry-picking individual Python functions and
61 adding defaults.
62
63 * Avoid implicit side effects to the Python standard library (or anywhere).
64
65 * Don't modify or use sys.argv
66
67 *Reason*: sys.argv should only be explicitly controlled by application
68 developers. Otherwise it requires hacks to work around a module modifying
69 or using the developers command line args which they rightfully own.
70
71 .. code:: python
72
73 saved_argv = sys.argv.copy()
74 sys.argv = []
75 from gi.repository import Gtk
76 sys.argv = saved_argv
77
78 * Never set Pythons default encoding.
79
80 *Reason*: Read or watch Ned Batchelders "`Pragmatic Unicode
81 <https://nedbatchelder.com/text/unipain.html>`__"
82
83 * Prefer adapter patterns over of inheritance and overrides.
84
85 *Reason*: An adapter allows more flexibility and less dependency on
86 overrides. It allows application developers to use the raw GI API without
87 having to think about if a particular typelibs overrides have been installed
88 or not.