fix: treat empty-string env vars as absent in Configuration#965
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #965 +/- ##
==========================================
+ Coverage 89.90% 89.91% +0.01%
==========================================
Files 49 49
Lines 3091 3095 +4
==========================================
+ Hits 2779 2783 +4
Misses 312 312
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
|
||
| def test_actor_storages_env_var_empty_string_becomes_none(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Test that an empty env var for actor_storages is converted to None instead of crashing.""" | ||
| monkeypatch.setenv('ACTOR_STORAGES_JSON', '') |
There was a problem hiding this comment.
Isn't this actually less defensive?
Is there any actual use case for having 'ACTOR_STORAGES_JSON' be set as an empty string? The env variable clearly states it should be a json. So either have json there, or don't set the variable.
Code should not try to guess the intention from an ambiguous action. And here we are guessing that an empty env variable probably means the user did not want to set it up in the first place...
Description
The Apify platform sometimes sets env vars to an empty string instead of leaving them unset (see #303, or this Discord thread). For fields whose type can't parse
'', validation raised and crashedActor.init(). Treat''as absent / fall back to the field default, mirroring the existing_parse_actor_pricing_infobehavior.ACTOR_STORAGES_JSON,APIFY_CHARGED_ACTOR_EVENT_COUNTS):json.loads('')raisedJSONDecodeError. Now treated as absent in_load_storage_keysand thecharged_event_countsvalidator.''failed pydantic validation. Added a shared_default_if_emptyvalidator and applied it tostarted_at,dedicated_cpus,proxy_port,web_server_port,metamorph_after_sleep,is_at_home,test_pay_per_event— and consolidated the existing ad-hoc lambdas (timeout_at,max_paid_dataset_items,max_total_charge_usd,user_is_paying).