|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import traceback |
4 | | -import types |
5 | 4 | from datetime import datetime, timedelta |
6 | 5 | from decimal import Decimal |
7 | 6 | from logging import getLogger |
8 | | -from typing import TYPE_CHECKING, Annotated, Any, cast |
| 7 | +from typing import Annotated, Any |
9 | 8 |
|
10 | 9 | from pydantic import AliasChoices, BeforeValidator, Field, model_validator |
11 | 10 | from typing_extensions import Self, deprecated |
12 | 11 |
|
13 | | -import crawlee |
14 | 12 | from crawlee import service_locator |
15 | 13 | from crawlee._utils.models import timedelta_ms |
16 | 14 | from crawlee._utils.urls import validate_http_url |
17 | 15 | from crawlee.configuration import Configuration as CrawleeConfiguration |
18 | 16 |
|
19 | 17 | from apify._utils import docs_group |
20 | 18 |
|
21 | | -if TYPE_CHECKING: |
22 | | - from crawlee._service_locator import ServiceLocator |
23 | | - |
24 | 19 | logger = getLogger(__name__) |
25 | 20 |
|
26 | 21 |
|
@@ -432,38 +427,23 @@ def disable_browser_sandbox_on_platform(self) -> Self: |
432 | 427 | def get_global_configuration(cls) -> Configuration: |
433 | 428 | """Retrieve the global instance of the configuration. |
434 | 429 |
|
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. |
437 | 432 | """ |
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() |
444 | 434 |
|
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): |
449 | 436 | # If Apify configuration was already stored in service locator, return it. |
450 | | - return self._configuration |
| 437 | + return global_configuration |
451 | 438 |
|
452 | | - stored_configuration = old_get_configuration() |
453 | 439 | apify_configuration = Configuration() |
454 | 440 |
|
455 | 441 | # Ensure the returned configuration is of type Apify Configuration. |
456 | 442 | # Most likely crawlee configuration was already set. Create Apify configuration from it. |
457 | 443 | # Due to known Pydantic issue https://github.com/pydantic/pydantic/issues/9516, creating new instance of |
458 | 444 | # Configuration from existing one in situation where environment can have some fields set by alias is very |
459 | 445 | # 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)) |
462 | 448 |
|
463 | 449 | 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