Skip to content

Commit e7c28b6

Browse files
authored
CG-10899: file.add_import (#673)
`file.add_import_from_string` and `file.add_symbol_import` has now been consolidated into one method `file.import` which takes in `Symbol | str` either the string import or the Symbol that needs to be imported and will handle the logic ---------
1 parent 3b3d135 commit e7c28b6

File tree

40 files changed

+545
-272
lines changed

40 files changed

+545
-272
lines changed

codegen-examples/examples/dict_to_schema/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def run(codebase: Codebase):
8484

8585
# Add imports if needed
8686
if needs_imports:
87-
file.add_import_from_import_string("from pydantic import BaseModel")
87+
file.add_import("from pydantic import BaseModel")
8888

8989
if file_modified:
9090
files_modified += 1

codegen-examples/examples/flask_to_fastapi_migration/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def setup_static_files(file):
5757
print(f"📁 Processing file: {file.filepath}")
5858

5959
# Add import for StaticFiles
60-
file.add_import_from_import_string("from fastapi.staticfiles import StaticFiles")
60+
file.add_import("from fastapi.staticfiles import StaticFiles")
6161
print("✅ Added import: from fastapi.staticfiles import StaticFiles")
6262

6363
# Add app.mount for static file handling

codegen-examples/examples/sqlalchemy_soft_delete/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The codemod processes your codebase in several steps:
5858
```python
5959
def ensure_and_import(file):
6060
if not any("and_" in imp.name for imp in file.imports):
61-
file.add_import_from_import_string("from sqlalchemy import and_")
61+
file.add_import("from sqlalchemy import and_")
6262
```
6363

6464
- Automatically adds required SQLAlchemy imports (`and_`)

codegen-examples/examples/sqlalchemy_soft_delete/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def ensure_and_import(file):
5151
"""Ensure the file has the necessary and_ import."""
5252
if not any("and_" in imp.name for imp in file.imports):
5353
print(f"File {file.filepath} does not import and_. Adding import.")
54-
file.add_import_from_import_string("from sqlalchemy import and_")
54+
file.add_import("from sqlalchemy import and_")
5555

5656

5757
def clone_repo(repo_url: str, repo_path: Path) -> None:

codegen-examples/examples/sqlalchemy_type_annotations/run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,16 @@ def run(codebase: Codebase):
100100

101101
# Add necessary imports
102102
if not cls.file.has_import("Mapped"):
103-
cls.file.add_import_from_import_string("from sqlalchemy.orm import Mapped\n")
103+
cls.file.add_import("from sqlalchemy.orm import Mapped\n")
104104

105105
if "Optional" in new_type and not cls.file.has_import("Optional"):
106-
cls.file.add_import_from_import_string("from typing import Optional\n")
106+
cls.file.add_import("from typing import Optional\n")
107107

108108
if "Decimal" in new_type and not cls.file.has_import("Decimal"):
109-
cls.file.add_import_from_import_string("from decimal import Decimal\n")
109+
cls.file.add_import("from decimal import Decimal\n")
110110

111111
if "datetime" in new_type and not cls.file.has_import("datetime"):
112-
cls.file.add_import_from_import_string("from datetime import datetime\n")
112+
cls.file.add_import("from datetime import datetime\n")
113113

114114
if class_modified:
115115
classes_modified += 1

codegen-examples/examples/unittest_to_pytest/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def convert_to_pytest_fixtures(file):
2424
print(f"🔍 Processing file: {file.filepath}")
2525

2626
if not any(imp.name == "pytest" for imp in file.imports):
27-
file.add_import_from_import_string("import pytest")
27+
file.add_import("import pytest")
2828
print(f"➕ Added pytest import to {file.filepath}")
2929

3030
for cls in file.classes:

codegen-examples/examples/usesuspensequery_to_usesuspensequeries/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The script automates the entire migration process in a few key steps:
2525

2626
```python
2727
import_str = "import { useQuery, useSuspenseQueries } from '@tanstack/react-query'"
28-
file.add_import_from_import_string(import_str)
28+
file.add_import(import_str)
2929
```
3030

3131
- Uses Codegen's import analysis to add required imports

codegen-examples/examples/usesuspensequery_to_usesuspensequeries/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def run(codebase: Codebase):
2626

2727
print(f"Processing {file.filepath}")
2828
# Add the import statement
29-
file.add_import_from_import_string(import_str)
29+
file.add_import(import_str)
3030
file_modified = False
3131

3232
# Iterate through all functions in the file

docs/building-with-codegen/imports.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ for module, imports in module_imports.items():
120120
if len(imports) > 1:
121121
# Create combined import
122122
symbols = [imp.name for imp in imports]
123-
file.add_import_from_import_string(
123+
file.add_import(
124124
f"import {{ {', '.join(symbols)} }} from '{module}'"
125125
)
126126
# Remove old imports

docs/building-with-codegen/react-and-jsx.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,5 @@ for function in codebase.functions:
136136

137137
# Add import if needed
138138
if not file.has_import("NewComponent"):
139-
file.add_symbol_import(new_component)
139+
file.add_import(new_component)
140140
```

0 commit comments

Comments
 (0)