From 99a0c45aef6f0fd64f86a2048b999bdb7d74377c Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 23 May 2023 22:03:50 +1000 Subject: [PATCH] tests/import/import_pkg9.py: Add test for subpackage attribute. When foo.bar is imported, bar is added as an attribute to foo. Previously this happened on every import, but should only happen on first import. This verifies the behavior for relative imports and overriding. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- tests/import/import_pkg9.py | 16 ++++++++++++++++ tests/import/pkg9/__init__.py | 5 +++++ tests/import/pkg9/mod1.py | 2 ++ tests/import/pkg9/mod2.py | 1 + 4 files changed, 24 insertions(+) create mode 100644 tests/import/import_pkg9.py create mode 100644 tests/import/pkg9/__init__.py create mode 100644 tests/import/pkg9/mod1.py create mode 100644 tests/import/pkg9/mod2.py diff --git a/tests/import/import_pkg9.py b/tests/import/import_pkg9.py new file mode 100644 index 0000000000..4de028494f --- /dev/null +++ b/tests/import/import_pkg9.py @@ -0,0 +1,16 @@ +# tests that import only sets subpackage attribute on first import + +import pkg9 + +pkg9.mod1() +pkg9.mod2() + +import pkg9.mod1 + +pkg9.mod1() +pkg9.mod2() + +import pkg9.mod2 + +pkg9.mod1() +print(pkg9.mod2.__name__, type(pkg9.mod2).__name__) diff --git a/tests/import/pkg9/__init__.py b/tests/import/pkg9/__init__.py new file mode 100644 index 0000000000..5d08d4b4a9 --- /dev/null +++ b/tests/import/pkg9/__init__.py @@ -0,0 +1,5 @@ +from .mod1 import mod1 + + +def mod2(): + print("mod2") diff --git a/tests/import/pkg9/mod1.py b/tests/import/pkg9/mod1.py new file mode 100644 index 0000000000..7e7066bada --- /dev/null +++ b/tests/import/pkg9/mod1.py @@ -0,0 +1,2 @@ +def mod1(): + print("mod1") diff --git a/tests/import/pkg9/mod2.py b/tests/import/pkg9/mod2.py new file mode 100644 index 0000000000..f4b3e265fb --- /dev/null +++ b/tests/import/pkg9/mod2.py @@ -0,0 +1 @@ +from . import mod2