Skip to content

Commit a8a7a59

Browse files
committed
Python: Add test for attribute name clash
1 parent b540eb0 commit a8a7a59

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from trace import *
2+
enter(__file__)
3+
4+
clashing_attr = "clashing_attr"
5+
6+
exit(__file__)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from trace import *
2+
enter(__file__)
3+
4+
exit(__file__)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from trace import *
2+
enter(__file__)
3+
4+
exit(__file__)

python/ql/test/experimental/import-resolution/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
check("foo_alias.foo_attr", foo_alias.foo_attr, "foo_attr", globals()) #$ prints=foo_attr
3636

3737
# A reference to a reexported module
38-
check("foo.bar_reexported.bar_attr", foo.bar_reexported.bar_attr, "bar_attr", globals()) #$ prints=bar_attr
38+
check("foo.bar_reexported.bar_attr", foo.bar_reexported.bar_attr, "bar_attr", globals()) #$ MISSING: prints=bar_attr
3939

4040
# A simple "import from" statement.
4141
from bar import bar_attr
@@ -73,6 +73,11 @@ def local_import():
7373
from namespace_package.namespace_module import namespace_module_attr
7474
check("namespace_module_attr", namespace_module_attr, "namespace_module_attr", globals()) #$ prints=namespace_module_attr
7575

76+
77+
from attr_clash import clashing_attr, non_clashing_submodule #$ imports=attr_clash.clashing_attr as=clashing_attr imports=attr_clash.non_clashing_submodule as=non_clashing_submodule
78+
check("clashing_attr", clashing_attr, "clashing_attr", globals()) #$ prints=clashing_attr
79+
check("non_clashing_submodule", non_clashing_submodule, "<module attr_clash.non_clashing_submodule>", globals())
80+
7681
exit(__file__)
7782

7883
print()

python/ql/test/experimental/import-resolution/trace.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def status():
2424
return _status
2525

2626
def check(attr_path, actual_value, expected_value, bindings):
27+
global _status
2728
parts = attr_path.split(".")
2829
base, parts = parts[0], parts[1:]
2930
if base not in bindings:
@@ -40,6 +41,8 @@ def check(attr_path, actual_value, expected_value, bindings):
4041
if val != actual_value:
4142
print("Error: Value at path {} and actual value are out of sync! {} != {}".format(attr_path, val, actual_value))
4243
_status = 1
44+
if str(val).startswith("<module"):
45+
val = "<module " + val.__name__ + ">"
4346
if val != expected_value:
4447
print("Error: Expected {} to be {}, got {}".format(attr_path, expected_value, val))
4548
_status = 1

0 commit comments

Comments
 (0)