Skip to content

refactor: Use ensure_context decorator in Actor class#822

Merged
vdusek merged 2 commits intomasterfrom
add-ensure-context
Mar 3, 2026
Merged

refactor: Use ensure_context decorator in Actor class#822
vdusek merged 2 commits intomasterfrom
add-ensure-context

Conversation

@vdusek
Copy link
Contributor

@vdusek vdusek commented Mar 2, 2026

Summary

  • Replace manual _raise_if_not_initialized() calls with the @ensure_context decorator from Crawlee, matching the pattern already used in ChargingManager
  • Add active attribute to _ActorType (replaces _is_initialized) for compatibility with ensure_context
  • Remove the now-unused _raise_if_not_initialized method

Reopens #400

Test plan

  • CI passes

🤖 Generated with Claude Code

Replace manual `_raise_if_not_initialized()` calls with the `ensure_context`
decorator from Crawlee, matching the pattern already used in ChargingManager.

- Add `active` attribute to `_ActorType` (replaces `_is_initialized`)
- Decorate all public methods with `@ensure_context` instead of calling
  `_raise_if_not_initialized()` at the start of each method
- Remove the now-unused `_raise_if_not_initialized` method
- Update all test references from `_is_initialized` to `active`

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vdusek vdusek added this to the 135th sprint - Tooling team milestone Mar 2, 2026
@github-actions github-actions bot added t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics. labels Mar 2, 2026
@codecov
Copy link

codecov bot commented Mar 2, 2026

Codecov Report

❌ Patch coverage is 98.27586% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.87%. Comparing base (69b8af9) to head (5d633d0).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
src/apify/_utils.py 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #822      +/-   ##
==========================================
- Coverage   85.92%   85.87%   -0.06%     
==========================================
  Files          46       46              
  Lines        2778     2796      +18     
==========================================
+ Hits         2387     2401      +14     
- Misses        391      395       +4     
Flag Coverage Δ
e2e 38.05% <81.03%> (+0.97%) ⬆️
integration 59.40% <91.37%> (+0.76%) ⬆️
unit 73.46% <98.27%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vdusek vdusek added the adhoc Ad-hoc unplanned task added during the sprint. label Mar 2, 2026
@vdusek vdusek marked this pull request as ready for review March 2, 2026 13:52
@vdusek vdusek requested a review from Pijukatel March 2, 2026 13:52
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.

The tiny small comment is that we now have one more public .active , which is not really needed. In a gigantic class like Actor, that further increases the clutter.

What about making the @ensure_context decorator with a parameter?

Then we can do something like this

_ensure_context = ensure_context("_active")

class Actor:
    ...
    @_ensure_context
    async def open_dataset

@vdusek
Copy link
Contributor Author

vdusek commented Mar 3, 2026

The tiny small comment is that we now have one more public .active , which is not really needed. In a gigantic class like Actor, that further increases the clutter.

What about making the @ensure_context decorator with a parameter?

Sure, good idea. I've implemented a new version of ensure_context that accepts an argument for the SDK.

I implemented it in the SDK rather than changing the one in the Crawlee; this way, we don't have to restrict the lower bound version of Crawlee.

We can unify it again in the next major version of Crawlee & SDK (to have only 1 version of ensure context).

@vdusek vdusek merged commit ed30461 into master Mar 3, 2026
31 checks passed
@vdusek vdusek deleted the add-ensure-context branch March 3, 2026 09:37
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. 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.

2 participants