Skip to content

Commit

Permalink
Use absolute imports (#177)
Browse files Browse the repository at this point in the history
Closes #160

Co-authored-by: Honza Javorek <mail@honzajavorek.cz>
  • Loading branch information
vdusek and honzajavorek committed Jan 19, 2024
1 parent e6174f2 commit 708078b
Show file tree
Hide file tree
Showing 51 changed files with 137 additions and 149 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

- Create a new subpackage for Scrapy pipelines
- Remove some noqas thanks to the new Ruff release
- Replace relative imports with absolute imports
- Replace asserts with custom checks in Scrapy subpackage

### Fixed
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Expand Up @@ -26,8 +26,8 @@ requires-python = ">=3.8"
# compatibility with a wide range of external packages. This decision was discussed in detail in the following PR:
# https://github.com/apify/apify-sdk-python/pull/154
dependencies = [
"apify-client ~= 1.6.0",
"apify-shared ~= 1.1.0",
"apify-client ~= 1.6.2",
"apify-shared ~= 1.1.1",
"aiofiles >= 22.1.0",
"aioshutil >= 1.0",
"colorama >= 0.4.6",
Expand Down Expand Up @@ -111,7 +111,6 @@ ignore = [
"S303", # Use of insecure MD2, MD4, MD5, or SHA1 hash function
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
"TD002", # Missing author in TODO; try: `# TODO(<author_name>): ...` or `# TODO @<author_name>: ...
"TID252", # Relative imports from parent modules are bannedRuff
"TRY003", # Avoid specifying long messages outside the exception class

# TODO: Remove this once the following issue is fixed
Expand Down Expand Up @@ -139,6 +138,7 @@ indent-style = "space"
"PLR2004", # Magic value used in comparison, consider replacing {value} with a constant variable
"S101", # Use of assert detected
"T20", # flake8-print
"TID252", # Relative imports from parent modules are banned
"TRY301", # Abstract `raise` to an inner function
]

Expand All @@ -147,7 +147,7 @@ docstring-quotes = "double"
inline-quotes = "single"

[tool.ruff.lint.isort]
known-first-party = ["apify", "apify_client", "apify_shared"]
known-local-folder = ["apify"]

[tool.ruff.lint.pydocstyle]
convention = "google"
5 changes: 2 additions & 3 deletions src/apify/_crypto.py
Expand Up @@ -4,14 +4,13 @@
import secrets
from typing import Any

from apify_shared.utils import ignore_docs
from cryptography.exceptions import InvalidTag as InvalidTagException
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding, rsa
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

from apify_shared.utils import ignore_docs

from .consts import ENCRYPTED_INPUT_VALUE_REGEXP
from apify.consts import ENCRYPTED_INPUT_VALUE_REGEXP

ENCRYPTION_KEY_LENGTH = 32
ENCRYPTION_IV_LENGTH = 16
Expand Down
3 changes: 1 addition & 2 deletions src/apify/_memory_storage/file_storage_utils.py
Expand Up @@ -4,10 +4,9 @@

import aiofiles
from aiofiles.os import makedirs

from apify_shared.utils import json_dumps

from .._utils import force_remove
from apify._utils import force_remove


async def update_metadata(*, data: dict, entity_directory: str, write_metadata: bool) -> None:
Expand Down
15 changes: 7 additions & 8 deletions src/apify/_memory_storage/memory_storage_client.py
Expand Up @@ -8,17 +8,16 @@
import aioshutil
from aiofiles import ospath
from aiofiles.os import rename, scandir

from apify_shared.consts import ApifyEnvVars
from apify_shared.utils import ignore_docs

from .._utils import maybe_parse_bool
from .resource_clients.dataset import DatasetClient
from .resource_clients.dataset_collection import DatasetCollectionClient
from .resource_clients.key_value_store import KeyValueStoreClient
from .resource_clients.key_value_store_collection import KeyValueStoreCollectionClient
from .resource_clients.request_queue import RequestQueueClient
from .resource_clients.request_queue_collection import RequestQueueCollectionClient
from apify._memory_storage.resource_clients.dataset import DatasetClient
from apify._memory_storage.resource_clients.dataset_collection import DatasetCollectionClient
from apify._memory_storage.resource_clients.key_value_store import KeyValueStoreClient
from apify._memory_storage.resource_clients.key_value_store_collection import KeyValueStoreCollectionClient
from apify._memory_storage.resource_clients.request_queue import RequestQueueClient
from apify._memory_storage.resource_clients.request_queue_collection import RequestQueueCollectionClient
from apify._utils import maybe_parse_bool

"""
Memory storage emulates data storages that are available on the Apify platform.
Expand Down
Expand Up @@ -10,7 +10,7 @@
if TYPE_CHECKING:
from typing_extensions import Self

from ..memory_storage_client import MemoryStorageClient
from apify._memory_storage.memory_storage_client import MemoryStorageClient


@ignore_docs
Expand Down
Expand Up @@ -7,11 +7,11 @@
from apify_shared.models import ListPage
from apify_shared.utils import ignore_docs

from ..file_storage_utils import update_metadata
from .base_resource_client import BaseResourceClient
from apify._memory_storage.file_storage_utils import update_metadata
from apify._memory_storage.resource_clients.base_resource_client import BaseResourceClient

if TYPE_CHECKING:
from ..memory_storage_client import MemoryStorageClient
from apify._memory_storage.memory_storage_client import MemoryStorageClient


ResourceClientType = TypeVar('ResourceClientType', bound=BaseResourceClient, contravariant=True) # noqa: PLC0105
Expand Down
13 changes: 6 additions & 7 deletions src/apify/_memory_storage/resource_clients/dataset.py
Expand Up @@ -7,20 +7,19 @@
from typing import TYPE_CHECKING, Any, AsyncIterator

import aioshutil

from apify_shared.models import ListPage
from apify_shared.utils import ignore_docs

from ..._crypto import crypto_random_object_id
from ..._utils import force_rename, raise_on_duplicate_storage, raise_on_non_existing_storage
from ...consts import StorageTypes
from ..file_storage_utils import _update_dataset_items, update_metadata
from .base_resource_client import BaseResourceClient
from apify._crypto import crypto_random_object_id
from apify._memory_storage.file_storage_utils import _update_dataset_items, update_metadata
from apify._memory_storage.resource_clients.base_resource_client import BaseResourceClient
from apify._utils import force_rename, raise_on_duplicate_storage, raise_on_non_existing_storage
from apify.consts import StorageTypes

if TYPE_CHECKING:
from apify_shared.types import JSONSerializable

from ..memory_storage_client import MemoryStorageClient
from apify._memory_storage.memory_storage_client import MemoryStorageClient

# This is what API returns in the x-apify-pagination-limit
# header when no limit query parameter is used.
Expand Down
Expand Up @@ -4,8 +4,8 @@

from apify_shared.utils import ignore_docs

from .base_resource_collection_client import BaseResourceCollectionClient
from .dataset import DatasetClient
from apify._memory_storage.resource_clients.base_resource_collection_client import BaseResourceCollectionClient
from apify._memory_storage.resource_clients.dataset import DatasetClient

if TYPE_CHECKING:
from apify_shared.models import ListPage
Expand Down
15 changes: 7 additions & 8 deletions src/apify/_memory_storage/resource_clients/key_value_store.py
Expand Up @@ -13,27 +13,26 @@
import aiofiles
import aioshutil
from aiofiles.os import makedirs

from apify_shared.utils import ignore_docs, is_file_or_bytes, json_dumps

from ..._crypto import crypto_random_object_id
from ..._utils import (
from apify._crypto import crypto_random_object_id
from apify._memory_storage.file_storage_utils import update_metadata
from apify._memory_storage.resource_clients.base_resource_client import BaseResourceClient
from apify._utils import (
force_remove,
force_rename,
guess_file_extension,
maybe_parse_body,
raise_on_duplicate_storage,
raise_on_non_existing_storage,
)
from ...consts import DEFAULT_API_PARAM_LIMIT, StorageTypes
from ...log import logger
from ..file_storage_utils import update_metadata
from .base_resource_client import BaseResourceClient
from apify.consts import DEFAULT_API_PARAM_LIMIT, StorageTypes
from apify.log import logger

if TYPE_CHECKING:
from typing_extensions import NotRequired

from ..memory_storage_client import MemoryStorageClient
from apify._memory_storage.memory_storage_client import MemoryStorageClient


class KeyValueStoreRecord(TypedDict):
Expand Down
Expand Up @@ -4,8 +4,8 @@

from apify_shared.utils import ignore_docs

from .base_resource_collection_client import BaseResourceCollectionClient
from .key_value_store import KeyValueStoreClient
from apify._memory_storage.resource_clients.base_resource_collection_client import BaseResourceCollectionClient
from apify._memory_storage.resource_clients.key_value_store import KeyValueStoreClient

if TYPE_CHECKING:
from apify_shared.models import ListPage
Expand Down
15 changes: 7 additions & 8 deletions src/apify/_memory_storage/resource_clients/request_queue.py
Expand Up @@ -8,18 +8,17 @@
from typing import TYPE_CHECKING

import aioshutil
from sortedcollections import ValueSortedDict

from apify_shared.utils import filter_out_none_values_recursively, ignore_docs, json_dumps
from sortedcollections import ValueSortedDict

from ..._crypto import crypto_random_object_id
from ..._utils import force_rename, raise_on_duplicate_storage, raise_on_non_existing_storage, unique_key_to_request_id
from ...consts import StorageTypes
from ..file_storage_utils import delete_request, update_metadata, update_request_queue_item
from .base_resource_client import BaseResourceClient
from apify._crypto import crypto_random_object_id
from apify._memory_storage.file_storage_utils import delete_request, update_metadata, update_request_queue_item
from apify._memory_storage.resource_clients.base_resource_client import BaseResourceClient
from apify._utils import force_rename, raise_on_duplicate_storage, raise_on_non_existing_storage, unique_key_to_request_id
from apify.consts import StorageTypes

if TYPE_CHECKING:
from ..memory_storage_client import MemoryStorageClient
from apify._memory_storage.memory_storage_client import MemoryStorageClient


@ignore_docs
Expand Down
Expand Up @@ -4,8 +4,8 @@

from apify_shared.utils import ignore_docs

from .base_resource_collection_client import BaseResourceCollectionClient
from .request_queue import RequestQueueClient
from apify._memory_storage.resource_clients.base_resource_collection_client import BaseResourceCollectionClient
from apify._memory_storage.resource_clients.request_queue import RequestQueueClient

if TYPE_CHECKING:
from apify_shared.models import ListPage
Expand Down
3 changes: 1 addition & 2 deletions src/apify/_utils.py
Expand Up @@ -35,7 +35,6 @@
import psutil
from aiofiles import ospath
from aiofiles.os import remove, rename

from apify_shared.consts import (
BOOL_ENV_VARS,
BOOL_ENV_VARS_TYPE,
Expand All @@ -57,7 +56,7 @@
maybe_extract_enum_member_value,
)

from .consts import REQUEST_ID_LENGTH, StorageTypes
from apify.consts import REQUEST_ID_LENGTH, StorageTypes

T = TypeVar('T')

Expand Down
18 changes: 9 additions & 9 deletions src/apify/actor.py
Expand Up @@ -12,8 +12,8 @@
from apify_shared.consts import ActorEnvVars, ActorEventTypes, ActorExitCodes, ApifyEnvVars, WebhookEventType
from apify_shared.utils import ignore_docs, maybe_extract_enum_member_value

from ._crypto import decrypt_input_secrets, load_private_key
from ._utils import (
from apify._crypto import decrypt_input_secrets, load_private_key
from apify._utils import (
dualproperty,
fetch_and_parse_env_var,
get_cpu_usage_percent,
Expand All @@ -23,18 +23,18 @@
run_func_at_interval_async,
wrap_internal,
)
from .config import Configuration
from .consts import EVENT_LISTENERS_TIMEOUT_SECS
from .event_manager import EventManager
from .log import logger
from .proxy_configuration import ProxyConfiguration
from .storages import Dataset, KeyValueStore, RequestQueue, StorageClientManager
from apify.config import Configuration
from apify.consts import EVENT_LISTENERS_TIMEOUT_SECS
from apify.event_manager import EventManager
from apify.log import logger
from apify.proxy_configuration import ProxyConfiguration
from apify.storages import Dataset, KeyValueStore, RequestQueue, StorageClientManager

if TYPE_CHECKING:
import logging
from types import TracebackType

from ._memory_storage import MemoryStorageClient
from apify._memory_storage import MemoryStorageClient

T = TypeVar('T')
MainReturnType = TypeVar('MainReturnType')
Expand Down
2 changes: 1 addition & 1 deletion src/apify/config.py
Expand Up @@ -2,7 +2,7 @@

from apify_shared.consts import ActorEnvVars, ApifyEnvVars

from ._utils import fetch_and_parse_env_var
from apify._utils import fetch_and_parse_env_var


class Configuration:
Expand Down
7 changes: 3 additions & 4 deletions src/apify/event_manager.py
Expand Up @@ -8,16 +8,15 @@
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Union

import websockets.client
from pyee.asyncio import AsyncIOEventEmitter

from apify_shared.utils import ignore_docs, maybe_extract_enum_member_value, parse_date_fields
from pyee.asyncio import AsyncIOEventEmitter

from .log import logger
from apify.log import logger

if TYPE_CHECKING:
from apify_shared.consts import ActorEventTypes

from .config import Configuration
from apify.config import Configuration

ListenerType = Union[Callable[[], None], Callable[[Any], None], Callable[[], Coroutine[Any, Any, None]], Callable[[Any], Coroutine[Any, Any, None]]]

Expand Down
3 changes: 1 addition & 2 deletions src/apify/log.py
Expand Up @@ -6,9 +6,8 @@
import traceback
from typing import Any

from colorama import Fore, Style, just_fix_windows_console

from apify_shared.utils import ignore_docs
from colorama import Fore, Style, just_fix_windows_console

just_fix_windows_console()

Expand Down
8 changes: 3 additions & 5 deletions src/apify/proxy_configuration.py
Expand Up @@ -7,17 +7,15 @@
from urllib.parse import urljoin, urlparse

import httpx

from apify_shared.consts import ApifyEnvVars
from apify_shared.utils import ignore_docs

from .config import Configuration
from .log import logger
from apify.config import Configuration
from apify.log import logger

if TYPE_CHECKING:
from typing_extensions import NotRequired

from apify_client import ApifyClientAsync
from typing_extensions import NotRequired

APIFY_PROXY_VALUE_REGEX = re.compile(r'^[\w._~]+$')
COUNTRY_CODE_REGEX = re.compile(r'^[A-Z]{2}$')
Expand Down
6 changes: 3 additions & 3 deletions src/apify/scrapy/middlewares/apify_proxy.py
Expand Up @@ -12,9 +12,9 @@
'To use this module, you need to install the "scrapy" extra. Run "pip install apify[scrapy]".',
) from exc

from ...actor import Actor
from ...proxy_configuration import ProxyConfiguration
from ..utils import get_basic_auth_header
from apify.actor import Actor
from apify.proxy_configuration import ProxyConfiguration
from apify.scrapy.utils import get_basic_auth_header


class ApifyHttpProxyMiddleware:
Expand Down
6 changes: 3 additions & 3 deletions src/apify/scrapy/middlewares/apify_retry.py
Expand Up @@ -13,11 +13,11 @@
'To use this module, you need to install the "scrapy" extra. Run "pip install apify[scrapy]".',
) from exc

from ...actor import Actor
from ..utils import nested_event_loop, open_queue_with_custom_client, to_apify_request
from apify.actor import Actor
from apify.scrapy.utils import nested_event_loop, open_queue_with_custom_client, to_apify_request

if TYPE_CHECKING:
from ...storages import RequestQueue
from apify.storages import RequestQueue


class ApifyRetryMiddleware(RetryMiddleware):
Expand Down
2 changes: 1 addition & 1 deletion src/apify/scrapy/pipelines/actor_dataset_push.py
Expand Up @@ -9,7 +9,7 @@
'To use this module, you need to install the "scrapy" extra. Run "pip install apify[scrapy]".',
) from exc

from ...actor import Actor
from apify.actor import Actor


class ActorDatasetPushPipeline:
Expand Down

0 comments on commit 708078b

Please sign in to comment.