Skip to content

refactor!: make Configuration immutable - #2551

Closed
charlie-morrison wants to merge 1 commit into
apify:masterfrom
charlie-morrison:frozen-configuration
Closed

charlie-morrison wants to merge 1 commit into
apify:masterfrom
charlie-morrison:frozen-configuration

Conversation

@charlie-morrison

Copy link
Copy Markdown

Closes #2549.

Configuration now sets frozen=True. configuration.purge_on_start = False raises a pydantic ValidationError (frozen_instance), and ty check reports the same line as invalid-assignment before anything runs. The class docstring says how to get a different value: a new instance, or model_copy(update=...). I noted there that model_copy does 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 build Configuration(purge_on_start=False) and register it with service_locator.set_configuration(), the same pattern global_configuration.py uses. The sentence in 06_stopping_and_resuming.mdx follows the examples. I ran rl_basic_example_with_persist.py on this branch: it processes the requests and writes its key-value store. The master copy of the same file stops at the assignment with Instance is frozen.

Nothing in src/ or tests/ assigns to any of the 15 settings (checked with grep). The unit suite passes: 2370 passed, 6 skipped, and the run_alone group shows 6 passed, 1 skipped.

upgrading_to_v2.md is 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:

  • assignment raises, and the value stays as it was;
  • model_copy(update=...) returns a changed copy and leaves the original alone;
  • a subclass that declares its own model_config is 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_config line: frozen=False, no frozen, and validate_assignment=True in 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 in src/apify/_configuration.py assign to the instance and now raise:

  • disable_browser_sandbox_on_platform sets self.disable_browser_sandbox = True. With APIFY_IS_AT_HOME=1, apify.Configuration() raises, which means every Actor on the platform would fail at startup.
  • from_configuration copies the fields over with setattr, so it raises on the first one (internal_timeout).

The SDK's tests/unit/actor/test_configuration.py gives 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 = True and similar) and 16 in test_apify_storages.py (a fixture sets input_key, storage_dir and purge_on_start). Two tests in test_actor_child_runs.py also failed under -n 3, but they pass when the file runs alone (80/80). The SDK pins crawlee<2.0.0, so none of this breaks until it moves to 2.0.

Checks

ruff format --check, ruff check (repo and docs/), ty check, and pre-commit on the changed files all pass. Unit tests results are above.

`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>
@github-actions github-actions Bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Nov 30, 2026
@vdusek vdusek closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make Configuration immutable

2 participants