@@ -12,17 +12,19 @@ from pydantic_settings import BaseSettings
1212class Settings (BaseSettings ):
1313 database_url: str
1414 api_key: str
15- port: int
15+ port: int = 8080
1616
17- settings = Settings() # Crashes if DATABASE_URL not set
17+ settings = Settings() # Raises ValidationError if DATABASE_URL or API_KEY not set
1818```
1919
20- ``` python
20+ ``` text
2121pydantic_core._pydantic_core.ValidationError: 2 validation errors for Settings
2222database_url
2323 Field required [type=missing, input_value={}, input_type=dict]
24+ For further information visit https://errors.pydantic.dev/2.12/v/missing
2425api_key
2526 Field required [type=missing, input_value={}, input_type=dict]
27+ For further information visit https://errors.pydantic.dev/2.12/v/missing
2628```
2729
2830This error is confusing because:
@@ -31,23 +33,6 @@ This error is confusing because:
3133- The ` input_value={} ` is misleading (it's not a dict, it's your environment)
3234- Hard to quickly see which variables need to be set
3335
34- # ## Type errors
35-
36- If you have fields in your config that don' t have a default value, you get a type error:
37-
38- ```python
39- from pydantic_settings import BaseSettings
40-
41- class Settings (BaseSettings ):
42- database_url: str
43- api_key: str
44- port: int
45-
46- settings = Settings() # expects parameters to be passed in
47- ```
48-
49- This is not very good DX either.
50-
5136## The Solution
5237
5338` pyenvalid ` wraps validation and provides clear, actionable error messages:
@@ -151,4 +136,4 @@ The error type shown is the raw pydantic error type:
151136
152137## License
153138
154- [ MIT] ( LICENSE )
139+ [ MIT] ( https://github.com/truehazker/pyenvalid/blob/main/ LICENSE)
0 commit comments