Allow typing-related object imports

While our "no object imports" rule makes sense in most cases, it has
proven impractical when it comes to typing. Attempts to enforce this
here have resulted is absurdly long and difficult to comprehend function
signatures, or have necessitated unintuitive import aliasing. For
example, contrast:

    def process(
        data: collections.abc.Sequence[
            dict[str, collections.abc.Iterable[str]]
        ] | None = None,
    ) -> None: ...

with:

    def process(data: Sequence[dict[str, Iterable[str]]]) -> None:

We already gave exceptions for sqlalchemy, sqlalchemy-migrate, and i18n
packages to prevent code like this:

    query = query.filter(
        sql.or_(
          models.Foo.deleted == sql.false(), models.Bar.deleted.is_(None)
        )
    )
    if not query.count():
        msg = i18n._("a really bad error")
        raise Exception(msg)

rather than the more pleasing (and typical):

    query = query.filter(
        or_(models.Foo.deleted == sql.false(), models.Bar.deleted.is_(None))
    )
    if not query.count():
        msg = _("a really bad error")
        raise Exception(msg)

So this is merely a continuation.

While here, we also remove sqlalchemy-migrate from the list of
exceptions. No one is using this anymore since it doesn't support
sqlalchemy 2.x and has gone end-of-life.

Change-Id: Ifc45876e3d3655bca66ca5eb6d585a03cd15ab8d
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane
2025-11-19 16:37:22 +00:00
parent d27907accd
commit 4de6e790fb
3 changed files with 17 additions and 4 deletions
+6 -3
View File
@@ -22,6 +22,7 @@ Step 0
General
-------
- [H903] Use only UNIX style newlines (``\n``), not Windows style (``\r\n``)
- It is preferred to wrap long lines in parentheses and not a backslash
for line continuation.
@@ -68,9 +69,11 @@ Imports
(*) exceptions are:
- imports from ``migrate`` package
- imports from ``sqlalchemy`` package
- function imports from ``i18n`` module
- imports from stdlib ``collections.abc``, ``types``, and ``typing`` packages
- imports from third-party ``sqlalchemy`` package
- other objects that are only used for type hinting
- function imports from ``i18n`` module (this should be enforced at the project
level using the ``[hacking] import_exceptions`` config option)
Import order template
^^^^^^^^^^^^^^^^^^^^^
+3 -1
View File
@@ -75,8 +75,10 @@ CONF = config.Config('hacking')
DEFAULT_IMPORT_EXCEPTIONS = [
'collections.abc',
'sqlalchemy',
'migrate',
'types',
'typing'
]
IMPORT_EXCEPTIONS = CONF.get_multiple('import_exceptions', default=[])
@@ -0,0 +1,8 @@
---
upgrade:
- |
The default set of exceptions for the H301 (Do not import more than one
module per line) and H303 (Do not use wildcard ``*`` import) rules has
changed, with the end-of-life ``migrate`` package being removed and the
``collections.abc``, ``types``, and ``typing`` stdlib packages being
added.