Skip to content

Commit a033bdb

Browse files
author
Callum Dickinson
committed
Update type hints for Python 3.10+
Update all type annotations and documentation in `openstack-odooclient` to use the new features available in Python 3.10 and later.
1 parent 7a1dfbf commit a033bdb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+606
-703
lines changed

docs/managers/account-move.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ All specified records will be processed in a single request.
6060
```python
6161
send_openstack_invoice_email(
6262
account_move: int | AccountMove,
63-
email_ctx: Optional[Mapping[str, Any]] = None,
63+
email_ctx: Mapping[str, Any] | None = None,
6464
) -> None
6565
```
6666

@@ -291,7 +291,7 @@ Change this draft account move (invoice) into "posted" state.
291291

292292
```python
293293
send_openstack_invoice_email(
294-
email_ctx: Optional[Mapping[str, Any]] = None,
294+
email_ctx: Mapping[str, Any] | None = None,
295295
) -> None
296296
```
297297

docs/managers/custom.md

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ the type hint should be defined as shown below.
180180
```python
181181
from __future__ import annotations
182182

183-
from typing import Literal, Union
183+
from typing import Literal
184184

185185
from openstack_odooclient import RecordBase
186186

187187
class CustomRecord(RecordBase["CustomRecordManager"]):
188-
custom_field: Union[str, Literal[False]]
188+
custom_field: str | Literal[False]
189189
"""Description of the field."""
190190
```
191191

@@ -197,12 +197,10 @@ the type hint should be defined as shown below instead.
197197
```python
198198
from __future__ import annotations
199199

200-
from typing import Optional
201-
202200
from openstack_odooclient import RecordBase
203201

204202
class CustomRecord(RecordBase["CustomRecordManager"]):
205-
custom_field: Optional[str]
203+
custom_field: str | None
206204
"""Description of the field."""
207205
```
208206

@@ -223,8 +221,9 @@ The type of the alias **must** match the type of the target field.
223221
```python
224222
from __future__ import annotations
225223

224+
from typing import Annotated
225+
226226
from openstack_odooclient import FieldAlias, RecordBase
227-
from typing_extensions import Annotated
228227

229228
class CustomRecord(RecordBase["CustomRecordManager"]):
230229
custom_field: str
@@ -265,8 +264,9 @@ The first is to expose the record's ID directly as an integer.
265264
```python
266265
from __future__ import annotations
267266

267+
from typing import Annotated
268+
268269
from openstack_odooclient import ModelRef, RecordBase, User
269-
from typing_extensions import Annotated
270270

271271
class CustomRecord(RecordBase["CustomRecordManager"]):
272272
user_id: Annotated[int, ModelRef("user_id", User)]
@@ -278,8 +278,9 @@ The second is to expose the target record's display name as a string.
278278
```python
279279
from __future__ import annotations
280280

281+
from typing import Annotated
282+
281283
from openstack_odooclient import ModelRef, RecordBase, User
282-
from typing_extensions import Annotated
283284

284285
class CustomRecord(RecordBase["CustomRecordManager"]):
285286
user_name: Annotated[str, ModelRef("user_id", User)]
@@ -291,8 +292,9 @@ The third and final one is to define the target record itself as a record object
291292
```python
292293
from __future__ import annotations
293294

295+
from typing import Annotated
296+
294297
from openstack_odooclient import ModelRef, RecordBase, User
295-
from typing_extensions import Annotated
296298

