Skip to content

Fix type hint for dotenv_path var, add StrPath alias #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/dotenv/main.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,12 @@
from .parser import Binding, parse_stream
from .variables import parse_variables

# A type alias for a string path to be used for the paths in this file.
# These paths may flow to `open()` and `shutil.move()`; `shutil.move()`
# only accepts string paths, not byte paths or file descriptors. See
# https://github.com/python/typeshed/pull/6832.
StrPath = Union[str, 'os.PathLike[str]']

logger = logging.getLogger(__name__)


@@ -28,14 +34,14 @@ def with_warn_for_invalid_lines(mappings: Iterator[Binding]) -> Iterator[Binding
class DotEnv:
def __init__(
self,
dotenv_path: Optional[Union[str, os.PathLike]],
dotenv_path: Optional[StrPath],
stream: Optional[IO[str]] = None,
verbose: bool = False,
encoding: Optional[str] = None,
interpolate: bool = True,
override: bool = True,
) -> None:
self.dotenv_path: Optional[Union[str, os.PathLike]] = dotenv_path
self.dotenv_path: Optional[StrPath] = dotenv_path
self.stream: Optional[IO[str]] = stream
self._dict: Optional[Dict[str, Optional[str]]] = None
self.verbose: bool = verbose
@@ -108,7 +114,7 @@ def get(self, key: str) -> Optional[str]:


def get_key(
dotenv_path: Union[str, os.PathLike],
dotenv_path: StrPath,
key_to_get: str,
encoding: Optional[str] = "utf-8",
) -> Optional[str]:
@@ -122,7 +128,7 @@ def get_key(

@contextmanager
def rewrite(
path: Union[str, os.PathLike],
path: StrPath,
encoding: Optional[str],
) -> Iterator[Tuple[IO[str], IO[str]]]:
if not os.path.isfile(path):
@@ -139,7 +145,7 @@ def rewrite(


def set_key(
dotenv_path: Union[str, os.PathLike],
dotenv_path: StrPath,
key_to_set: str,
value_to_set: str,
quote_mode: str = "always",
@@ -188,7 +194,7 @@ def set_key(


def unset_key(
dotenv_path: Union[str, os.PathLike],
dotenv_path: StrPath,
key_to_unset: str,
quote_mode: str = "always",
encoding: Optional[str] = "utf-8",
@@ -303,7 +309,7 @@ def _is_interactive():


def load_dotenv(
dotenv_path: Union[str, os.PathLike, None] = None,
dotenv_path: Optional[StrPath] = None,
stream: Optional[IO[str]] = None,
verbose: bool = False,
override: bool = False,
@@ -341,7 +347,7 @@ def load_dotenv(


def dotenv_values(
dotenv_path: Union[str, os.PathLike, None] = None,
dotenv_path: Optional[StrPath] = None,
stream: Optional[IO[str]] = None,
verbose: bool = False,
interpolate: bool = True,