Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

from crawlee import service_locator
from crawlee.configuration import Configuration
from crawlee.request_loaders import RequestList

logging.basicConfig(level=logging.INFO, format='%(asctime)s-%(levelname)s-%(message)s')
Expand All @@ -12,8 +13,8 @@
# This is necessary so that the state keys are not cleared at startup.
# The recommended way to achieve this behavior is setting the environment variable
# `CRAWLEE_PURGE_ON_START=0`
configuration = service_locator.get_configuration()
configuration.purge_on_start = False
configuration = Configuration(purge_on_start=False)
service_locator.set_configuration(configuration)


async def main() -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

from crawlee import service_locator
from crawlee.configuration import Configuration
from crawlee.http_clients import ImpitHttpClient
from crawlee.request_loaders import SitemapRequestLoader

Expand All @@ -13,8 +14,8 @@
# This is necessary so that the state keys are not cleared at startup.
# The recommended way to achieve this behavior is setting the environment variable
# `CRAWLEE_PURGE_ON_START=0`
configuration = service_locator.get_configuration()
configuration.purge_on_start = False
configuration = Configuration(purge_on_start=False)
service_locator.set_configuration(configuration)


async def main() -> None:
Expand Down
2 changes: 1 addition & 1 deletion docs/04_guides/06_stopping_and_resuming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Now resume crawling after the pause to process the remaining 3 links.

![Resuming crawling](/img/resuming-paused-crawl/01.webp 'Resuming crawling.')

Alternatively, set the environment variable `CRAWLEE_PURGE_ON_START=0` instead of `configuration.purge_on_start = False`:
Alternatively, set the environment variable `CRAWLEE_PURGE_ON_START=0` instead of registering `Configuration(purge_on_start=False)`:

```bash
CRAWLEE_PURGE_ON_START=0 python your_crawler.py
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio

from crawlee import ConcurrencySettings, service_locator
from crawlee.configuration import Configuration
from crawlee.crawlers import (
BeautifulSoupCrawler,
BeautifulSoupCrawlingContext,
Expand All @@ -10,8 +11,8 @@
# This makes the scraper continue from where it left off in the previous run.
# The recommended way to achieve this behavior is setting the environment variable
# `CRAWLEE_PURGE_ON_START=0`
configuration = service_locator.get_configuration()
configuration.purge_on_start = False
configuration = Configuration(purge_on_start=False)
service_locator.set_configuration(configuration)


async def main() -> None:
Expand Down
5 changes: 4 additions & 1 deletion src/crawlee/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ class Configuration(BaseSettings):
operations, and more.

Settings can also be configured via environment variables, prefixed with `CRAWLEE_`.

Instances are immutable, since components read a setting once and do not see a later change. To use a different
value, create a new instance with it, or use `model_copy(update=...)`, which does not validate the new values.
"""

# TODO: https://github.com/pydantic/pydantic-settings/issues/706
# Use `SettingsConfigDict(validate_by_name=True, validate_by_alias=True)` when issue is resolved.
model_config = SettingsConfigDict(populate_by_name=True)
model_config = SettingsConfigDict(populate_by_name=True, frozen=True)

internal_timeout: Annotated[timedelta | None, Field(alias='crawlee_internal_timeout')] = None
"""Timeout for the internal asynchronous operations."""
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
from pydantic import ValidationError
from pydantic_settings import SettingsConfigDict

from crawlee import service_locator
from crawlee.configuration import Configuration
Expand Down Expand Up @@ -45,3 +46,39 @@ def test_global_configuration_works_reversed() -> None:
is Configuration.get_global_configuration()
is Configuration.get_global_configuration()
)


def test_configuration_rejects_attribute_assignment() -> None:
"""Assigning to a setting raises and leaves the instance unchanged."""
configuration = Configuration(purge_on_start=True)

with pytest.raises(ValidationError, match='frozen'):
configuration.purge_on_start = False # ty: ignore[invalid-assignment]

assert configuration.purge_on_start is True


def test_configuration_model_copy_returns_changed_copy() -> None:
"""`model_copy(update=...)` returns a new instance with the value and leaves the original unchanged."""
configuration = Configuration(purge_on_start=True)

changed = configuration.model_copy(update={'purge_on_start': False})

assert changed.purge_on_start is False
assert configuration.purge_on_start is True


def test_configuration_subclass_with_own_model_config_is_frozen() -> None:
"""A subclass that declares its own `model_config` still rejects attribute assignment."""

class ExtendedConfiguration(Configuration):
model_config = SettingsConfigDict(env_prefix='extended_')

extra_setting: int = 1

configuration = ExtendedConfiguration()

with pytest.raises(ValidationError, match='frozen'):
configuration.extra_setting = 2 # ty: ignore[invalid-assignment]

assert configuration.extra_setting == 1