Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies = [
"protego>=0.5.0",
"psutil>=6.0.0",
"pydantic-settings>=2.2.0,!=2.7.0,!=2.7.1,!=2.8.0",
"pydantic>=2.11.0,<2.12.0",
"pydantic>=2.11.0",
"pyee>=9.0.0",
"tldextract>=5.1.0",
"typing-extensions>=4.1.0",
Expand Down
51 changes: 31 additions & 20 deletions src/crawlee/_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,33 +185,44 @@ class Request(BaseModel):
method: HttpMethod = 'GET'
"""HTTP request method."""

headers: Annotated[HttpHeaders, Field(default_factory=HttpHeaders)] = HttpHeaders()
"""HTTP request headers."""

payload: Annotated[
HttpPayload | None,
BeforeValidator(lambda v: v.encode() if isinstance(v, str) else v),
PlainSerializer(lambda v: v.decode() if isinstance(v, bytes) else v),
] = None
"""HTTP request payload."""

user_data: Annotated[
dict[str, JsonSerializable], # Internally, the model contains `UserData`, this is just for convenience
Field(alias='userData', default_factory=lambda: UserData()),
PlainValidator(user_data_adapter.validate_python),
PlainSerializer(
lambda instance: user_data_adapter.dump_python(
instance,
by_alias=True,
exclude_none=True,
exclude_unset=True,
exclude_defaults=True,
)
),
] = {}
"""Custom user data assigned to the request. Use this to save any request related data to the
request's scope, keeping them accessible on retries, failures etc.
"""
# Workaround for pydantic 2.12 and mypy type checking issue for Annotated with default_factory
if TYPE_CHECKING:
headers: HttpHeaders = HttpHeaders()
"""HTTP request headers."""

user_data: dict[str, JsonSerializable] = {}
"""Custom user data assigned to the request. Use this to save any request related data to the
request's scope, keeping them accessible on retries, failures etc.
"""

else:
headers: Annotated[HttpHeaders, Field(default_factory=HttpHeaders)]
"""HTTP request headers."""

user_data: Annotated[
dict[str, JsonSerializable], # Internally, the model contains `UserData`, this is just for convenience
Field(alias='userData', default_factory=lambda: UserData()),
PlainValidator(user_data_adapter.validate_python),
PlainSerializer(
lambda instance: user_data_adapter.dump_python(
instance,
by_alias=True,
exclude_none=True,
exclude_unset=True,
exclude_defaults=True,
)
),
]
"""Custom user data assigned to the request. Use this to save any request related data to the
request's scope, keeping them accessible on retries, failures etc.
"""

retry_count: Annotated[int, Field(alias='retryCount')] = 0
"""Number of times the request has been retried."""
Expand Down
26 changes: 10 additions & 16 deletions src/crawlee/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@
import dataclasses
from collections.abc import Callable, Iterator, Mapping
from dataclasses import dataclass
from typing import (
TYPE_CHECKING,
Annotated,
Any,
Literal,
Protocol,
TypedDict,
TypeVar,
cast,
overload,
)
from typing import TYPE_CHECKING, Annotated, Any, Literal, Protocol, TypedDict, TypeVar, cast, overload

from pydantic import ConfigDict, Field, PlainValidator, RootModel

Expand Down Expand Up @@ -71,11 +61,15 @@ class HttpHeaders(RootModel, Mapping[str, str]):

model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)

root: Annotated[
dict[str, str],
PlainValidator(lambda value: _normalize_headers(value)),
Field(default_factory=dict),
] = {}
# Workaround for pydantic 2.12 and mypy type checking issue for Annotated with default_factory
if TYPE_CHECKING:
root: dict[str, str] = {}
else:
root: Annotated[
dict[str, str],
PlainValidator(lambda value: _normalize_headers(value)),
Field(default_factory=dict),
]

def __getitem__(self, key: str) -> str:
return self.root[key.lower()]
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading