-
Notifications
You must be signed in to change notification settings - Fork 5
Odoo Type Hints
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.
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.
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.
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.
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.
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.
Code completion in values declaration:
Code completion in updating values:
Code completion in return expression: