ph¶
Private Methods
Private methods, if any (those starting with _), are documented
for completeness but do not offer any stability guarantees.
They may change or be removed at any time without notice.
Polytropos helpers for Odoo addons.
This library is meant to be used by Odoo addons at runtime.
OverrideResolver
¶
Descriptor for version-conditional method overrides.
Created by :func:override. When a class is defined, :meth:__set_name__
replaces this descriptor with the resolved function — the first variant
whose specifier matches the current Odoo release, or the default.
Source code in src/polytropos/ph.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
__get__(obj, objtype=None)
¶
Descriptor protocol — resolves and binds on access.
This is a safety net in case __set_name__ has not been called
(e.g. module-level use outside a class body) or the descriptor
was manually kept.
Source code in src/polytropos/ph.py
__set_name__(owner, name)
¶
Replace this descriptor with the resolved function.
Called by type.__new__ during class creation. The resolved
function becomes the class attribute, so lookup and super()
work naturally.
Source code in src/polytropos/ph.py
_resolve()
¶
Resolve which function to use based on the current Odoo release.
The resolved function always exposes the original default
implementation via its .default attribute, so variants
can call it when they only need to adjust behavior for a
specific release.
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
The matching variant (with |
Callable[..., Any]
|
default (with |
Source code in src/polytropos/ph.py
when(spec)
¶
Register a version-conditional variant of the method.
The variant replaces the default when the current Odoo release matches spec. Variants are evaluated in declaration order; the first match wins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
str
|
PEP 440 version specifier (e.g., |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the variant function's |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., Any]], OverrideResolver]
|
A decorator that registers the variant and returns this resolver. |
Source code in src/polytropos/ph.py
override(f)
¶
Make a method version-aware with :meth:OverrideResolver.when.
The decorated method is the default implementation. Register
version-specific alternatives by decorating the same name with
.when()::
from polytropos import ph
class MyModel(models.Model):
@ph.override
def name_search(self, name, domain=None, operator="ilike", limit=100):
return super().name_search(
name, domain=domain, operator=operator, limit=limit,
)
@name_search.when("<19")
def name_search(self, name, args=None, operator="ilike", limit=100):
return super().name_search(
name, args=args, operator=operator, limit=limit,
)
At class creation time the resolver checks the current Odoo release
and keeps only the method that matches. Inheritance and super()
work as expected.
Raises:
| Type | Description |
|---|---|
TypeError
|
If Odoo is not installed and no default is reachable (should not happen in normal Odoo operation). |
Returns:
| Type | Description |
|---|---|
OverrideResolver
|
class: |
OverrideResolver
|
resolved function when the class is created. |
Source code in src/polytropos/ph.py
rel(spec)
¶
Check if current Odoo release matches a PEP 440 version specifier.
This function checks the Odoo release (e.g., "17.0", "18.0") against a PEP 440 specifier, useful for conditional code execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
str
|
PEP 440 version specifier (e.g., ">=17.0", ">=18.0,<19.0") |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If Odoo is not installed. |
Returns:
| Type | Description |
|---|---|
bool
|
True if current Odoo release matches the spec, False otherwise. |