From 490b116e6548e8d1bbc8184e36768f6ae73c06b2 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Sat, 3 Sep 2022 20:38:05 +0530 Subject: [PATCH] Revert "fix: out of scope error when "dest" variable is undefined #413" This reverts commit a53d652f0618c27a0fd62c4a7946e609919179f3. --- src/dotenv/main.py | 9 ++++----- tests/test_main.py | 5 ----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/dotenv/main.py b/src/dotenv/main.py index 33217885..05d377a9 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -125,16 +125,15 @@ def rewrite( path: Union[str, os.PathLike], encoding: Optional[str], ) -> Iterator[Tuple[IO[str], IO[str]]]: - dest = None try: if not os.path.isfile(path): with open(path, "w+", encoding=encoding) as source: source.write("") - dest = tempfile.NamedTemporaryFile(mode="w+", delete=False, encoding=encoding) - with open(path, encoding=encoding) as source: - yield (source, dest) # type: ignore + with tempfile.NamedTemporaryFile(mode="w+", delete=False, encoding=encoding) as dest: + with open(path, encoding=encoding) as source: + yield (source, dest) # type: ignore except BaseException: - if dest and os.path.isfile(dest.name): + if os.path.isfile(dest.name): os.unlink(dest.name) raise else: diff --git a/tests/test_main.py b/tests/test_main.py index 5f579ccd..82c73ba1 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -22,11 +22,6 @@ def test_set_key_no_file(tmp_path): assert os.path.exists(nx_file) -def test_set_key_invalid_file(): - with pytest.raises(TypeError): - dotenv.set_key(None, "foo", "bar") - - @pytest.mark.parametrize( "before,key,value,expected,after", [