[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v5 05/36] qapi: Prefer explicit relative imports
From: |
Philippe Mathieu-Daudé |
Subject: |
Re: [PATCH v5 05/36] qapi: Prefer explicit relative imports |
Date: |
Tue, 6 Oct 2020 13:33:35 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 |
On 10/5/20 9:51 PM, 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>
> Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
> Reviewed-by: Cleber Rosa <crosa@redhat.com>
> ---
> scripts/qapi/commands.py | 4 ++--
> scripts/qapi/events.py | 8 ++++----
> scripts/qapi/expr.py | 4 ++--
> scripts/qapi/gen.py | 4 ++--
> scripts/qapi/introspect.py | 8 ++++----
> scripts/qapi/main.py | 14 +++++++-------
> scripts/qapi/parser.py | 4 ++--
> scripts/qapi/schema.py | 8 ++++----
> scripts/qapi/types.py | 6 +++---
> scripts/qapi/visit.py | 6 +++---
> 10 files changed, 33 insertions(+), 33 deletions(-)
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
- Re: [PATCH v5 03/36] qapi-gen: Separate arg-parsing from generation, (continued)
[PATCH v5 07/36] qapi: enforce import order/styling with isort, John Snow, 2020/10/05
[PATCH v5 05/36] qapi: Prefer explicit relative imports, John Snow, 2020/10/05
- Re: [PATCH v5 05/36] qapi: Prefer explicit relative imports,
Philippe Mathieu-Daudé <=
[PATCH v5 06/36] qapi: Remove wildcard includes, John Snow, 2020/10/05
[PATCH v5 09/36] qapi: add pylintrc, John Snow, 2020/10/05
[PATCH v5 08/36] qapi: delint using flake8, John Snow, 2020/10/05
[PATCH v5 10/36] qapi/common.py: Remove python compatibility workaround, John Snow, 2020/10/05
[PATCH v5 11/36] qapi/common.py: Add indent manager, John Snow, 2020/10/05