297299
class CustomRecord(RecordBase["CustomRecordManager"]):
298300
user: Annotated[User, ModelRef("user_id", User)]
@@ -315,8 +317,9 @@ The final result should be the following fields defined on your record class.
315317
```python
316318
from __future__ import annotations
317319

320+
from typing import Annotated
321+
318322
from openstack_odooclient import ModelRef, RecordBase, User
319-
from typing_extensions import Annotated
320323

321324
class CustomRecord(RecordBase["CustomRecordManager"]):
322325
user_id: Annotated[int, ModelRef("user_id", User)]
@@ -339,25 +342,24 @@ and [creating new records](index.md#create).
339342
The record ID or the record object can be passed directly to those methods
340343
(the record display name is not guaranteed to be unique, and thus, not accepted).
341344

342-
Record references can be made optional by encasing the type hint with `Optional`.
345+
Record references can be made optional by unioning with `None`.
343346
If the record reference is not set, `None` will be returned.
344347

345348
```python
346349
from __future__ import annotations
347350

348-
from typing import Optional
351+
from typing import Annotated
349352

350353
from openstack_odooclient import ModelRef, RecordBase, User
351-
from typing_extensions import Annotated
352354

353355
class CustomRecord(RecordBase["CustomRecordManager"]):
354-
user_id: Annotated[Optional[int], ModelRef("user_id", User)]
356+
user_id: Annotated[int | None, ModelRef("user_id", User)]
355357
"""ID for the user that owns this record."""
356358

357-
user_name: Annotated[Optional[str], ModelRef("user_id", User)]
359+
user_name: Annotated[str | None, ModelRef("user_id", User)]
358360
"""Name of the user that owns this record."""
359361

360-
user: Annotated[Optional[User], ModelRef("user_id", User)]
362+
user: Annotated[User | None, ModelRef("user_id", User)]
361363
"""The user that owns this record.
362364
363365
This fetches the full record from Odoo once,
@@ -371,19 +373,19 @@ In the below example, `Self` resolves to `CustomRecord`.
371373
```python
372374
from __future__ import annotations
373375

374-
from typing import Optional
376+
from typing import Annotated
375377

376378
from openstack_odooclient import ModelRef, RecordBase
377-
from typing_extensions import Annotated, Self
379+
from typing_extensions import Self
378380

379381
class CustomRecord(RecordBase["CustomRecordManager"]):
380-
record_id: Annotated[Optional[int], ModelRef("user_id", Self)]
382+
record_id: Annotated[int | None, ModelRef("user_id", Self)]
381383
"""ID for the record related to this one, if set.."""
382384

383-
record_name: Annotated[Optional[str], ModelRef("user_id", Self)]
385+
record_name: Annotated[str | None, ModelRef("user_id", Self)]
384386
"""Name of the record related to this one, if set."""
385387

386-
record: Annotated[Optional[Self], ModelRef("user_id", Self)]
388+
record: Annotated[Self | None, ModelRef("user_id", Self)]
387389
"""The record related to this one, if set.
388390
389391
This fetches the full record from Odoo once,
@@ -408,13 +410,12 @@ The first is to expose the record IDs directly as a list of integers.
408410
```python
409411
from __future__ import annotations
410412

411-
from typing import List
413+
from typing import Annotated
412414

413415
from openstack_odooclient import ModelRef, RecordBase, Product
414-
from typing_extensions import Annotated
415416

416417
class CustomRecord(RecordBase["CustomRecordManager"]):
417-
product_ids: Annotated[List[int], ModelRef("product_id", Product)]
418+
product_ids: Annotated[list[int], ModelRef("product_id", Product)]
418419
"""The list of IDs for the products to use."""
419420
```
420421

@@ -423,13 +424,12 @@ The second and final one is to expose the records as a list of record objects.
423424
```python
424425
from __future__ import annotations
425426

426-
from typing import List
427+
from typing import Annotated
427428

428429
from openstack_odooclient import ModelRef, RecordBase, Product
429-
from typing_extensions import Annotated
430430

431431
class CustomRecord(RecordBase["CustomRecordManager"]):
432-
products: Annotated[List[Product], ModelRef("product_id", Product)]
432+
products: Annotated[list[Product], ModelRef("product_id", Product)]
433433
"""The list of products to use.
434434
435435
This fetches the full records from Odoo once,
@@ -449,16 +449,15 @@ The final result should be the following fields defined on your record class.
449449
```python
450450
from __future__ import annotations
451451

