Repository navigation
refactor!: make Configuration immutable - #2551
Closed
charlie-morrison wants to merge 1 commit into
Closed
charlie-morrison wants to merge 1 commit into
charlie-morrison wants to merge 1 commit into
Conversation
`Configuration` sets `frozen=True`, so assigning to a setting raises a `ValidationError` and type checkers report the assignment. Components read a setting when they start, so an assignment made after that was not seen by them. The docs examples that turned off `purge_on_start` on the global configuration now register `Configuration(purge_on_start=False)` with the service locator. Closes apify#2549 BREAKING CHANGE: `Configuration` instances are frozen. Create a new instance with the value, or use `model_copy(update=...)`, which does not validate the new values. Co-authored-by: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2549.
Configurationnow setsfrozen=True.configuration.purge_on_start = Falseraises a pydanticValidationError(frozen_instance), andty checkreports the same line asinvalid-assignmentbefore anything runs. The class docstring says how to get a different value: a new instance, ormodel_copy(update=...). I noted there thatmodel_copydoes not validate the new values, since a wrong type passes through it silently.Docs
These are the 4 files from the issue. The three examples (
rl_basic_example_with_persist.py,sitemap_example_with_persist.py,resuming_paused_crawl.py) now buildConfiguration(purge_on_start=False)and register it withservice_locator.set_configuration(), the same patternglobal_configuration.pyuses. The sentence in06_stopping_and_resuming.mdxfollows the examples. I ranrl_basic_example_with_persist.pyon this branch: it processes the requests and writes its key-value store. The master copy of the same file stops at the assignment withInstance is frozen.Nothing in
src/ortests/assigns to any of the 15 settings (checked with grep). The unit suite passes: 2370 passed, 6 skipped, and therun_alonegroup shows 6 passed, 1 skipped.upgrading_to_v2.mdis being created in #2550, so I didn't touch it here. Once that lands I can add the entry in this PR, or it can go in as one line there.Tests
There are 3 new tests in
tests/unit/test_configuration.py:model_copy(update=...)returns a changed copy and leaves the original alone;model_configis still frozen. The SDK's subclass does this (alias_generator), and pydantic merges the parent's config into it.The first and third fail on master. The second passes on master as well; it pins the documented way to change a value. I also ran three mutants of the
model_configline:frozen=False, nofrozen, andvalidate_assignment=Truein its place. All three turn both of those tests red.apify-sdk-python (checked here, not changed)
I installed the SDK (master
b7911f3) on top of this branch. Two places insrc/apify/_configuration.pyassign to the instance and now raise:disable_browser_sandbox_on_platformsetsself.disable_browser_sandbox = True. WithAPIFY_IS_AT_HOME=1,apify.Configuration()raises, which means every Actor on the platform would fail at startup.from_configurationcopies the fields over withsetattr, so it raises on the first one (internal_timeout).The SDK's
tests/unit/actor/test_configuration.pygives 6 failed and 30 passed on this branch. The change below fixes both spots, and with it that file passes 36/36 on this branch and on master.model_copy(update=...)keeps the no-revalidation behaviour that the pydantic#9516 comment above the loop asks for.SDK diff I tested
@model_validator(mode='after') def disable_browser_sandbox_on_platform(self) -> Self: ... if self.is_at_home and not self.disable_browser_sandbox: - self.disable_browser_sandbox = True + # Crawlee's `Configuration` is frozen, so the attribute is set past the frozen check. + object.__setattr__(self, 'disable_browser_sandbox', True) logger.warning('Actor is running on the Apify platform, `disable_browser_sandbox` was changed to True.') return self @@ - for name in type(configuration).model_fields: - setattr(apify_configuration, name, getattr(configuration, name)) - - return apify_configuration + values = {name: getattr(configuration, name) for name in type(configuration).model_fields} + return apify_configuration.model_copy(update=values)With that change in place, the rest of the SDK unit suite has 32 more failures than on master, and all of them are tests assigning to a configuration: 16 in
test_actor_helpers.py(Actor.configuration.is_at_home = Trueand similar) and 16 intest_apify_storages.py(a fixture setsinput_key,storage_dirandpurge_on_start). Two tests intest_actor_child_runs.pyalso failed under-n 3, but they pass when the file runs alone (80/80). The SDK pinscrawlee<2.0.0, so none of this breaks until it moves to 2.0.Checks
ruff format --check,ruff check(repo anddocs/),ty check, and pre-commit on the changed files all pass. Unit tests results are above.