|
| 1 | +from codegen.sdk.codebase.factory.get_session import get_codebase_session |
| 2 | +from codegen.sdk.enums import ProgrammingLanguage |
| 3 | + |
| 4 | + |
| 5 | +def test_py_import_is_dynamic_in_function(tmpdir): |
| 6 | + # language=python |
| 7 | + content = """ |
| 8 | + def my_function(): |
| 9 | + import foo # Dynamic import inside function |
| 10 | + from bar import baz # Another dynamic import |
| 11 | +
|
| 12 | + import static_import # Static import at module level |
| 13 | + """ |
| 14 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 15 | + file = codebase.get_file("test.py") |
| 16 | + imports = file.imports |
| 17 | + |
| 18 | + # Dynamic imports inside function |
| 19 | + assert imports[0].is_dynamic # import foo |
| 20 | + assert imports[1].is_dynamic # from bar import baz |
| 21 | + |
| 22 | + # Static import at module level |
| 23 | + assert not imports[2].is_dynamic # import static_import |
| 24 | + |
| 25 | + |
| 26 | +def test_py_import_is_dynamic_in_if_block(tmpdir): |
| 27 | + # language=python |
| 28 | + content = """ |
| 29 | + import top_level # Static import |
| 30 | +
|
| 31 | + if condition: |
| 32 | + import conditional # Dynamic import in if block |
| 33 | + from x import y # Another dynamic import |
| 34 | + """ |
| 35 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 36 | + file = codebase.get_file("test.py") |
| 37 | + imports = file.imports |
| 38 | + |
| 39 | + assert not imports[0].is_dynamic # top_level import |
| 40 | + assert imports[1].is_dynamic # conditional import |
| 41 | + assert imports[2].is_dynamic # from x import y |
| 42 | + |
| 43 | + |
| 44 | +def test_py_import_is_dynamic_in_try_except(tmpdir): |
| 45 | + # language=python |
| 46 | + content = """ |
| 47 | + import static_first # Static import |
| 48 | +
|
| 49 | + try: |
| 50 | + import dynamic_in_try # Dynamic import in try block |
| 51 | + from x.y import z # Another dynamic import |
| 52 | + except ImportError: |
| 53 | + pass |
| 54 | + """ |
| 55 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 56 | + file = codebase.get_file("test.py") |
| 57 | + imports = file.imports |
| 58 | + |
| 59 | + assert not imports[0].is_dynamic # static_first import |
| 60 | + assert imports[1].is_dynamic # dynamic_in_try import |
| 61 | + assert imports[2].is_dynamic # from x.y import z |
| 62 | + |
| 63 | + |
| 64 | +def test_py_import_is_dynamic_in_with_block(tmpdir): |
| 65 | + # language=python |
| 66 | + content = """ |
| 67 | + import static_import # Static import |
| 68 | +
|
| 69 | + with context_manager(): |
| 70 | + import dynamic_in_with # Dynamic import in with block |
| 71 | + from a.b import c # Another dynamic import |
| 72 | + """ |
| 73 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 74 | + file = codebase.get_file("test.py") |
| 75 | + imports = file.imports |
| 76 | + |
| 77 | + assert not imports[0].is_dynamic # static_import |
| 78 | + assert imports[1].is_dynamic # dynamic_in_with import |
| 79 | + assert imports[2].is_dynamic # from a.b import c |
| 80 | + |
| 81 | + |
| 82 | +def test_py_import_is_dynamic_in_class_method(tmpdir): |
| 83 | + # language=python |
| 84 | + content = """ |
| 85 | + import static_import # Static import |
| 86 | +
|
| 87 | + class MyClass: |
| 88 | + def my_method(self): |
| 89 | + import dynamic_in_method # Dynamic import in method |
| 90 | + from pkg import module # Another dynamic import |
| 91 | +
|
| 92 | + @classmethod |
| 93 | + def class_method(cls): |
| 94 | + import another_dynamic # Dynamic import in classmethod |
| 95 | + """ |
| 96 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 97 | + file = codebase.get_file("test.py") |
| 98 | + imports = file.imports |
| 99 | + |
| 100 | + assert not imports[0].is_dynamic # static_import |
| 101 | + assert imports[1].is_dynamic # dynamic_in_method import |
| 102 | + assert imports[2].is_dynamic # from pkg import module |
| 103 | + assert imports[3].is_dynamic # another_dynamic import |
| 104 | + |
| 105 | + |
| 106 | +def test_py_import_is_dynamic_in_nested_function(tmpdir): |
| 107 | + # language=python |
| 108 | + content = """ |
| 109 | + import static_import # Static import |
| 110 | +
|
| 111 | + def outer_function(): |
| 112 | + import dynamic_in_outer # Dynamic import in outer function |
| 113 | +
|
| 114 | + def inner_function(): |
| 115 | + import dynamic_in_inner # Dynamic import in inner function |
| 116 | + from x import y # Another dynamic import |
| 117 | + """ |
| 118 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 119 | + file = codebase.get_file("test.py") |
| 120 | + imports = file.imports |
| 121 | + |
| 122 | + assert not imports[0].is_dynamic # static_import |
| 123 | + assert imports[1].is_dynamic # dynamic_in_outer import |
| 124 | + assert imports[2].is_dynamic # dynamic_in_inner import |
| 125 | + assert imports[3].is_dynamic # from x import y |
| 126 | + |
| 127 | + |
| 128 | +def test_py_import_is_dynamic_in_else_clause(tmpdir): |
| 129 | + # language=python |
| 130 | + content = """ |
| 131 | + import static_import # Static import |
| 132 | +
|
| 133 | + if condition: |
| 134 | + pass |
| 135 | + else: |
| 136 | + import dynamic_in_else # Dynamic import in else clause |
| 137 | + from x import y # Another dynamic import |
| 138 | + """ |
| 139 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 140 | + file = codebase.get_file("test.py") |
| 141 | + imports = file.imports |
| 142 | + |
| 143 | + assert not imports[0].is_dynamic # static_import |
| 144 | + assert imports[1].is_dynamic # dynamic_in_else import |
| 145 | + assert imports[2].is_dynamic # from x import y |
| 146 | + |
| 147 | + |
| 148 | +def test_py_import_is_dynamic_in_except_clause(tmpdir): |
| 149 | + # language=python |
| 150 | + content = """ |
| 151 | + import static_import # Static import |
| 152 | +
|
| 153 | + try: |
| 154 | + pass |
| 155 | + except ImportError: |
| 156 | + import dynamic_in_except # Dynamic import in except clause |
| 157 | + from x import y # Another dynamic import |
| 158 | + """ |
| 159 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 160 | + file = codebase.get_file("test.py") |
| 161 | + imports = file.imports |
| 162 | + |
| 163 | + assert not imports[0].is_dynamic # static_import |
| 164 | + assert imports[1].is_dynamic # dynamic_in_except import |
| 165 | + assert imports[2].is_dynamic # from x import y |
| 166 | + |
| 167 | + |
| 168 | +def test_py_import_is_dynamic_in_finally_clause(tmpdir): |
| 169 | + # language=python |
| 170 | + content = """ |
| 171 | + import static_import # Static import |
| 172 | +
|
| 173 | + try: |
| 174 | + pass |
| 175 | + except ImportError: |
| 176 | + pass |
| 177 | + finally: |
| 178 | + import dynamic_in_finally # Dynamic import in finally clause |
| 179 | + from x import y # Another dynamic import |
| 180 | + """ |
| 181 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 182 | + file = codebase.get_file("test.py") |
| 183 | + imports = file.imports |
| 184 | + |
| 185 | + assert not imports[0].is_dynamic # static_import |
| 186 | + assert imports[1].is_dynamic # dynamic_in_finally import |
| 187 | + assert imports[2].is_dynamic # from x import y |
| 188 | + |
| 189 | + |
| 190 | +def test_py_import_is_dynamic_in_while_statement(tmpdir): |
| 191 | + # language=python |
| 192 | + content = """ |
| 193 | + import static_import # Static import |
| 194 | +
|
| 195 | + while condition: |
| 196 | + import dynamic_in_while # Dynamic import in while loop |
| 197 | + from a import b # Another dynamic import |
| 198 | + """ |
| 199 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 200 | + file = codebase.get_file("test.py") |
| 201 | + imports = file.imports |
| 202 | + |
| 203 | + assert not imports[0].is_dynamic # static_import |
| 204 | + assert imports[1].is_dynamic # dynamic_in_while import |
| 205 | + assert imports[2].is_dynamic # from a import b |
| 206 | + |
| 207 | + |
| 208 | +def test_py_import_is_dynamic_in_match_case(tmpdir): |
| 209 | + # language=python |
| 210 | + content = """ |
| 211 | + import static_import # Static import |
| 212 | +
|
| 213 | + match value: |
| 214 | + case 1: |
| 215 | + import dynamic_in_case # Dynamic import in case clause |
| 216 | + from x import y # Another dynamic import |
| 217 | + case _: |
| 218 | + import another_dynamic # Dynamic import in default case |
| 219 | + """ |
| 220 | + with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: |
| 221 | + file = codebase.get_file("test.py") |
| 222 | + imports = file.imports |
| 223 | + |
| 224 | + assert not imports[0].is_dynamic # static_import |
| 225 | + assert imports[1].is_dynamic # dynamic_in_case import |
| 226 | + assert imports[2].is_dynamic # from x import y |
| 227 | + assert imports[3].is_dynamic # another_dynamic import |
0 commit comments