| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import unittest |
| 4 |
from typing import cast |
| 5 |
|
| 6 |
from mypy.build import Graph |
| 7 |
from mypy.nodes import Import, MypyFile |
| 8 |
from mypy.options import Options |
| 9 |
from mypyc.errors import Errors |
| 10 |
from mypyc.irbuild.builder import IRBuilder |
| 11 |
from mypyc.irbuild.mapper import Mapper |
| 12 |
from mypyc.irbuild.prebuildvisitor import PreBuildVisitor |
| 13 |
from mypyc.irbuild.statement import ( |
| 14 |
IMPORT_NATIVE_ATTR, |
| 15 |
IMPORT_NATIVE_SUBMODULE, |
| 16 |
IMPORT_NON_NATIVE, |
| 17 |
classify_import_from, |
| 18 |
group_consecutive, |
| 19 |
import_globals_id_and_name, |
| 20 |
split_import_group_to_python_and_native, |
| 21 |
) |
| 22 |
from mypyc.irbuild.visitor import IRBuilderVisitor |
| 23 |
from mypyc.options import CompilerOptions |
| 24 |
|
| 25 |
|
| 26 |
def make_builder( |
| 27 |
*, |
| 28 |
module_name: str = "pkg.current", |
| 29 |
native_modules: set[str] | None = None, |
| 30 |
same_group_modules: set[str] | None = None, |
| 31 |
graph: set[str] | None = None, |
| 32 |
) -> IRBuilder: |
| 33 |
native_modules = native_modules or set() |
| 34 |
same_group_modules = same_group_modules or set() |
| 35 |
group_map: dict[str, str | None] = {module_name: "current-group"} |
| 36 |
for module in native_modules: |
| 37 |
group_map[module] = "current-group" if module in same_group_modules else "other-group" |
| 38 |
|
| 39 |
errors = Errors(Options()) |
| 40 |
current_file = MypyFile([], []) |
| 41 |
current_file._fullname = module_name |
| 42 |
pbv = PreBuildVisitor(errors, current_file, {}, {}) |
| 43 |
builder = IRBuilder( |
| 44 |
module_name, |
| 45 |
{}, |
| 46 |
cast(Graph, {name: object() for name in (graph or set())}), |
| 47 |
errors, |
| 48 |
Mapper(group_map), |
| 49 |
pbv, |
| 50 |
IRBuilderVisitor(), |
| 51 |
CompilerOptions(), |
| 52 |
{}, |
| 53 |
) |
| 54 |
builder.set_module(module_name, module_name.replace(".", "/") + ".py") |
| 55 |
return builder |
| 56 |
|
| 57 |
|
| 58 |
class TestStatementHelpers(unittest.TestCase): |
| 59 |
def test_import_globals_id_and_name_for_plain_import(self) -> None: |
| 60 |
assert import_globals_id_and_name("foo.bar", None) == ("foo", "foo") |
| 61 |
|
| 62 |
def test_import_globals_id_and_name_for_import_as(self) -> None: |
| 63 |
assert import_globals_id_and_name("foo.bar", "baz") == ("foo.bar", "baz") |
| 64 |
|
| 65 |
def test_split_import_group_to_python_and_native_preserves_runs(self) -> None: |
| 66 |
builder = make_builder( |
| 67 |
native_modules={"pkg.alpha", "pkg.beta", "pkg.gamma"}, |
| 68 |
same_group_modules={"pkg.alpha", "pkg.beta", "pkg.gamma"}, |
| 69 |
) |
| 70 |
group = [ |
| 71 |
Import([("pkg.alpha", None), ("py_mod", None)]), |
| 72 |
Import([("pkg.beta", "beta_alias"), ("foreign.mod", None), ("pkg.gamma", None)]), |
| 73 |
] |
| 74 |
group[0].line = 10 |
| 75 |
group[1].line = 20 |
| 76 |
|
| 77 |
result = split_import_group_to_python_and_native(builder, group) |
| 78 |
|
| 79 |
assert result == [ |
| 80 |
([("pkg.alpha", None, 10)], True), |
| 81 |
([("py_mod", None, 10)], False), |
| 82 |
([("pkg.beta", "beta_alias", 20)], True), |
| 83 |
([("foreign.mod", None, 20)], False), |
| 84 |
([("pkg.gamma", None, 20)], True), |
| 85 |
] |
| 86 |
|
| 87 |
def test_group_consecutive_groups_by_kind_and_preserves_aliases(self) -> None: |
| 88 |
buckets = group_consecutive( |
| 89 |
[ |
| 90 |
(IMPORT_NATIVE_SUBMODULE, "a", "a"), |
| 91 |
(IMPORT_NATIVE_SUBMODULE, "b", "b_alias"), |
| 92 |
(IMPORT_NON_NATIVE, "c", "c"), |
| 93 |
(IMPORT_NATIVE_ATTR, "d", "d_alias"), |
| 94 |
(IMPORT_NATIVE_ATTR, "e", "e"), |
| 95 |
] |
| 96 |
) |
| 97 |
|
| 98 |
assert [(bucket.kind, bucket.names, bucket.as_names) for bucket in buckets] == [ |
| 99 |
(IMPORT_NATIVE_SUBMODULE, ["a", "b"], ["a", "b_alias"]), |
| 100 |
(IMPORT_NON_NATIVE, ["c"], ["c"]), |
| 101 |
(IMPORT_NATIVE_ATTR, ["d", "e"], ["d_alias", "e"]), |
| 102 |
] |
| 103 |
|
| 104 |
def test_classify_import_from_groups_consecutive_kinds(self) -> None: |
| 105 |
builder = make_builder( |
| 106 |
native_modules={"pkg.native_a", "pkg.native_b"}, |
| 107 |
same_group_modules={"pkg.native_a", "pkg.native_b"}, |
| 108 |
graph={"pkg.native_a", "pkg.native_b", "pkg.foreign_a", "pkg.foreign_b"}, |
| 109 |
) |
| 110 |
|
| 111 |
buckets = classify_import_from( |
| 112 |
builder, |
| 113 |
"pkg", |
| 114 |
["native_a", "native_b", "foreign_a", "foreign_b"], |
| 115 |
["native_a", "native_b_alias", "foreign_a", "foreign_b_alias"], |
| 116 |
parent_is_native=True, |
| 117 |
) |
| 118 |
|
| 119 |
assert [(bucket.kind, bucket.names, bucket.as_names) for bucket in buckets] == [ |
| 120 |
(IMPORT_NATIVE_SUBMODULE, ["native_a", "native_b"], ["native_a", "native_b_alias"]), |
| 121 |
(IMPORT_NON_NATIVE, ["foreign_a", "foreign_b"], ["foreign_a", "foreign_b_alias"]), |
| 122 |
] |
| 123 |
|
| 124 |
def test_classify_import_from_treats_missing_name_under_native_parent_as_attr(self) -> None: |
| 125 |
builder = make_builder(graph={"pkg.foreign"}) |
| 126 |
|
| 127 |
buckets = classify_import_from( |
| 128 |
builder, |
| 129 |
"pkg", |
| 130 |
["attr_name", "foreign"], |
| 131 |
["attr_alias", "foreign_alias"], |
| 132 |
parent_is_native=True, |
| 133 |
) |
| 134 |
|
| 135 |
assert [(bucket.kind, bucket.names, bucket.as_names) for bucket in buckets] == [ |
| 136 |
(IMPORT_NATIVE_ATTR, ["attr_name"], ["attr_alias"]), |
| 137 |
(IMPORT_NON_NATIVE, ["foreign"], ["foreign_alias"]), |
| 138 |
] |
| 139 |
|
| 140 |
def test_classify_import_from_without_native_parent_never_uses_native_attr(self) -> None: |
| 141 |
builder = make_builder() |
| 142 |
|
| 143 |
buckets = classify_import_from( |
| 144 |
builder, "pkg", ["attr_name"], ["attr_alias"], parent_is_native=False |
| 145 |
) |
| 146 |
|
| 147 |
assert [(bucket.kind, bucket.names, bucket.as_names) for bucket in buckets] == [ |
| 148 |
(IMPORT_NON_NATIVE, ["attr_name"], ["attr_alias"]) |
| 149 |
] |