Skip to content
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

fix: Ensure importability of modules not included in __init__.py files #5965

Merged
merged 2 commits into from
Jan 28, 2025

Conversation

ogabrielluiz
Copy link
Contributor

Enhance the import mechanism to allow modules that are not explicitly included in init.py files to be importable by attempting to access them as attributes first, and falling back to importing the full module path if necessary.

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jan 27, 2025
@github-actions github-actions bot added the bug Something isn't working label Jan 27, 2025
codeflash-ai bot added a commit that referenced this pull request Jan 27, 2025
…from-imports`)

To optimize the program for both execution speed and memory usage, we can make some modifications. Specifically, we'll eliminate repetitive calls to `compile` and `exec` for `ClassDef`, `FunctionDef`, and `Assign` nodes. Instead, we'll compile all such nodes together and then execute them in one go. We will also handle imports more efficiently to avoid re-importing modules multiple times. Here's the rewritten code.



### Explanation.

1. **Avoid Re-importing**: The code now checks if the module or alias is already in `exec_globals` to avoid re-importing it.
2. **Batch Compilation and Execution**: All `ClassDef`, `FunctionDef`, and `Assign` nodes are collected in `exec_bodies`. They are then compiled and executed together in one call to `exec`, reducing the overhead.
3. **Using Warnings Context Manager**: Warnings for deprecations are handled using a context manager, ensuring that the warning filter is applied only for the scope needing it.

These changes should help improve the overall runtime performance and memory efficiency of the function.
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jan 27, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Jan 27, 2025
codeflash-ai bot added a commit that referenced this pull request Jan 27, 2025
…from-imports`)

To optimize the program, we can make a few improvements like reducing the number of compile and exec calls by batching statements together, which will help in reducing the overhead introduced by multiple invocations. Additionally, using defaultdict for storing the import statements may simplify the logic and enhance readability.

Let's rewrite the code with these optimizations.



Changes and optimizations.
1. **Batched Imports:** Grouping import and import-from statements separately to reduce redundancy.
2. **Batch Compilation:** Aggregated the handling of class, function, and assignment definitions into fewer compile and exec calls to limit overhead.
3. **Code Simplification:** Use defaultdict to store import and import-from statements for better readability and manageability.

These changes aim to make the function more efficient by reducing repetitive operations and improving clarity.
Copy link

codspeed-hq bot commented Jan 27, 2025

CodSpeed Performance Report

Merging #5965 will degrade performances by 96.06%

Comparing fix-from-imports (30f5613) with main (ea2685f)

Summary

❌ 1 regressions
✅ 8 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
test_successful_run_with_input_type_text 163.2 ms 4,146.5 ms -96.06%

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 27, 2025
@ogabrielluiz ogabrielluiz added this pull request to the merge queue Jan 28, 2025
Merged via the queue into main with commit 0ef54c5 Jan 28, 2025
32 of 41 checks passed
@ogabrielluiz ogabrielluiz deleted the fix-from-imports branch January 28, 2025 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants