Skip to content

scalar-in-domain (ODW9505)

Preview (since 0.17.0.36) · 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 a domain condition whose operator is in or not in and whose value is a single value rather than a list, such as ("country_ids", "in", record.country_id.id).

It looks at every (field, "in" | "not in", value) triple, so it covers the domains passed to search, search_count, search_read, filtered_domain, _search or domain=, and the ones kept in a variable, in a Python file and in the domain of an XML file: the domain and filter_domain attributes, <attribute name="domain">, the domain and domain_force fields of a record, and attrs. XML files are linted when they are passed to Ruff or matched by extend-include.

The value is reported when it is certainly a scalar:

  • a literal: "draft", 5, True, False, None;
  • uid, or an attribute named uid or _uid (self.env.uid);
  • the id of a record: partner.id, user.id, self.env.company.id;
  • a local name whose only assignment in the same scope is one of the above.

In an XML file, where the names are the fields of the form, a bare name that is id, active_id or ends in _id (donation_type_id) is reported too: it holds the id of a many2one, or False. The same goes for a domain written as a string in the domain= argument of a Python field (fields.Many2one(domain="[('company_ids', 'in', company_id)]")), which Odoo evaluates with the fields of the record. A string that is an f-string, is concatenated or holds an escape is not read.

Everything else is left alone, because it may well be a collection: a list, tuple, set or dict display, a comprehension, a recordset, record.ids, records.mapped(...), any call, subscript or conditional expression, and any other name. A Python name is never guessed from its spelling: company_id = self.env.company holds a recordset, which a domain accepts.

Why is this bad?

in and not in compare a field with a collection. Since 19.0 a domain that gets a scalar logs The domain condition [...] should have a list value. and wraps it in a list, which hides the mistake. A falsy scalar (False, None, 0, "", or the id of an empty recordset) never reaches that step: the condition is replaced by a constant, always false for in and always true for not in, whatever the records hold.

The step is _optimize_in_set.

Example

domain = [("country_ids", "in", orphan.country_id.id)]

Use instead:

domain = [("country_ids", "in", orphan.country_id.ids)]

Fix safety

The fix is offered for Python only, and only when the replacement means the same thing: a truthy literal is wrapped in a list (safe), and .id becomes .ids or uid is wrapped in a list (unsafe, because the object may not be a record, and for a record that is new .id is a NewId while .ids is not). A falsy literal and a name are reported without a fix: [False] is not the same condition as False.