Skip to main content

Tool to generate Odoo model type classes for better IDE support

Project description

Pre-commit Status

odoo-typing-classes-generator

Trying to Improve the Developer Experience by Generating Typing Classes for Odoo Models

This is project at this stage is only a proof of concept.

For a given Odoo module, this tool scans its dependencies and introspects any classes inheriting from odoo.models.BaseModel in order to generate corresponding typing classes for every Odoo model.

Installation

$ uv sync
$ source .venv/bin/activate
$ pip install -e .
$ odoo-typing-classes-generator --help

Usage: Generating the Typing Classes

$ odoo-typing-classes-generator --odoo-path /path/to/odoo --module-path /path/to/module --output-file /path/to/output/file.py

[Required] --modules TEXT

Comma-separated list of Comma-separated list of Odoo modules to generate typing classes for. Typing classes

[Required] --addons-path TEXT

Path where the modules are located, relative to the current working directory.

[Flag] --stub-mode

If set, the definitions will be put in the stub files typing/models.pyi. By default, the typing/models.py files will be created empty if they don't exist yet.

You will have to manually add the classes you want to use for typing, in the format:

class ClassName:
    pass

You can check in the models.pyi to see the available classes, all of them have a field _name = "[Odoo Model Name]" you can search for. Alternatively, if you have an Odoo model abc.def_ghi, the typing class name will be AbcDefGhi.

Note: You should most likely avoid to put the stub files into your VCS.

[Flag] --generate-all-classes

This option is only available when --stub-mode is set. When set, the typing/models.py files will be created with all the available classes. This is useful if you want to have all the classes available for typing, even if you don't use them all.

Warning: this can create a very large file, depending on the number of models in the module and its dependencies.

Usage: Use the Typing Classes In Code

It is very important to import the typing.models module for this to work (see reasons below). You can, of course, use an alias to avoid name collisions.

For example:

from odoo import models

from odoo.addons.foobar.typing import models as odoo_typing

class ResPartner(models.Model):
    _inherit = "res.partner"

    def test(self, companies: odoo_typing.ResCompany) -> odoo_typing.ResUsers:
        ...

Warning: in stub mode, if you import the typing class directly from the typing.models module, and you will have no autocomplete suggestion from your IDE*.

*I don't know why this is the case, plus I only tested this script with IntelliJ IDEA 2025.2.1 (Ultimate Edition), if you have an explanation, you can add it here, thanks! ❤️

In Case of Missing Classes in the typing/models.py File

With the --stub-mode Option and without the --generate-all-classes Option

You can just add them manually. If the autocomplete still doesn't work for one or more classes, check if they are present in the stub file. If they are absent, check if you have missing dependencies in your manifest.

Without the --stub-mode Option or with the --generate-all-classes Option

Check if you have missing dependencies in your manifest.

General Process

  1. Scan the module and all its dependencies recursively by reading the manifest files, while doing that every time we find an Odoo model, we collect the following information:
    • whether the model is abstract, transient or concrete;
    • the list of inherited models;
    • all defined Odoo fields;
    • all the non-private / named-mangled methods and functions with their signatures;
    • for basic fields we map them to built in types;
    • for the related fields we only store the "related" value; and
    • for other fields referencing another Odoo model, we only store the model name;
  2. then, for each Odoo model, we aggregate all the definitions: the list of inherited models, the list of fields, the list of functions and methods;
  3. then, the type of all related fields is resolved; and
  4. finally, we write the typing classes into a file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

odoo_typing_classes_generator-0.0.1a4.tar.gz (117.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

odoo_typing_classes_generator-0.0.1a4-py3-none-any.whl (49.9 kB view details)

Uploaded Python 3

File details

Details for the file odoo_typing_classes_generator-0.0.1a4.tar.gz.

File metadata

File hashes

Hashes for odoo_typing_classes_generator-0.0.1a4.tar.gz
Algorithm Hash digest
SHA256 da92e381d99cc8bdc52d954c965669c2388ef360a127451744e225808fdec2f8
MD5 994ad9100fff6319408a55fac16ad26b
BLAKE2b-256 a59cdd453db9b42ff8b84ca48cf849e7ea0d1e8af8e8b044bc2747d2b92a29f7

See more details on using hashes here.

File details

Details for the file odoo_typing_classes_generator-0.0.1a4-py3-none-any.whl.

File metadata

File hashes

Hashes for odoo_typing_classes_generator-0.0.1a4-py3-none-any.whl
Algorithm Hash digest
SHA256 f50e232d574fd789312bf69264218130a7818ee54338029724e4be7df7917b5d
MD5 8f6da5fbdcbecd95397cd9b73d81378b
BLAKE2b-256 129d16016b253d47aaed2e62e893fea72bd3355ddeab76238fa4418d9112bd80

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page