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

chore: utilize ensure_context decorator for Actor #400

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

vdusek
Copy link
Contributor

@vdusek vdusek commented Feb 13, 2025

Utilize ensure_context decorator from Crawlee rather than calling raise_if_... method in the Actor class.

@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. debt Code quality improvement or decrease of technical debt. t-tooling Issues with this label are in the ownership of the tooling team. labels Feb 13, 2025
@vdusek vdusek added this to the 108th sprint - Tooling team milestone Feb 13, 2025
@vdusek vdusek self-assigned this Feb 13, 2025
@vdusek vdusek marked this pull request as ready for review February 13, 2025 09:16
@vdusek vdusek marked this pull request as draft February 13, 2025 09:17
@github-actions github-actions bot added the tested Temporary label used only programatically for some analytics. label Feb 13, 2025
@vdusek vdusek marked this pull request as ready for review February 13, 2025 09:25
Copy link
Contributor

@janbuchar janbuchar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly not sure about this. It reduces some duplication, yes, but the error message will be pretty cryptic. It is fine for an internal helper in Crawlee, but here, it's very much user-facing. Maybe we could make a wrapper for the decorator?

@vdusek
Copy link
Contributor Author

vdusek commented Feb 13, 2025

Error message:

RuntimeError: The _ActorType is not active. Use it within the async context.

Whole traceback:

>>> asyncio.run(Actor.reboot())
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    asyncio.run(Actor.reboot())
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.13/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "/usr/lib64/python3.13/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/usr/lib64/python3.13/asyncio/base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/home/vdusek/Projects/apify-sdk-python/.venv/lib/python3.13/site-packages/crawlee/_utils/context.py", line 36, in async_wrapper
    raise RuntimeError(f'The {self.__class__.__name__} is not active. Use it within the async context.')
RuntimeError: The _ActorType is not active. Use it within the async context.

Is it that bad? We can wrap it of course.

@janbuchar
Copy link
Contributor

janbuchar commented Feb 13, 2025

Is it that bad? We can wrap it of course.

Well, leaking the existence of _ActorType is confusing by itself. Plus, the message should tell the user "Just use Actor.init or async with Actor" - many of them won't even know what a context manager is.

Copy link
Contributor

@Pijukatel Pijukatel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this decorator, maybe we have to have custom error message in this case, but I think it makes the code more readable.

@vdusek
Copy link
Contributor Author

vdusek commented Feb 17, 2025

Yep, I will try to update the decorator to have a better error message there.

@janbuchar janbuchar self-requested a review March 24, 2025 14:09
Copy link
Contributor

@janbuchar janbuchar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap that error message 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adhoc Ad-hoc unplanned task added during the sprint. debt Code quality improvement or decrease of technical debt. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants