Currently, py-dotenv uses a strategy of overwriting with the latter value when handling configuration items with the same key, that is, using the last value of the repeated key. However, I just wasted an entire morning because there were three configuration items with the same key in my .env file. 😢
To avoid others encountering similar issues, I propose adding an optional behavior parameter. The default should be warning, meaning the latter value overwrites the former but with a warning. It could also be set to strict, which throws an exception, or to latter-covers, which means the latter value overwrites the former without any warning.
I have researched how other libraries handle this issue, for reference:
| Parser |
Duplicate Handling |
Notes |
| Node.js dotenv |
Latter covers |
Most common mode |
| Python dotenv |
Latter covers |
Configurable to override system variables |
| PHP phpdotenv |
Default error |
Can be set to allow overrides |
| Ruby dotenv |
Latter covers |
Issues a warning |
| Go godotenv |
Latter covers |
No warning |
| Docker Compose |
Latter covers |
Follows file loading order |
Append: maybe a Callable[[list[str]], str] like sum is also acceptable, for #374 suggests a customed merge logic.