Skip to content

Commit 652948b

Browse files
author
codegen-bot
committed
added links
1 parent 4249adb commit 652948b

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

docs/tutorials/fixing-import-loops-in-pytorch.mdx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ def some_func():
101101

102102
A dynamic import is an import defined inside of a function, method or any executable body of code which delays the import execution until that function, method or body of code is called.
103103

104-
You can use `imp.is_dynamic` to check if the import is dynamic allowing you to investigate imports that are handled more intentionally.
104+
You can use [Import.is_dynamic](/api-reference/core/Import#is-dynamic) to check if the import is dynamic allowing you to investigate imports that are handled more intentionally.
105105

106106
# Step 2: Visualize Import Loops
107107
- Create a new subgraph to visualize one cycle
108108
- color and label the edges based on their type (dynamic/static)
109-
- visualize the cycle graph using `codebase.visualize(graph)`
109+
- visualize the cycle graph using [codebase.visualize(graph)](/api-reference/core/Codebase#visualize)
110+
111+
<Tip>Learn more about codebase visualization [here](/building-with-codegen/codebase-visualization)</Tip>
110112

111113
```python
112114
cycle = cycles[0]
@@ -217,7 +219,9 @@ problematic_cycles = find_problematic_import_loops(G, cycles)
217219
```
218220

219221
# Step 4: Fix the loop by moving the shared symbols to a separate `utils.py` file
220-
One common fix to this problem to break this cycle is to move all the shared symbols to a separate `utils.py` file. We can do this using the method `symbol.move_to_file`:
222+
One common fix to this problem to break this cycle is to move all the shared symbols to a separate `utils.py` file. We can do this using the method [symbol.move_to_file](/api-reference/core/Symbol#move-to-file):
223+
224+
<Tip>Learn more about moving symbols [here](/building-with-codegen/moving-symbols)</Tip>
221225

222226
```python
223227
# Create new utils file
@@ -246,15 +250,27 @@ for symbol in symbols_to_move:
246250
print(f"🔄 Moved {len(symbols_to_move)} symbols to flex_utils.py")
247251
for symbol in symbols_to_move:
248252
print(symbol.name)
249-
```
250253
251-
```python
252-
# run this command to have the changes take effect in the codebase
254+
# Commit changes
253255
codebase.commit()
254256
```
255257

256-
Next Steps
257-
Verify all tests pass after the migration and fix other problematic import loops using the suggested strategies:
258-
1. Move the shared symbols to a separate file
259-
2. If a module needs imports only for type hints, consider using `if TYPE_CHECKING` from the `typing` module
260-
3. Use lazy imports using `importlib` to load imports dynamically
258+
# Conclusions & Next Steps
259+
260+
Import loops can be tricky to identify and fix, but Codegen provides powerful tools to help manage them:
261+
262+
- Use `codebase.imports` to analyze import relationships across your project
263+
- Visualize import cycles to better understand dependencies
264+
- Distinguish between static and dynamic imports using `Import.is_dynamic`
265+
- Move shared symbols to break cycles using `symbol.move_to_file`
266+
267+
Here are some next steps you can take:
268+
269+
1. **Analyze Your Codebase**: Run similar analysis on your own codebase to identify potential import cycles
270+
2. **Create Import Guidelines**: Establish best practices for your team around when to use static vs dynamic imports
271+
3. **Automate Fixes**: Create scripts to automatically detect and fix problematic import patterns
272+
4. **Monitor Changes**: Set up CI checks to prevent new problematic import cycles from being introduced
273+
274+
<Info>
275+
For more examples of codebase analysis and refactoring, check out our other [tutorials](/tutorials/at-a-glance).
276+
</Info>

0 commit comments

Comments
 (0)