Skip to content

Commit 61c516d

Browse files
authored
chore: Replace populate_by_name with validate_by_name and validate_by_alias for Pydantic ConfigDict (#1412)
### Description - Since we have bumped the minimum version of pydantic to 2.11, using `populate_by_name` is [not recommended](https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.populate_by_name).
1 parent 3627919 commit 61c516d

File tree

12 files changed

+30
-30
lines changed

12 files changed

+30
-30
lines changed

src/crawlee/_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Request(BaseModel):
163163
```
164164
"""
165165

166-
model_config = ConfigDict(populate_by_name=True)
166+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
167167

168168
unique_key: Annotated[str, Field(alias='uniqueKey')]
169169
"""A unique key identifying the request. Two requests with the same `unique_key` are considered as pointing

src/crawlee/_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _normalize_headers(headers: Mapping[str, str]) -> dict[str, str]:
6969
class HttpHeaders(RootModel, Mapping[str, str]):
7070
"""A dictionary-like object representing HTTP headers."""
7171

72-
model_config = ConfigDict(populate_by_name=True)
72+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
7373

7474
root: Annotated[
7575
dict[str, str],

src/crawlee/_utils/system.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _get_used_memory(process: psutil.Process) -> int:
3636
class CpuInfo(BaseModel):
3737
"""Information about the CPU usage."""
3838

39-
model_config = ConfigDict(populate_by_name=True)
39+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
4040

4141
used_ratio: Annotated[float, Field(alias='usedRatio')]
4242
"""The ratio of CPU currently in use, represented as a float between 0 and 1."""
@@ -51,7 +51,7 @@ class CpuInfo(BaseModel):
5151
class MemoryUsageInfo(BaseModel):
5252
"""Information about the memory usage."""
5353

54-
model_config = ConfigDict(populate_by_name=True)
54+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
5555

5656
current_size: Annotated[
5757
ByteSize,
@@ -71,7 +71,7 @@ class MemoryUsageInfo(BaseModel):
7171
class MemoryInfo(MemoryUsageInfo):
7272
"""Information about system memory."""
7373

74-
model_config = ConfigDict(populate_by_name=True)
74+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
7575

7676
total_size: Annotated[
7777
ByteSize, PlainValidator(ByteSize.validate), PlainSerializer(lambda size: size.bytes), Field(alias='totalSize')

src/crawlee/crawlers/_adaptive_playwright/_adaptive_playwright_crawler_statistics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class AdaptivePlaywrightCrawlerStatisticState(StatisticsState):
1313
"""Statistic data about a crawler run with additional information related to adaptive crawling."""
1414

15-
model_config = ConfigDict(populate_by_name=True, ser_json_inf_nan='constants')
15+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True, ser_json_inf_nan='constants')
1616

1717
http_only_request_handler_runs: Annotated[int, Field(alias='http_only_request_handler_runs')] = 0
1818
"""Number representing how many times static http based crawling was used."""

src/crawlee/crawlers/_adaptive_playwright/_rendering_type_predictor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
class RenderingTypePredictorState(BaseModel):
35-
model_config = ConfigDict(populate_by_name=True)
35+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
3636

3737
model: Annotated[
3838
LogisticRegression,

src/crawlee/events/_types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Event(str, Enum):
4040
class EventPersistStateData(BaseModel):
4141
"""Data for the persist state event."""
4242

43-
model_config = ConfigDict(populate_by_name=True)
43+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
4444

4545
is_migrating: Annotated[bool, Field(alias='isMigrating')]
4646

@@ -49,7 +49,7 @@ class EventPersistStateData(BaseModel):
4949
class EventSystemInfoData(BaseModel):
5050
"""Data for the system info event."""
5151

52-
model_config = ConfigDict(populate_by_name=True)
52+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
5353

5454
cpu_info: Annotated[CpuInfo, Field(alias='cpuInfo')]
5555
memory_info: Annotated[
@@ -62,7 +62,7 @@ class EventSystemInfoData(BaseModel):
6262
class EventMigratingData(BaseModel):
6363
"""Data for the migrating event."""
6464

65-
model_config = ConfigDict(populate_by_name=True)
65+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
6666

6767
# The remaining time in seconds before the migration is forced and the process is killed
6868
# Optional because it's not present when the event handler is called manually
@@ -73,21 +73,21 @@ class EventMigratingData(BaseModel):
7373
class EventAbortingData(BaseModel):
7474
"""Data for the aborting event."""
7575

76-
model_config = ConfigDict(populate_by_name=True)
76+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
7777

7878

7979
@docs_group('Event data')
8080
class EventExitData(BaseModel):
8181
"""Data for the exit event."""
8282

83-
model_config = ConfigDict(populate_by_name=True)
83+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
8484

8585

8686
@docs_group('Event data')
8787
class EventCrawlerStatusData(BaseModel):
8888
"""Data for the crawler status event."""
8989

90-
model_config = ConfigDict(populate_by_name=True)
90+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
9191

9292
message: str
9393
"""A message describing the current status of the crawler."""

src/crawlee/fingerprint_suite/_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class ScreenOptions(BaseModel):
14-
model_config = ConfigDict(extra='forbid', populate_by_name=True)
14+
model_config = ConfigDict(extra='forbid', validate_by_name=True, validate_by_alias=True)
1515

1616
"""Defines the screen constrains for the fingerprint generator."""
1717

@@ -31,7 +31,7 @@ class ScreenOptions(BaseModel):
3131
class HeaderGeneratorOptions(BaseModel):
3232
"""Collection of header related attributes that can be used by the fingerprint generator."""
3333

34-
model_config = ConfigDict(extra='forbid', populate_by_name=True)
34+
model_config = ConfigDict(extra='forbid', validate_by_name=True, validate_by_alias=True)
3535

3636
browsers: list[SupportedBrowserType] | None = None
3737
"""List of BrowserSpecifications to generate the headers for."""

src/crawlee/request_loaders/_request_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
class RequestListState(BaseModel):
20-
model_config = ConfigDict(populate_by_name=True)
20+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
2121

2222
next_index: Annotated[int, Field(alias='nextIndex')] = 0
2323
next_unique_key: Annotated[str | None, Field(alias='nextUniqueKey')] = None

src/crawlee/request_loaders/_sitemap_request_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SitemapRequestLoaderState(BaseModel):
5656
`in_progress` is cleared.
5757
"""
5858

59-
model_config = ConfigDict(populate_by_name=True)
59+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
6060

6161
url_queue: Annotated[deque[str], Field(alias='urlQueue')]
6262
"""Queue of URLs extracted from sitemaps and ready for processing."""

src/crawlee/sessions/_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class SessionModel(BaseModel):
2121
"""Model for a Session object."""
2222

23-
model_config = ConfigDict(populate_by_name=True)
23+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
2424

2525
id: Annotated[str, Field(alias='id')]
2626
max_age: Annotated[timedelta, Field(alias='maxAge')]
@@ -38,7 +38,7 @@ class SessionModel(BaseModel):
3838
class SessionPoolModel(BaseModel):
3939
"""Model for a SessionPool object."""
4040

41-
model_config = ConfigDict(populate_by_name=True)
41+
model_config = ConfigDict(validate_by_name=True, validate_by_alias=True)
4242

4343
max_pool_size: Annotated[int, Field(alias='maxPoolSize')]
4444

0 commit comments

Comments
 (0)