452-
from typing import List
452+
from typing import Annotated
453453

454454
from openstack_odooclient import ModelRef, RecordBase, Product
455-
from typing_extensions import Annotated
456455

457456
class CustomRecord(RecordBase["CustomRecordManager"]):
458-
product_ids: Annotated[List[int], ModelRef("product_id", Product)]
457+
product_ids: Annotated[list[int], ModelRef("product_id", Product)]
459458
"""The list of IDs for the products to use."""
460459

461-
products: Annotated[List[Product], ModelRef("product_id", Product)]
460+
products: Annotated[list[Product], ModelRef("product_id", Product)]
462461
"""The list of products to use.
463462
464463
This fetches the full records from Odoo once,
@@ -478,16 +477,16 @@ In the below example, `Self` resolves to `CustomRecord`.
478477
```python
479478
from __future__ import annotations
480479

481-
from typing import List
480+
from typing import Annotated
482481

483482
from openstack_odooclient import ModelRef, RecordBase
484-
from typing_extensions import Annotated, Self
483+
from typing_extensions import Self
485484

486485
class CustomRecord(RecordBase["CustomRecordManager"]):
487-
child_ids: Annotated[List[int], ModelRef("child_id", Self)]
486+
child_ids: Annotated[list[int], ModelRef("child_id", Self)]
488487
"""The list of IDs for the child records."""
489488

490-
children: Annotated[List[Self], ModelRef("child_id", Self)]
489+
children: Annotated[list[Self], ModelRef("child_id", Self)]
491490
"""The list of child records.
492491
493492
This fetches the full records from Odoo once,
@@ -511,16 +510,15 @@ Below is an example of two record classes that correctly reference each other.
511510
```python title="parent.py"
512511
from __future__ import annotations
513512

514-
from typing import List
513+
from typing import Annotated
515514

516515
from openstack_odooclient import ModelRef, RecordBase, RecordManagerBase
517-
from typing_extensions import Annotated
518516

519517
class Parent(RecordBase["ParentManager"]):
520-
child_ids: Annotated[List[int], ModelRef("child_id", Child)]
518+
child_ids: Annotated[list[int], ModelRef("child_id", Child)]
521519
"""The list of IDs for the children records."""
522520

523-
children: Annotated[List[Parent], ModelRef("child_id", Child)]
521+
children: Annotated[list[Parent], ModelRef("child_id", Child)]
524522
"""The list of children records.
525523
526524
This fetches the full records from Odoo once,
@@ -537,19 +535,18 @@ from .child import Child # noqa: E402
537535
```python title="child.py"
538536
from __future__ import annotations
539537

540-
from typing import Optional
538+
from typing import Annotated
541539

542540
from openstack_odooclient import ModelRef, RecordBase, RecordManagerBase
543-
from typing_extensions import Annotated
544541

545542
class Child(RecordBase["ChildManager"]):
546-
parent_id: Annotated[Optional[int], ModelRef("parent_id", Parent)]
543+
parent_id: Annotated[int | None, ModelRef("parent_id", Parent)]
547544
"""ID for the parent record, if it has one."""
548545

549-
parent_name: Annotated[Optional[str], ModelRef("parent_id", Parent)]
546+
parent_name: Annotated[str | None, ModelRef("parent_id", Parent)]
550547
"""Name of the parent record, if it has one."""
551548

