|
15 | 15 |
|
16 | 16 | from pydantic import BaseSettings, validator |
17 | 17 | from pydantic.fields import ModelField, Undefined |
| 18 | +from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict |
18 | 19 |
|
19 | 20 |
|
20 | 21 | def assert_field_specs( |
@@ -50,9 +51,8 @@ class Settings(BaseSettings): |
50 | 51 | @classmethod |
51 | 52 | def parse_none(cls, v, values, field: ModelField): |
52 | 53 | # WARNING: In nullable fields, envs equal to null or none are parsed as None !! |
53 | | - if field.allow_none: |
54 | | - if isinstance(v, str) and v.lower() in ("null", "none"): |
55 | | - return None |
| 54 | + if field.allow_none and isinstance(v, str) and v.lower() in ("null", "none"): |
| 55 | + return None |
56 | 56 | return v |
57 | 57 |
|
58 | 58 |
|
@@ -132,15 +132,21 @@ def test_fields_declarations(): |
132 | 132 | def test_construct(monkeypatch): |
133 | 133 | # from __init__ |
134 | 134 | settings_from_init = Settings( |
135 | | - VALUE=1, VALUE_ALSO_REQUIRED=10, VALUE_NULLABLE_REQUIRED=None |
| 135 | + VALUE=1, |
| 136 | + VALUE_ALSO_REQUIRED=10, |
| 137 | + VALUE_NULLABLE_REQUIRED=None, |
136 | 138 | ) |
| 139 | + |
137 | 140 | print(settings_from_init.json(exclude_unset=True, indent=1)) |
138 | 141 |
|
139 | 142 | # from env vars |
140 | | - monkeypatch.setenv("VALUE", "1") |
141 | | - monkeypatch.setenv("VALUE_ALSO_REQUIRED", "10") |
142 | | - monkeypatch.setenv( |
143 | | - "VALUE_NULLABLE_REQUIRED", "null" |
| 143 | + setenvs_from_dict( |
| 144 | + monkeypatch, |
| 145 | + { |
| 146 | + "VALUE": "1", |
| 147 | + "VALUE_ALSO_REQUIRED": "10", |
| 148 | + "VALUE_NULLABLE_REQUIRED": "null", |
| 149 | + }, |
144 | 150 | ) # WARNING: set this env to None would not work w/o ``parse_none`` validator! bug??? |
145 | 151 |
|
146 | 152 | settings_from_env = Settings() |
|
0 commit comments