Skip to content

odoo-addons-relative-import (ODW8150)

Preview (since 0.16.2.2) · Related issues · View source

Derived from the odoo linter.

Fix is sometimes available.

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Checks for odoo.addons.<this_module> imports, absolute imports of the very module the file itself belongs to.

Why is this bad?

A module importing itself by its own name (instead of using a relative import) breaks if the module is ever renamed or vendored under a different name.

Example

# in my_module/models/foo.py
from odoo.addons.my_module import models

Use instead:

from . import models

Fix safety

For a from odoo.addons.<module>... import ... statement the fix replaces the dotted path with the equivalent relative one, computed from the file's location inside the module; the imported module is the same, so the fix is safe. The other spellings — import odoo.addons.<module> and from odoo.addons import <module> — bind the absolute name and have no relative equivalent, so they get no fix.