Skip to content

Conversation

Gokujo
Copy link

@Gokujo Gokujo commented Nov 15, 2024

If you use empty values in your env files, e.g. DB_PORT= and cast it in call into int or float, so you will get an error:

ValueError: invalid literal for int() with base 10: ''

I just fixed that for me with this universal solution. It works not only on int or float

If you use empty values in your env files, e.g. `DB_PORT=` and cast it in call into int or float, so you will get an error:
```
ValueError: invalid literal for int() with base 10: ''
```

I just fixed that for me with this universal solution. It works not only on int or float
Copy link
Member

@lucasrcezimbra lucasrcezimbra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add a test case to avoid regression.

Maxim Harder and others added 3 commits August 27, 2025 09:43
Fix boolean conversions and empty-value handling across Decouple casting
logic to avoid treating empty strings as truthy or as missing in unexpected
ways.

- Return False for empty string in _cast_boolean instead of using bool('')
  which is always False only accidentally; explicitly map '' -> False.
- When value is missing/empty and default is Undefined, if cast is the
  boolean caster return False directly (preserve previous intent and avoid
  falling through to unintended casts).
- Treat empty string the same as None in Transform.__call__ so that empty
  values trigger post_process() rather than being stripped and cast, which
  could lead to incorrect results.

These changes prevent empty strings from being misinterpreted by boolean
casts and ensure consistent handling of empty config values.
Add comprehensive unit tests verifying how empty values in env and
ini repositories (and AutoConfig) are treated with defaults and casts.
Key behavior validated:
- Empty numeric values use provided numeric defaults when cast=int.
- Empty values return None when default=None is specified.
- Bool casts: empty value with default True returns True; without
  default, casting the empty value yields False.
- CSV cast returns an empty list for an empty value and honors a
  provided default list.
- Ensure RepositoryIni/RepositoryEnv and AutoConfig behavior is
  consistent.
Also adjust existing tests to assert None is returned when default=None
instead of expecting an empty string, aligning tests with intended
semantics.

Add fixtures and patched file IO (StringIO) to simulate .env/.ini files
and avoid touching the filesystem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants