Skip to content

Commit 1914bac

Browse files
committed
Remove typing guards
Since we now require Python 3.5+, we can assume that the `typing` module is available. The guards can also be removed because importing that module is cheap. This simplifies the code.
1 parent 4617e39 commit 1914bac

File tree

7 files changed

+30
-82
lines changed

7 files changed

+30
-82
lines changed

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
bumpversion
2-
typing; python_version<"3.5"
32
click
43
flake8>=2.2.3
54
ipython

src/dotenv/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from .compat import IS_TYPE_CHECKING
2-
from .main import load_dotenv, get_key, set_key, unset_key, find_dotenv, dotenv_values
1+
from typing import Any, Optional
32

4-
if IS_TYPE_CHECKING:
5-
from typing import Any, Optional
3+
from .main import (dotenv_values, find_dotenv, get_key, load_dotenv, set_key,
4+
unset_key)
65

76

87
def load_ipython_extension(ipython):

src/dotenv/cli.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
from subprocess import Popen
4+
from typing import Any, Dict, List
45

56
try:
67
import click
@@ -9,13 +10,9 @@
910
'Run pip install "python-dotenv[cli]" to fix this.')
1011
sys.exit(1)
1112

12-
from .compat import IS_TYPE_CHECKING
1313
from .main import dotenv_values, get_key, set_key, unset_key
1414
from .version import __version__
1515

16-
if IS_TYPE_CHECKING:
17-
from typing import Any, List, Dict
18-
1916

2017
@click.group()
2118
@click.option('-f', '--file', default=os.path.join(os.getcwd(), '.env'),

src/dotenv/compat.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/dotenv/main.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66
import tempfile
77
from collections import OrderedDict
88
from contextlib import contextmanager
9+
from typing import (IO, Dict, Iterable, Iterator, Mapping, Optional, Text,
10+
Tuple, Union)
911

10-
from .compat import IS_TYPE_CHECKING
1112
from .parser import Binding, parse_stream
1213
from .variables import parse_variables
1314

1415
logger = logging.getLogger(__name__)
1516

16-
if IS_TYPE_CHECKING:
17-
from typing import (IO, Dict, Iterable, Iterator, Mapping, Optional, Text,
18-
Tuple, Union)
19-
if sys.version_info >= (3, 6):
20-
_PathLike = os.PathLike
21-
else:
22-
_PathLike = Text
17+
if sys.version_info >= (3, 6):
18+
_PathLike = os.PathLike
19+
else:
20+
_PathLike = Text
2321

2422

2523
def with_warn_for_invalid_lines(mappings):

src/dotenv/parser.py

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import codecs
22
import re
3-
4-
from .compat import IS_TYPE_CHECKING
5-
6-
if IS_TYPE_CHECKING:
7-
from typing import ( # noqa:F401
8-
IO, Iterator, Match, NamedTuple, Optional, Pattern, Sequence, Text,
9-
Tuple
10-
)
3+
from typing import (IO, Iterator, Match, NamedTuple, Optional, # noqa:F401
4+
Pattern, Sequence, Text, Tuple)
115

126

137
def make_regex(string, extra_flags=0):
@@ -32,47 +26,23 @@ def make_regex(string, extra_flags=0):
3226
_single_quote_escapes = make_regex(r"\\[\\']")
3327

3428

35-
try:
36-
# this is necessary because we only import these from typing
37-
# when we are type checking, and the linter is upset if we
38-
# re-import
39-
import typing
40-
41-
Original = typing.NamedTuple(
42-
"Original",
43-
[
44-
("string", typing.Text),
45-
("line", int),
46-
],
47-
)
48-
49-
Binding = typing.NamedTuple(
50-
"Binding",
51-
[
52-
("key", typing.Optional[typing.Text]),
53-
("value", typing.Optional[typing.Text]),
54-
("original", Original),
55-
("error", bool),
56-
],
57-
)
58-
except (ImportError, AttributeError):
59-
from collections import namedtuple
60-
Original = namedtuple( # type: ignore
61-
"Original",
62-
[
63-
"string",
64-
"line",
65-
],
66-
)
67-
Binding = namedtuple( # type: ignore
68-
"Binding",
69-
[
70-
"key",
71-
"value",
72-
"original",
73-
"error",
74-
],
75-
)
29+
Original = NamedTuple(
30+
"Original",
31+
[
32+
("string", Text),
33+
("line", int),
34+
],
35+
)
36+
37+
Binding = NamedTuple(
38+
"Binding",
39+
[
40+
("key", Optional[Text]),
41+
("value", Optional[Text]),
42+
("original", Original),
43+
("error", bool),
44+
],
45+
)
7646

7747

7848
class Position:

src/dotenv/variables.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import re
22
from abc import ABCMeta
3-
4-
from .compat import IS_TYPE_CHECKING
5-
6-
if IS_TYPE_CHECKING:
7-
from typing import Iterator, Mapping, Optional, Pattern, Text
8-
3+
from typing import Iterator, Mapping, Optional, Pattern, Text
94

105
_posix_variable = re.compile(
116
r"""

0 commit comments

Comments
 (0)