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

v0.0.3 Return correct Type and Repr after first use #21

Merged
merged 10 commits into from
Jan 17, 2025
Merged

Conversation

glenn-jocher
Copy link
Member

@glenn-jocher glenn-jocher commented Jan 17, 2025

πŸ› οΈ PR Summary

Made with ❀️ by Ultralytics Actions

🌟 Summary

Update to the autoimport package improving LazyLoader functionality, with test enhancements and version bump. πŸš€

πŸ“Š Key Changes

  • Updated LazyLoader to copy critical module attributes (__file__, __path__, etc.) for better compatibility.
  • Enhanced LazyLoader.__repr__() for improved clarity when modules aren't loaded.
  • Updated tests to ensure LazyLoader correctly integrates with standard imports.
  • Expanded test documentation, offering both pytest (recommended) and unittest testing options.
  • Incremented package version from 0.0.2 to 0.0.3.

🎯 Purpose & Impact

  • πŸ› οΈ Improved Module Compatibility: Ensures LazyLoader works seamlessly with external tools and libraries that rely on module attributes.
  • πŸ“„ More Informative Debugging: Clearer representations of LazyLoader instances simplify debugging.
  • βœ… Testing Flexibility: Users now have multiple testing options, making it easier to adopt best practices or work with built-in tools.
  • πŸ”„ Version Bump: Indicates backward-compatible changes and improved stability.

This update enhances reliability and usability for developers leveraging the autoimport package. 🌟

@UltralyticsAssistant UltralyticsAssistant added the enhancement New feature or request label Jan 17, 2025
@UltralyticsAssistant
Copy link
Member

πŸ‘‹ Hello @glenn-jocher, thank you for submitting a ultralytics/autoimport πŸš€ PR! To ensure a seamless integration of your changes, please review the following checklist:

  • βœ… Define a Purpose: Clearly explain the purpose of your fix or feature in your PR description, and link to any relevant issues. Your summary looks great, but feel free to refine it further if needed.
  • βœ… Synchronize with Source: Ensure your PR is synchronized with the ultralytics/autoimport main branch. If it's behind, update it by clicking the 'Update branch' button or running git pull and git merge main locally before pushing again.
  • βœ… Ensure CI Checks Pass: Confirm that all Ultralytics Continuous Integration (CI) tests are passing. If any checks fail, please review and address the issues to meet our contribution requirements.
  • βœ… Update Documentation: If your PR introduces or modifies functionality, ensure the corresponding documentation is updated accordingly.
  • βœ… Add Tests: For your changes, consider adding or updating tests where appropriate, and confirm that all tests successfully pass.
  • βœ… Sign the CLA: If this is your first PR for Ultralytics, please review and sign our Contributor License Agreement by leaving a comment stating, "I have read the CLA Document and I sign the CLA."
  • βœ… Minimize Changes: Limit your changes to those essential for your update. Lean and clean code is key! "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." β€” Antoine de Saint-ExupΓ©ry

This is an automated response to help streamline the review process. An Ultralytics engineer will review your submission and provide additional feedback soon.

For additional guidance, please refer to our Contributing Guide. Let us know if you have any questions or need assistance. Thank you for contributing to Ultralytics! πŸš€πŸŽ‰

@glenn-jocher
Copy link
Member Author

glenn-jocher commented Jan 17, 2025

Test script

import time
from autoimport import lazy

with lazy():
    t0 = time.perf_counter()
    import torch  # Import is deferred until first use
    print(f"\nInitial import time: {time.perf_counter() - t0:.3f}s")  # Example output: 0.000s
    print(f"Initial type: {type(torch)}")
    print(f"Initial print: {str(torch)}")

t1 = time.perf_counter()
torch.cuda.is_available()  # Package is actually loaded here
print(f"\nFirst use time: {time.perf_counter() - t1:.3f}s")  # Example output: 0.462s
print(f"First use type: {type(torch)}")
print(f"First use print: {str(torch)}")

Currently returns this, module type is incorrect which may impact code bases that check using i.e. isinstance(x, module)

Initial import time: 0.000s
Initial type: <class 'autoimport.main.LazyLoader'>
Initial print: <LazyLoader for 'torch'>

First use time: 0.496s
First use type: <class 'autoimport.main.LazyLoader'>
First use print: <module 'torch' from '/Users/glennjocher/PycharmProjects/.venv/lib/python3.12/site-packages/torch/__init__.py'>

@glenn-jocher
Copy link
Member Author

Ok I think this is working now. After latest commit:

Initial import time: 0.000s
Initial type: <class 'autoimport.main.LazyLoader'>
Initial print: <LazyLoader for 'torch'>

First use time: 0.494s
First use type: <class 'module'>
First use print: <module 'torch' from '/Users/glennjocher/PycharmProjects/.venv/lib/python3.12/site-packages/torch/__init__.py'>

@glenn-jocher glenn-jocher changed the title Repr updates Return correct Type and Repr after first use Jan 17, 2025
Signed-off-by: Glenn Jocher <[email protected]>
Signed-off-by: Glenn Jocher <[email protected]>
@glenn-jocher glenn-jocher changed the title Return correct Type and Repr after first use v0.0.3 Return correct Type and Repr after first use Jan 17, 2025
@glenn-jocher glenn-jocher merged commit 2b0da2f into main Jan 17, 2025
7 checks passed
@glenn-jocher glenn-jocher deleted the repr branch January 17, 2025 13:28
@UltralyticsAssistant
Copy link
Member

πŸŽ‰ PR Merged! Huge thanks, @glenn-jocher, for this fantastic update to autoimport! πŸš€ Your work improving LazyLoader functionality and expanding test clarity positively impacts both usability and developer experience. The meticulous care you invested in compatibility and debugging is a game-changer for the community. πŸ™Œ

As Leonardo da Vinci once said, "Details make perfection, and perfection is not a detail." Your contributions embody this truthβ€”thank you for your dedication. Here's to making tools smarter and the developer's journey smoother! 🌟

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants