Skip to content

Commit 72efe88

Browse files
vdusekclaude
andauthored
fix: Fix BeforeValidator treating 0 as falsy in configuration fields (#819)
## Summary - The `BeforeValidator(lambda val: val or None)` on `max_paid_dataset_items` and `max_total_charge_usd` configuration fields treated `0` as falsy, converting it to `None`. This meant a legitimate value of `0` (e.g. a zero charge limit) would be silently discarded. - Replaced with `lambda val: val if val != '' else None` which only converts empty strings (from unset env vars) to `None`, preserving `0` as a valid value. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f980b0a commit 72efe88

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/apify/_configuration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class Configuration(CrawleeConfiguration):
295295
alias='actor_max_paid_dataset_items',
296296
description='For paid-per-result Actors, the user-set limit on returned results. Do not exceed this limit',
297297
),
298-
BeforeValidator(lambda val: val or None),
298+
BeforeValidator(lambda val: val if val != '' else None),
299299
] = None
300300

301301
max_total_charge_usd: Annotated[
@@ -304,7 +304,7 @@ class Configuration(CrawleeConfiguration):
304304
alias='actor_max_total_charge_usd',
305305
description='For pay-per-event Actors, the user-set limit on total charges. Do not exceed this limit',
306306
),
307-
BeforeValidator(lambda val: val or None),
307+
BeforeValidator(lambda val: val if val != '' else None),
308308
] = None
309309

310310
test_pay_per_event: Annotated[

tests/unit/actor/test_configuration.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from decimal import Decimal
23
from pathlib import Path
34

45
import pytest
@@ -275,6 +276,34 @@ def test_default_values() -> None:
275276
assert config.test_pay_per_event is False
276277

277278

279+
def test_max_paid_dataset_items_zero_is_preserved(monkeypatch: pytest.MonkeyPatch) -> None:
280+
"""Test that max_paid_dataset_items=0 is not treated as falsy and converted to None."""
281+
monkeypatch.setenv('ACTOR_MAX_PAID_DATASET_ITEMS', '0')
282+
config = ApifyConfiguration()
283+
assert config.max_paid_dataset_items == 0
284+
285+
286+
def test_max_total_charge_usd_zero_is_preserved(monkeypatch: pytest.MonkeyPatch) -> None:
287+
"""Test that max_total_charge_usd=0 is not treated as falsy and converted to None."""
288+
monkeypatch.setenv('ACTOR_MAX_TOTAL_CHARGE_USD', '0')
289+
config = ApifyConfiguration()
290+
assert config.max_total_charge_usd == Decimal(0)
291+
292+
293+
def test_max_paid_dataset_items_empty_string_becomes_none(monkeypatch: pytest.MonkeyPatch) -> None:
294+
"""Test that an empty env var for max_paid_dataset_items is converted to None."""
295+
monkeypatch.setenv('ACTOR_MAX_PAID_DATASET_ITEMS', '')
296+
config = ApifyConfiguration()
297+
assert config.max_paid_dataset_items is None
298+
299+
300+
def test_max_total_charge_usd_empty_string_becomes_none(monkeypatch: pytest.MonkeyPatch) -> None:
301+
"""Test that an empty env var for max_total_charge_usd is converted to None."""
302+
monkeypatch.setenv('ACTOR_MAX_TOTAL_CHARGE_USD', '')
303+
config = ApifyConfiguration()
304+
assert config.max_total_charge_usd is None
305+
306+
278307
def test_max_total_charge_usd_decimal_parsing(monkeypatch: pytest.MonkeyPatch) -> None:
279308
"""Test that max_total_charge_usd is parsed as Decimal from env var."""
280309
from decimal import Decimal

0 commit comments

Comments
 (0)