Skip to content

Commit 432c79c

Browse files
committed
Remove any monkey patching from Configuration
1 parent 0b96454 commit 432c79c

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
lines changed

src/apify/_actor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ async def init(self) -> None:
271271
'Using the existing configuration.'
272272
)
273273
# Use the configuration from the service locator
274-
self._configuration = service_locator.get_configuration()
274+
self._configuration = Configuration.get_global_configuration()
275275

276276
if self._is_initialized:
277277
raise RuntimeError('The Actor was already initialized!')

src/apify/_configuration.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
from __future__ import annotations
22

33
import traceback
4-
import types
54
from datetime import datetime, timedelta
65
from decimal import Decimal
76
from logging import getLogger
8-
from typing import TYPE_CHECKING, Annotated, Any, cast
7+
from typing import Annotated, Any
98

109
from pydantic import AliasChoices, BeforeValidator, Field, model_validator
1110
from typing_extensions import Self, deprecated
1211

13-
import crawlee
1412
from crawlee import service_locator
1513
from crawlee._utils.models import timedelta_ms
1614
from crawlee._utils.urls import validate_http_url
1715
from crawlee.configuration import Configuration as CrawleeConfiguration
1816

1917
from apify._utils import docs_group
2018

21-
if TYPE_CHECKING:
22-
from crawlee._service_locator import ServiceLocator
23-
2419
logger = getLogger(__name__)
2520

2621

@@ -432,38 +427,23 @@ def disable_browser_sandbox_on_platform(self) -> Self:
432427
def get_global_configuration(cls) -> Configuration:
433428
"""Retrieve the global instance of the configuration.
434429
435-
Mostly for the backwards compatibility. It is recommended to use the `service_locator.get_configuration()`
436-
instead.
430+
This method ensures that ApifyConfigration is returned, even if CrawleeConfiguration was set in the
431+
service locator.
437432
"""
438-
return cast('Configuration', service_locator.get_configuration())
439-
440-
441-
def _patch_service_locator() -> None:
442-
"""Patch the Crawlee ServiceLocator to ensure that Apify Configuration is always returned."""
443-
old_get_configuration = crawlee.service_locator.get_configuration
433+
global_configuration = service_locator.get_configuration()
444434

445-
def get_configuration(self: ServiceLocator) -> Configuration:
446-
# ApifyServiceLocator can store any children of Crawlee Configuration, but in Apify context it is desired to
447-
# return Apify Configuration.
448-
if isinstance(self._configuration, Configuration):
435+
if isinstance(global_configuration, Configuration):
449436
# If Apify configuration was already stored in service locator, return it.
450-
return self._configuration
437+
return global_configuration
451438

452-
stored_configuration = old_get_configuration()
453439
apify_configuration = Configuration()
454440

455441
# Ensure the returned configuration is of type Apify Configuration.
456442
# Most likely crawlee configuration was already set. Create Apify configuration from it.
457443
# Due to known Pydantic issue https://github.com/pydantic/pydantic/issues/9516, creating new instance of
458444
# Configuration from existing one in situation where environment can have some fields set by alias is very
459445
# unpredictable. Use the stable workaround.
460-
for name in stored_configuration.model_fields:
461-
setattr(apify_configuration, name, getattr(stored_configuration, name))
446+
for name in global_configuration.model_fields:
447+
setattr(apify_configuration, name, getattr(global_configuration, name))
462448

463449
return apify_configuration
464-
465-
crawlee.service_locator.get_configuration = types.MethodType(get_configuration, crawlee.service_locator)
466-
467-
468-
# Ensure that ApifyServiceLocator is used to make sure Apify Configuration is used.
469-
_patch_service_locator()

0 commit comments

Comments
 (0)