552-
parent: Annotated[Optional[Parent], ModelRef("parent_id", Parent)]
549+
parent: Annotated[Parent | None, ModelRef("parent_id", Parent)]
553550
"""The parent record, if it has one.
554551
555552
This fetches the full record from Odoo once,
@@ -686,8 +683,6 @@ Below is a simple example of a custom record type and its manager class.
686683
```python
687684
from __future__ import annotations
688685

689-
from typing import List, Union
690-
691686
from openstack_odooclient import RecordBase, RecordManagerBase
692687

693688
class CustomRecord(RecordBase["CustomRecordManager"]):
@@ -710,8 +705,6 @@ This will allow manager methods to be used by calling them on the manager object
710705
```python
711706
from __future__ import annotations
712707

713-
from typing import List, Union
714-
715708
from openstack_odooclient import Client, RecordBase, RecordManagerBase
716709

717710
class CustomRecord(RecordBase["CustomRecordManager"]):
@@ -736,8 +729,6 @@ custom manager class.
736729
```python
737730
from __future__ import annotations
738731

739-
from typing import List, Union
740-
741732
from openstack_odooclient import RecordBase, RecordManagerBase
742733

743734
class CustomRecord(RecordBase["CustomRecordManager"]):
@@ -768,8 +759,6 @@ Methods can be defined on manager classes to provide additional functionality.
768759
```python
769760
from __future__ import annotations
770761

771-
from typing import List, Union
772-
773762
from openstack_odooclient import RecordBase, RecordManagerBase
774763

775764
class CustomRecord(RecordBase["CustomRecordManager"]):
@@ -780,10 +769,10 @@ class CustomRecordManager(RecordManagerBase[CustomRecord]):
780769
env_name = "custom.record"
781770
record_class = CustomRecord
782771

783-
def search_by_custom_field(self, custom_field: str) -> List[Record]:
772+
def search_by_custom_field(self, custom_field: str) -> list[Record]:
784773
return self.search([("custom_field", "ilike", custom_field)])
785774

786-
def perform_action(self, custom_record: Union[int, CustomRecord]) -> None:
775+
def perform_action(self, custom_record: int | CustomRecord) -> None:
787776
self._env.perform_action(
788777
(
789778
custom_record.id

docs/managers/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ a ``dict`` object, instead of a record object.
324324

325325
```python
326326
search(
327-
filters: Sequence[Tuple[str, str, Any] | Sequence[Any] | str] | None = None,
327+
filters: Sequence[tuple[str, str, Any] | Sequence[Any] | str] | None = None,
328328
fields: Iterable[str] | None = None,
329329
order: str | None = None,
330330
as_id: bool = False,
@@ -334,7 +334,7 @@ search(
334334

335335
```python
336336
search(
337-
filters: Sequence[Tuple[str, str, Any] | Sequence[Any] | str] | None = None,
337+
filters: Sequence[tuple[str, str, Any] | Sequence[Any] | str] | None = None,
338338
fields: Iterable[str] | None = None,
339339
order: str | None = None,
340340
as_id: bool = True,
@@ -344,7 +344,7 @@ search(
344344

345345
```python
346346
search(
347-
filters: Sequence[Tuple[str, str, Any] | Sequence[Any] | str] | None = None,
347+
filters: Sequence[tuple[str, str, Any] | Sequence[Any] | str] | None = None,
348348
fields: Iterable[str] | None = None,
349349
order: str | None = None,
350350
as_id: bool = False,
@@ -564,7 +564,7 @@ a list of `dict` objects, instead of record objects.
564564

565565
| Name | Type | Description | Default |
566566
|-----------|---------------------------------------------------------------|---------------------------------------------------|---------|
567-
| `filters` | `Sequence[Tuple[str, str, Any] | Sequence[Any] | str] | None` | Filters to query by (or `None` for no filters) | `None` |
567+
| `filters` | `Sequence[tuple[str, str, Any] | Sequence[Any] | str] | None` | Filters to query by (or `None` for no filters) | `None` |
568568
| `fields` | `Iterable[str] | None` | Fields to select (or `None` to select all fields) | `None` |
569569
| `order` | `str | None` | Field to order results by, if ordering results | `None` |
570570
| `as_id` | `bool` | Return the record IDs only | `False` |

0 commit comments

Comments
 (0)