Skip to content

Odoo Type Hints

Trịnh Anh Ngọc edited this page Mar 5, 2024 · 2 revisions

Type hints for recordsets

Use imported model class definition

image This is the recommended way to do it. When you use a model class definition (origin or inheritances) to specify type for a object, the Odoo plugin will infer it as the corresponding Odoo model instead of just the class definition. This means all inheritance features will be taken into account for the type. You can also use this to specify type in docstrings and comments.

Use the magic odoo.model.*

image If you feel too lazy to import a class and use it for type hint, you can use the magic alias odoo.model.*. For example, odoo.model.res_partner would be understood as the model res.partner. Because the odoo.model.* doesn't exist at runtime, you must put it in a string if using the annotation syntax as the above screenshot. For docstrings and comments, you can use it without enclosing it in quotes.

Use model name directly

image If your type hint is just a simple model name, you can use the model name directly. This only applies for type hints in docstrings and comments.


Type hints for recordset dict values

As you know, it's common in Odoo to create specific methods for preparing data before creating or updating recordsets. If IDEs can know about the model for which you are preparing data, it will provide code completion and navigation for fields, it will also warn if a field does not exist so you can quickly fix it.

Use Annotated in conjunction with the magic odoo.values.*

image If you are using Python >= 3.9 then this syntax is recommended because for unsupported IDEs, this syntax will be treated like the normal dict type. You can learn more about Annotated here.

Use the magic odoo.values.* directly

image This syntax is more concise but currently it only works for PyCharm with the Odoo plugin. Unsupported IDEs will be confused and may treat it as unknown type.

Examples

Code completion in values declaration: image

Code completion in updating values: image

Code completion in return expression: image