Skip to content

Commit d6d43a1

Browse files
author
Saurabh Kumar
committed
Refactor move override variable as class variable
1 parent 8e37a5f commit d6d43a1

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

src/dotenv/main.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import shutil
88
import sys
99
import tempfile
10-
from typing import (Dict, Iterator, List, Match, Optional, # noqa
10+
from typing import (Any, Dict, Iterator, List, Match, Optional, # noqa
1111
Pattern, Union, TYPE_CHECKING, Text, IO, Tuple)
1212
import warnings
1313
from collections import OrderedDict
@@ -32,12 +32,13 @@
3232

3333
class DotEnv():
3434

35-
def __init__(self, dotenv_path, verbose=False, encoding=None):
36-
# type: (Union[Text, _PathLike, _StringIO], bool, Union[None, Text]) -> None
35+
def __init__(self, dotenv_path, verbose=False, encoding=None, override=False):
36+
# type: (Union[Text, _PathLike, _StringIO], bool, Union[None, Text], bool) -> None
3737
self.dotenv_path = dotenv_path # type: Union[Text,_PathLike, _StringIO]
3838
self._dict = None # type: Optional[Dict[Text, Text]]
3939
self.verbose = verbose # type: bool
4040
self.encoding = encoding # type: Union[None, Text]
41+
self.override = override # type: bool
4142

4243
@contextmanager
4344
def _get_stream(self):
@@ -69,18 +70,16 @@ def parse(self):
6970
if mapping.key is not None and mapping.value is not None:
7071
yield mapping.key, mapping.value
7172

72-
def set_as_environment_variables(self, override=False):
73-
# type: (bool) -> bool
73+
def set_as_environment_variables(self):
74+
# type: () -> None
7475
"""
7576
Load the current dotenv as system environemt variable.
7677
"""
7778
for k, v in self.dict().items():
78-
if k in os.environ and not override:
79+
if k in os.environ and not self.override:
7980
continue
8081
os.environ[to_env(k)] = to_env(v)
8182

82-
return True
83-
8483
def get(self, key):
8584
# type: (Text) -> Optional[Text]
8685
"""
@@ -265,13 +264,13 @@ def find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False):
265264
return ''
266265

267266

268-
def load_dotenv(dotenv_path=None, stream=None, verbose=False, override=False, **kwargs):
269-
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, bool, Union[None, Text]) -> bool
267+
def load_dotenv(dotenv_path=None, stream=None, **kwargs):
268+
# type: (Union[Text, _PathLike, None], Optional[_StringIO], **Any) -> None
270269
f = dotenv_path or stream or find_dotenv()
271-
return DotEnv(f, verbose=verbose, **kwargs).set_as_environment_variables(override=override)
270+
return DotEnv(f, **kwargs).set_as_environment_variables()
272271

273272

274-
def dotenv_values(dotenv_path=None, stream=None, verbose=False, **kwargs):
275-
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, Union[None, Text]) -> Dict[Text, Text]
273+
def dotenv_values(dotenv_path=None, stream=None, **kwargs):
274+
# type: (Union[Text, _PathLike, None], Optional[_StringIO], **Any) -> Dict[Text, Text]
276275
f = dotenv_path or stream or find_dotenv()
277-
return DotEnv(f, verbose=verbose, **kwargs).dict()
276+
return DotEnv(f, **kwargs).dict()

tests/test_core.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def test_load_dotenv(tmp_path):
7979
sh.touch(dotenv_path)
8080
set_key(dotenv_path, 'DOTENV', 'WORKS')
8181
assert 'DOTENV' not in os.environ
82-
success = load_dotenv(dotenv_path)
83-
assert success
82+
load_dotenv(dotenv_path)
8483
assert 'DOTENV' in os.environ
8584
assert os.environ['DOTENV'] == 'WORKS'
8685

@@ -92,8 +91,7 @@ def test_load_dotenv_override(tmp_path):
9291
sh.touch(dotenv_path)
9392
os.environ[key_name] = "OVERRIDE"
9493
set_key(dotenv_path, key_name, 'WORKS')
95-
success = load_dotenv(dotenv_path, override=True)
96-
assert success
94+
load_dotenv(dotenv_path, override=True)
9795
assert key_name in os.environ
9896
assert os.environ[key_name] == 'WORKS'
9997

0 commit comments

Comments
 (0)