Skip to content

Commit cf53178

Browse files
committed
Replace Werkzeug to save some disk space
1 parent f900c58 commit cf53178

File tree

6 files changed

+14
-25
lines changed

6 files changed

+14
-25
lines changed

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ dependencies = [
3434
"crawlee >= 0.3.0",
3535
"cryptography >= 39.0.0",
3636
"httpx >= 0.24.0",
37+
"lazy-object-proxy >= 1.10.0",
3738
"psutil >= 5.9.0",
3839
"pyee >= 11.0.0",
3940
"sortedcollections >= 2.0.0",
4041
"typing-extensions >= 4.1.0",
4142
"websockets >= 10.1",
42-
"werkzeug >= 3.0.0",
4343
]
4444

4545
[project.optional-dependencies]
@@ -185,5 +185,5 @@ warn_unreachable = true
185185
warn_unused_ignores = true
186186

187187
[[tool.mypy.overrides]]
188-
module = ['scrapy', 'scrapy.*', 'sortedcollections']
188+
module = ['scrapy', 'scrapy.*', 'sortedcollections', 'lazy_object_proxy']
189189
ignore_missing_imports = true

src/apify/actor.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from datetime import timedelta
77
from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast
88

9+
from lazy_object_proxy import Proxy
910
from pydantic import AliasChoices
1011
from typing_extensions import Self
11-
from werkzeug.local import LocalProxy
1212

1313
from apify._crypto import decrypt_input_secrets, load_private_key
1414
from apify._utils import get_system_info, is_running_in_ipython
@@ -103,7 +103,7 @@ async def __aexit__(
103103
await self.exit()
104104

105105
def __repr__(self) -> str:
106-
if self is _default_instance:
106+
if self is cast(Proxy, Actor).__wrapped__:
107107
return '<apify.Actor>'
108108

109109
return super().__repr__()
@@ -933,17 +933,5 @@ async def create_proxy_configuration(
933933
return proxy_configuration
934934

935935

936-
_default_instance: _ActorType | None = None
937-
938-
939-
def _get_default_instance() -> _ActorType:
940-
global _default_instance # noqa: PLW0603
941-
942-
if not _default_instance:
943-
_default_instance = _ActorType()
944-
945-
return _default_instance
946-
947-
948-
Actor = cast(_ActorType, LocalProxy(_get_default_instance))
936+
Actor = cast(_ActorType, Proxy(_ActorType))
949937
"""The entry point of the SDK, through which all the Actor operations should be done."""

tests/integration/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _reset_and_patch_default_instances() -> None:
3535
from crawlee import service_container
3636

3737
cast(dict, service_container._services).clear()
38-
apify.actor._default_instance = None
38+
delattr(apify.actor.Actor, '__wrapped__')
3939

4040
# TODO: StorageClientManager local storage client purge # noqa: TD003
4141

tests/integration/test_actor_lifecycle.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ async def main() -> None:
4545
import apify.actor
4646

4747
async with Actor:
48-
assert apify.actor._get_default_instance()._is_initialized
49-
assert apify.actor._get_default_instance()._is_initialized is False
48+
assert apify.actor.Actor._is_initialized
49+
assert apify.actor.Actor._is_initialized is False
5050

5151
actor = await make_actor('with-actor-init', main_func=main)
5252

tests/unit/actor/test_actor_lifecycle.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import asyncio
44
import contextlib
55
import json
6-
from typing import Any, Callable
6+
from typing import Any, Callable, cast
77

88
import pytest
99
import websockets.server
10+
from lazy_object_proxy import Proxy
1011

1112
import apify.actor
1213
from apify.actor import Actor, _ActorType
@@ -17,9 +18,9 @@
1718
class TestActorInit:
1819
async def test_async_with_actor_properly_initialize(self: TestActorInit) -> None:
1920
async with Actor:
20-
assert apify.actor._default_instance is not None
21-
assert apify.actor._default_instance._is_initialized
22-
assert not apify.actor._default_instance._is_initialized
21+
assert cast(Proxy, apify.actor.Actor).__wrapped__ is not None
22+
assert cast(Proxy, apify.actor.Actor).__wrapped__._is_initialized
23+
assert not cast(Proxy, apify.actor.Actor).__wrapped__._is_initialized
2324

2425
async def test_actor_init(self: TestActorInit) -> None:
2526
my_actor = _ActorType()

tests/unit/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def reset() -> None:
4141

4242
cast(dict, service_container._services).clear()
4343

44-
apify.actor._default_instance = None
44+
delattr(apify.actor.Actor, '__wrapped__')
4545
# TODO: local storage client purge # noqa: TD003
4646

4747
return reset

0 commit comments

Comments
 (0)