Skip to content

Commit

Permalink
Return None if return_path=True and .env not found; improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria committed Nov 20, 2024
1 parent 214ef11 commit bae777f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/environs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,15 @@ def read_env(
verbose: _BoolType = False,
override: _BoolType = False,
return_path: _BoolType = False,
) -> typing.Union[_BoolType, _StrType]:
) -> typing.Union[_BoolType, typing.Optional[_StrType]]:
"""Read a .env file into os.environ.
If .env is not found in the directory from which this method is called,
the default behavior is to recurse up the directory tree until a .env
file is found. If you do not wish to recurse up the tree, you may pass
False as a second positional argument.
"""

env_path = ""
env_path = None
is_env_loaded = False
if path is None:
# By default, start search from the same directory this function is called
Expand Down
7 changes: 2 additions & 5 deletions tests/test_environs.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,17 +426,14 @@ def test_read_env_return_path_if_env_not_found(self, env, tmp_path):
# Move .env file to temp location
env_path = HERE / ".env"
temp_env = tmp_path / ".env"
env_path.rename(temp_env)

try:
env_path.rename(temp_env)
path = env.read_env(return_path=True)
assert path is None
finally:
# Restore .env file
if temp_env.exists():
temp_env.rename(env_path)
assert path == ""

assert path == ""


def always_fail(value):
Expand Down

0 comments on commit bae777f

Please sign in to comment.