[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v2 04/38] qapi: Prefer explicit relative imports
From: |
Cleber Rosa |
Subject: |
Re: [PATCH v2 04/38] qapi: Prefer explicit relative imports |
Date: |
Wed, 23 Sep 2020 09:18:25 -0400 |
On Tue, Sep 22, 2020 at 05:00:27PM -0400, John Snow wrote:
> All of the QAPI include statements are changed to be package-aware, as
> explicit relative imports.
>
> A quirk of Python packages is that the name of the package exists only
> *outside* of the package. This means that to a module inside of the qapi
> folder, there is inherently no such thing as the "qapi" package. The
> reason these imports work is because the "qapi" package exists in the
> context of the caller -- the execution shim, where sys.path includes a
> directory that has a 'qapi' folder in it.
>
> When we write "from qapi import sibling", we are NOT referencing the folder
> 'qapi', but rather "any package named qapi in sys.path". If you should
> so happen to have a 'qapi' package in your path, it will use *that*
> package.
>
> When we write "from .sibling import foo", we always reference explicitly
> our sibling module; guaranteeing consistency in *where* we are importing
> these modules from.
>
> This can be useful when working with virtual environments and packages
> in development mode. In development mode, a package is installed as a
> series of symlinks that forwards to your same source files. The problem
> arises because code quality checkers will follow "import qapi.x" to the
> "installed" version instead of the sibling file and -- even though they
> are the same file -- they have different module paths, and this causes
> cyclic import problems, false positive type mismatch errors, and more.
>
> It can also be useful when dealing with hierarchical packages, e.g. if
> we allow qemu.core.qmp, qemu.qapi.parser, etc.
>
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
> scripts/qapi/commands.py | 4 ++--
> scripts/qapi/doc.py | 2 +-
> scripts/qapi/events.py | 8 ++++----
> scripts/qapi/expr.py | 4 ++--
> scripts/qapi/gen.py | 4 ++--
> scripts/qapi/introspect.py | 8 ++++----
> scripts/qapi/main.py | 16 ++++++++--------
> scripts/qapi/parser.py | 4 ++--
> scripts/qapi/schema.py | 8 ++++----
> scripts/qapi/types.py | 6 +++---
> scripts/qapi/visit.py | 6 +++---
> 11 files changed, 35 insertions(+), 35 deletions(-)
>
Relative imports are a source of heated debates, but when properly
used in a self contained module like here, they are very posititive
IMO.
Reviewed-by: Cleber Rosa <crosa@redhat.com>
signature.asc
Description: PGP signature
- Re: [PATCH v2 05/38] qapi: Remove wildcard includes, (continued)
[PATCH v2 12/38] qapi/common.py: check with pylint, John Snow, 2020/09/22
[PATCH v2 04/38] qapi: Prefer explicit relative imports, John Snow, 2020/09/22
[PATCH v2 08/38] qapi/common.py: Remove python compatibility workaround, John Snow, 2020/09/22
[PATCH v2 17/38] qapi/events.py: add type hint annotations, John Snow, 2020/09/22