-
Notifications
You must be signed in to change notification settings - Fork 62
CG-9706: Support imp.is_dynamic #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
tests/unit/codegen/sdk/python/import_resolution/test_is_dynamic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| from codegen.sdk.codebase.factory.get_session import get_codebase_session | ||
| from codegen.sdk.enums import ProgrammingLanguage | ||
|
|
||
|
|
||
| def test_py_import_is_dynamic_in_function(tmpdir): | ||
| # language=python | ||
| content = """ | ||
| def my_function(): | ||
| import foo # Dynamic import inside function | ||
| from bar import baz # Another dynamic import | ||
|
|
||
| import static_import # Static import at module level | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: | ||
| file = codebase.get_file("test.py") | ||
| imports = file.imports | ||
|
|
||
| # Dynamic imports inside function | ||
| assert imports[0].is_dynamic # import foo | ||
| assert imports[1].is_dynamic # from bar import baz | ||
|
|
||
| # Static import at module level | ||
| assert not imports[2].is_dynamic # import static_import | ||
|
|
||
|
|
||
| def test_py_import_is_dynamic_in_if_block(tmpdir): | ||
| # language=python | ||
| content = """ | ||
| import top_level # Static import | ||
|
|
||
| if condition: | ||
| import conditional # Dynamic import in if block | ||
| from x import y # Another dynamic import | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: | ||
| file = codebase.get_file("test.py") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # top_level import | ||
| assert imports[1].is_dynamic # conditional import | ||
| assert imports[2].is_dynamic # from x import y | ||
|
|
||
|
|
||
| def test_py_import_is_dynamic_in_try_except(tmpdir): | ||
| # language=python | ||
| content = """ | ||
| import static_first # Static import | ||
|
|
||
| try: | ||
| import dynamic_in_try # Dynamic import in try block | ||
| from x.y import z # Another dynamic import | ||
| except ImportError: | ||
| pass | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: | ||
| file = codebase.get_file("test.py") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # static_first import | ||
| assert imports[1].is_dynamic # dynamic_in_try import | ||
| assert imports[2].is_dynamic # from x.y import z | ||
|
|
||
|
|
||
| def test_py_import_is_dynamic_in_with_block(tmpdir): | ||
| # language=python | ||
| content = """ | ||
| import static_import # Static import | ||
|
|
||
| with context_manager(): | ||
| import dynamic_in_with # Dynamic import in with block | ||
| from a.b import c # Another dynamic import | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: | ||
| file = codebase.get_file("test.py") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # static_import | ||
| assert imports[1].is_dynamic # dynamic_in_with import | ||
| assert imports[2].is_dynamic # from a.b import c | ||
|
|
||
|
|
||
| def test_py_import_is_dynamic_in_class_method(tmpdir): | ||
| # language=python | ||
| content = """ | ||
| import static_import # Static import | ||
|
|
||
| class MyClass: | ||
| def my_method(self): | ||
| import dynamic_in_method # Dynamic import in method | ||
| from pkg import module # Another dynamic import | ||
|
|
||
| @classmethod | ||
| def class_method(cls): | ||
| import another_dynamic # Dynamic import in classmethod | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: | ||
| file = codebase.get_file("test.py") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # static_import | ||
| assert imports[1].is_dynamic # dynamic_in_method import | ||
| assert imports[2].is_dynamic # from pkg import module | ||
| assert imports[3].is_dynamic # another_dynamic import | ||
|
|
||
|
|
||
| def test_py_import_is_dynamic_in_nested_function(tmpdir): | ||
| # language=python | ||
| content = """ | ||
| import static_import # Static import | ||
|
|
||
| def outer_function(): | ||
| import dynamic_in_outer # Dynamic import in outer function | ||
|
|
||
| def inner_function(): | ||
| import dynamic_in_inner # Dynamic import in inner function | ||
| from x import y # Another dynamic import | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}, programming_language=ProgrammingLanguage.PYTHON) as codebase: | ||
| file = codebase.get_file("test.py") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # static_import | ||
| assert imports[1].is_dynamic # dynamic_in_outer import | ||
| assert imports[2].is_dynamic # dynamic_in_inner import | ||
| assert imports[3].is_dynamic # from x import y |
112 changes: 112 additions & 0 deletions
112
tests/unit/codegen/sdk/typescript/import_resolution/test_is_dynamic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| from codegen.sdk.codebase.factory.get_session import get_codebase_session | ||
| from codegen.sdk.enums import ProgrammingLanguage | ||
|
|
||
|
|
||
| def test_ts_import_is_dynamic_in_function_declaration(tmpdir): | ||
| # language=typescript | ||
| content = """ | ||
| import { staticImport } from './static'; | ||
| function loadModule() { | ||
| import('./dynamic').then(module => { | ||
| console.log(module); | ||
| }); | ||
| } | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.ts": content}, programming_language=ProgrammingLanguage.TYPESCRIPT) as codebase: | ||
| file = codebase.get_file("test.ts") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # static import | ||
| assert imports[1].is_dynamic # dynamic import in function | ||
|
|
||
|
|
||
| def test_ts_import_is_dynamic_in_method_definition(tmpdir): | ||
| # language=typescript | ||
| content = """ | ||
| import { Component } from '@angular/core'; | ||
| class MyComponent { | ||
| async loadFeature() { | ||
| const feature = await import('./feature'); | ||
| } | ||
| @Decorator() | ||
| async decoratedMethod() { | ||
| const module = await import('./decorated'); | ||
| } | ||
| } | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.ts": content}, programming_language=ProgrammingLanguage.TYPESCRIPT) as codebase: | ||
| file = codebase.get_file("test.ts") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # static import | ||
| assert imports[1].is_dynamic # dynamic import in method | ||
| assert imports[2].is_dynamic # dynamic import in decorated method | ||
|
|
||
|
|
||
| def test_ts_import_is_dynamic_in_arrow_function(tmpdir): | ||
| # language=typescript | ||
| content = """ | ||
| import { useState } from 'react'; | ||
| const MyComponent = () => { | ||
| const loadModule = async () => { | ||
| const module = await import('./lazy'); | ||
| }; | ||
| return <button onClick={loadModule} />; | ||
| }; | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.ts": content}, programming_language=ProgrammingLanguage.TYPESCRIPT) as codebase: | ||
| file = codebase.get_file("test.ts") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # static import | ||
| assert imports[1].is_dynamic # dynamic import in async arrow function | ||
|
|
||
|
|
||
| def test_ts_import_is_dynamic_in_if_statement(tmpdir): | ||
| # language=typescript | ||
| content = """ | ||
| import { isFeatureEnabled } from './utils'; | ||
| if (isFeatureEnabled('feature')) { | ||
| import('./feature').then(module => { | ||
| module.enableFeature(); | ||
| }); | ||
| } else { | ||
| import('./fallback').then(module => { | ||
| module.useFallback(); | ||
| }); | ||
| } | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.ts": content}, programming_language=ProgrammingLanguage.TYPESCRIPT) as codebase: | ||
| file = codebase.get_file("test.ts") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # static import | ||
| assert imports[1].is_dynamic # dynamic import in if block | ||
| assert imports[2].is_dynamic # dynamic import in else block | ||
|
|
||
|
|
||
| def test_ts_import_is_dynamic_in_try_statement(tmpdir): | ||
| # language=typescript | ||
| content = """ | ||
| import { logger } from './logger'; | ||
| try { | ||
| const module = await import('./main'); | ||
| } catch (error) { | ||
| const fallback = "hello" | ||
| } finally { | ||
| const cleanup = "hello" | ||
| } | ||
| """ | ||
| with get_codebase_session(tmpdir=tmpdir, files={"test.ts": content}, programming_language=ProgrammingLanguage.TYPESCRIPT) as codebase: | ||
| file = codebase.get_file("test.ts") | ||
| imports = file.imports | ||
|
|
||
| assert not imports[0].is_dynamic # static import | ||
tawsifkamal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert imports[1].is_dynamic # dynamic import in try block | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.