Skip to content

Commit f3f9e34

Browse files
committed
Python: Update tests for py/import-own-module
So I've been thinking a bit about import pkg_ok.foo1 after reading the Python references for imports of submodules https://docs.python.org/3/reference/import.html#submodules > When a submodule is loaded using any mechanism (...) a binding is placed in the parent module’s namespace to the submodule object. For example, if package spam has a submodule foo, after importing spam.foo, spam will have an attribute foo which is bound to the submodule. That does at least explain what is going on here. I feel that import pkg_ok.foo1 might be a very contrived example. In principle it should be an alert, since the module pkg_ok ends up with an import of itself, but my gut feeling is that in practice it's not a very important piece of code to give alerts for. if we really care about giving these import related alerts, we could probably add a new query for this pattern, as it's kind of surprising that it works when you're just an ordinary python programmer.
1 parent 2bffbf0 commit f3f9e34

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
| imports_test.py:8:1:8:19 | Import | The module 'imports_test' imports itself. |
22
| pkg_notok/__init__.py:4:1:4:16 | Import | The module 'pkg_notok' imports itself. |
3-
| pkg_notok/__init__.py:6:1:6:25 | Import | The module 'pkg_notok' imports itself. |
4-
| pkg_notok/__init__.py:7:1:7:37 | Import | The module 'pkg_notok' imports itself. |
5-
| pkg_ok/__init__.py:2:1:2:18 | Import | The module 'pkg_ok' imports itself. |
6-
| pkg_ok/__init__.py:4:1:4:23 | Import | The module 'pkg_ok' imports itself. |
7-
| pkg_ok/__init__.py:5:1:5:28 | Import | The module 'pkg_ok' imports itself. |
8-
| pkg_ok/__init__.py:7:1:7:18 | Import | The module 'pkg_ok' imports itself. |
9-
| pkg_ok/__init__.py:8:1:8:22 | Import | The module 'pkg_ok' imports itself. |
3+
| pkg_notok/__init__.py:10:1:10:20 | Import | The module 'pkg_notok' imports itself. |
4+
| pkg_notok/__init__.py:12:1:12:25 | Import | The module 'pkg_notok' imports itself. |
5+
| pkg_notok/__init__.py:13:1:13:37 | Import | The module 'pkg_notok' imports itself. |
6+
| pkg_ok/__init__.py:1:1:1:26 | Import | The module 'pkg_ok' imports itself. |
7+
| pkg_ok/__init__.py:3:1:3:23 | Import | The module 'pkg_ok' imports itself. |
8+
| pkg_ok/__init__.py:4:1:4:28 | Import | The module 'pkg_ok' imports itself. |
9+
| pkg_ok/__init__.py:6:1:6:18 | Import | The module 'pkg_ok' imports itself. |
10+
| pkg_ok/__init__.py:7:1:7:22 | Import | The module 'pkg_ok' imports itself. |

python/ql/test/query-tests/Imports/PyCheckerTests/pkg_notok/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ class Foo(object):
33

44
import pkg_notok
55

6+
# This import is a bit tricky. It will make `bar` available in as `pkg_notok.bar` as a
7+
# side effect (see https://docs.python.org/3/reference/import.html#submodules), but the
8+
# *import* will add a binding to `pkg_notok` to the current scope -- so technically the
9+
# module imports itself.
10+
import pkg_notok.bar
11+
612
from pkg_notok import Foo
713
from pkg_notok import Foo as NotOkFoo
814
from pkg_notok import * # TODO: TN
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Bar():
2+
pass

python/ql/test/query-tests/Imports/PyCheckerTests/pkg_ok/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# This import makes `pkg_ok` available, but also `foo1` (surprising as it may be)
2-
import pkg_ok.foo1 # TODO: FP
1+
import pkg_ok.foo1 as foo1 # TODO: FP
32

43
from pkg_ok import foo2 # TODO: FP
54
from pkg_ok.foo3 import Foo3 # TODO: FP

0 commit comments

Comments
 (0)