[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 05/34] qapi: Prefer explicit relative imports
From: |
Markus Armbruster |
Subject: |
[PULL 05/34] qapi: Prefer explicit relative imports |
Date: |
Sat, 10 Oct 2020 11:54:35 +0200 |
From: John Snow <jsnow@redhat.com>
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>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20201009161558.107041-6-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@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(-)
diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py
index 6e6fc94a14..1f43a0a34e 100644
--- a/scripts/qapi/commands.py
+++ b/scripts/qapi/commands.py
@@ -13,8 +13,8 @@ This work is licensed under the terms of the GNU GPL, version
2.
See the COPYING file in the top-level directory.
"""
-from qapi.common import *
-from qapi.gen import QAPIGenCCode, QAPISchemaModularCVisitor, ifcontext
+from .common import *
+from .gen import QAPIGenCCode, QAPISchemaModularCVisitor, ifcontext
def gen_command_decl(name, arg_type, boxed, ret_type):
diff --git a/scripts/qapi/events.py b/scripts/qapi/events.py
index b544af5a1c..0467272438 100644
--- a/scripts/qapi/events.py
+++ b/scripts/qapi/events.py
@@ -12,10 +12,10 @@ This work is licensed under the terms of the GNU GPL,
version 2.
See the COPYING file in the top-level directory.
"""
-from qapi.common import *
-from qapi.gen import QAPISchemaModularCVisitor, ifcontext
-from qapi.schema import QAPISchemaEnumMember
-from qapi.types import gen_enum, gen_enum_lookup
+from .common import *
+from .gen import QAPISchemaModularCVisitor, ifcontext
+from .schema import QAPISchemaEnumMember
+from .types import gen_enum, gen_enum_lookup
def build_event_send_proto(name, arg_type, boxed):
diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py
index a15c1fb474..bb4dc55f56 100644
--- a/scripts/qapi/expr.py
+++ b/scripts/qapi/expr.py
@@ -16,8 +16,8 @@
import re
from collections import OrderedDict
-from qapi.common import c_name
-from qapi.error import QAPISemError
+from .common import c_name
+from .error import QAPISemError
# Names must be letters, numbers, -, and _. They must start with letter,
diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py
index dc7b94aa11..fc57fdca5b 100644
--- a/scripts/qapi/gen.py
+++ b/scripts/qapi/gen.py
@@ -17,8 +17,8 @@ import os
import re
from contextlib import contextmanager
-from qapi.common import *
-from qapi.schema import QAPISchemaVisitor
+from .common import *
+from .schema import QAPISchemaVisitor
class QAPIGen:
diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py
index 5907b09cd5..6c82d9d95f 100644
--- a/scripts/qapi/introspect.py
+++ b/scripts/qapi/introspect.py
@@ -10,10 +10,10 @@ This work is licensed under the terms of the GNU GPL,
version 2.
See the COPYING file in the top-level directory.
"""
-from qapi.common import *
-from qapi.gen import QAPISchemaMonolithicCVisitor
-from qapi.schema import (QAPISchemaArrayType, QAPISchemaBuiltinType,
- QAPISchemaType)
+from .common import *
+from .gen import QAPISchemaMonolithicCVisitor
+from .schema import (QAPISchemaArrayType, QAPISchemaBuiltinType,
+ QAPISchemaType)
def _make_tree(obj, ifcond, features, extra=None):
diff --git a/scripts/qapi/main.py b/scripts/qapi/main.py
index 92a2a31ca1..42517210b8 100644
--- a/scripts/qapi/main.py
+++ b/scripts/qapi/main.py
@@ -12,13 +12,13 @@ import re
import sys
from typing import Optional
-from qapi.commands import gen_commands
-from qapi.error import QAPIError
-from qapi.events import gen_events
-from qapi.introspect import gen_introspect
-from qapi.schema import QAPISchema
-from qapi.types import gen_types
-from qapi.visit import gen_visit
+from .commands import gen_commands
+from .error import QAPIError
+from .events import gen_events
+from .introspect import gen_introspect
+from .schema import QAPISchema
+from .types import gen_types
+from .visit import gen_visit
def invalid_prefix_char(prefix: str) -> Optional[str]:
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 9d1a3e2eea..7298f5dbd1 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -18,8 +18,8 @@ import os
import re
from collections import OrderedDict
-from qapi.error import QAPIParseError, QAPISemError
-from qapi.source import QAPISourceInfo
+from .error import QAPIParseError, QAPISemError
+from .source import QAPISourceInfo
class QAPISchemaParser:
diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index d1307ec661..676185d1a7 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -18,10 +18,10 @@ import os
import re
from collections import OrderedDict
-from qapi.common import c_name, pointer_suffix
-from qapi.error import QAPIError, QAPISemError
-from qapi.expr import check_exprs
-from qapi.parser import QAPISchemaParser
+from .common import c_name, pointer_suffix
+from .error import QAPIError, QAPISemError
+from .expr import check_exprs
+from .parser import QAPISchemaParser
class QAPISchemaEntity:
diff --git a/scripts/qapi/types.py b/scripts/qapi/types.py
index 3640f17cd6..ca9a5aacb3 100644
--- a/scripts/qapi/types.py
+++ b/scripts/qapi/types.py
@@ -13,9 +13,9 @@ This work is licensed under the terms of the GNU GPL, version
2.
# See the COPYING file in the top-level directory.
"""
-from qapi.common import *
-from qapi.gen import QAPISchemaModularCVisitor, ifcontext
-from qapi.schema import QAPISchemaEnumMember, QAPISchemaObjectType
+from .common import *
+from .gen import QAPISchemaModularCVisitor, ifcontext
+from .schema import QAPISchemaEnumMember, QAPISchemaObjectType
# variants must be emitted before their container; track what has already
diff --git a/scripts/qapi/visit.py b/scripts/qapi/visit.py
index cdabc5fa28..7850f6e848 100644
--- a/scripts/qapi/visit.py
+++ b/scripts/qapi/visit.py
@@ -13,9 +13,9 @@ This work is licensed under the terms of the GNU GPL, version
2.
See the COPYING file in the top-level directory.
"""
-from qapi.common import *
-from qapi.gen import QAPISchemaModularCVisitor, ifcontext
-from qapi.schema import QAPISchemaObjectType
+from .common import *
+from .gen import QAPISchemaModularCVisitor, ifcontext
+from .schema import QAPISchemaObjectType
def gen_visit_decl(name, scalar=False):
--
2.26.2
- [PULL 00/34] QAPI patches patches for 2020-10-10, Markus Armbruster, 2020/10/10
- [PULL 13/34] qapi/common.py: Replace one-letter 'c' variable, Markus Armbruster, 2020/10/10
- [PULL 03/34] qapi-gen: Separate arg-parsing from generation, Markus Armbruster, 2020/10/10
- [PULL 01/34] docs: repair broken references, Markus Armbruster, 2020/10/10
- [PULL 07/34] qapi: enforce import order/styling with isort, Markus Armbruster, 2020/10/10
- [PULL 08/34] qapi: delint using flake8, Markus Armbruster, 2020/10/10
- [PULL 11/34] qapi/common.py: Add indent manager, Markus Armbruster, 2020/10/10
- [PULL 12/34] qapi/common.py: delint with pylint, Markus Armbruster, 2020/10/10
- [PULL 02/34] qapi: modify docstrings to be sphinx-compatible, Markus Armbruster, 2020/10/10
- [PULL 15/34] qapi/common.py: add type hint annotations, Markus Armbruster, 2020/10/10
- [PULL 05/34] qapi: Prefer explicit relative imports,
Markus Armbruster <=
- [PULL 14/34] qapi/common.py: check with pylint, Markus Armbruster, 2020/10/10
- [PULL 10/34] qapi/common.py: Remove python compatibility workaround, Markus Armbruster, 2020/10/10
- [PULL 04/34] qapi: move generator entrypoint into package, Markus Armbruster, 2020/10/10
- [PULL 06/34] qapi: Remove wildcard includes, Markus Armbruster, 2020/10/10
- [PULL 27/34] qapi/gen.py: Remove unused parameter, Markus Armbruster, 2020/10/10
- [PULL 18/34] qapi: establish mypy type-checking baseline, Markus Armbruster, 2020/10/10
- [PULL 24/34] qapi/source.py: delint with pylint, Markus Armbruster, 2020/10/10
- [PULL 28/34] qapi/gen.py: update write() to be more idiomatic, Markus Armbruster, 2020/10/10
- [PULL 21/34] qapi/commands.py: Don't re-bind to variable of different type, Markus Armbruster, 2020/10/10
- [PULL 17/34] qapi/common.py: move build_params into gen.py, Markus Armbruster, 2020/10/10