1010from collections import OrderedDict
1111from contextlib import contextmanager
1212
13- from .compat import IS_TYPE_CHECKING , PY2 , StringIO , to_env
13+ from .compat import IS_TYPE_CHECKING
1414from .parser import Binding , parse_stream
1515from .variables import parse_variables
1616
2424 else :
2525 _PathLike = Text
2626
27- if sys .version_info >= (3 , 0 ):
28- _StringIO = StringIO
29- else :
30- _StringIO = StringIO [Text ]
31-
3227
3328def with_warn_for_invalid_lines (mappings ):
3429 # type: (Iterator[Binding]) -> Iterator[Binding]
@@ -44,8 +39,8 @@ def with_warn_for_invalid_lines(mappings):
4439class DotEnv ():
4540
4641 def __init__ (self , dotenv_path , verbose = False , encoding = None , interpolate = True , override = True ):
47- # type: (Union[Text, _PathLike, _StringIO ], bool, Union[None, Text], bool, bool) -> None
48- self .dotenv_path = dotenv_path # type: Union[Text,_PathLike, _StringIO ]
42+ # type: (Union[Text, _PathLike, io.StringIO ], bool, Union[None, Text], bool, bool) -> None
43+ self .dotenv_path = dotenv_path # type: Union[Text,_PathLike, io.StringIO ]
4944 self ._dict = None # type: Optional[Dict[Text, Optional[Text]]]
5045 self .verbose = verbose # type: bool
5146 self .encoding = encoding # type: Union[None, Text]
@@ -55,15 +50,15 @@ def __init__(self, dotenv_path, verbose=False, encoding=None, interpolate=True,
5550 @contextmanager
5651 def _get_stream (self ):
5752 # type: () -> Iterator[IO[Text]]
58- if isinstance (self .dotenv_path , StringIO ):
53+ if isinstance (self .dotenv_path , io . StringIO ):
5954 yield self .dotenv_path
6055 elif os .path .isfile (self .dotenv_path ):
6156 with io .open (self .dotenv_path , encoding = self .encoding ) as stream :
6257 yield stream
6358 else :
6459 if self .verbose :
6560 logger .info ("Python-dotenv could not find configuration file %s." , self .dotenv_path or '.env' )
66- yield StringIO ('' )
61+ yield io . StringIO ('' )
6762
6863 def dict (self ):
6964 # type: () -> Dict[Text, Optional[Text]]
@@ -96,7 +91,7 @@ def set_as_environment_variables(self):
9691 if k in os .environ and not self .override :
9792 continue
9893 if v is not None :
99- os .environ [to_env ( k ) ] = to_env ( v )
94+ os .environ [k ] = v
10095
10196 return True
10297
@@ -271,13 +266,7 @@ def _is_interactive():
271266 else :
272267 # will work for .py files
273268 frame = sys ._getframe ()
274- # find first frame that is outside of this file
275- if PY2 and not __file__ .endswith ('.py' ):
276- # in Python2 __file__ extension could be .pyc or .pyo (this doesn't account
277- # for edge case of Python compiled for non-standard extension)
278- current_file = __file__ .rsplit ('.' , 1 )[0 ] + '.py'
279- else :
280- current_file = __file__
269+ current_file = __file__
281270
282271 while frame .f_code .co_filename == current_file :
283272 assert frame .f_back is not None
@@ -304,7 +293,7 @@ def load_dotenv(
304293 interpolate = True ,
305294 encoding = "utf-8" ,
306295):
307- # type: (Union[Text, _PathLike, None], Optional[_StringIO ], bool, bool, bool, Optional[Text]) -> bool # noqa
296+ # type: (Union[Text, _PathLike, None], Optional[io.StringIO ], bool, bool, bool, Optional[Text]) -> bool # noqa
308297 """Parse a .env file and then load all the variables found as environment variables.
309298
310299 - *dotenv_path*: absolute or relative path to .env file.
@@ -335,7 +324,7 @@ def dotenv_values(
335324 interpolate = True ,
336325 encoding = "utf-8" ,
337326):
338- # type: (Union[Text, _PathLike, None], Optional[_StringIO ], bool, bool, Optional[Text]) -> Dict[Text, Optional[Text]] # noqa: E501
327+ # type: (Union[Text, _PathLike, None], Optional[io.StringIO ], bool, bool, Optional[Text]) -> Dict[Text, Optional[Text]] # noqa: E501
339328 """
340329 Parse a .env file and return its content as a dict.
341330
0 commit comments