Fix type hint for dotenv_path var, add StrPath alias by eaftan · Pull Request #432 · theskumar/python-dotenv

@eaftan

nandgator


def load_dotenv(
dotenv_path: Union[str, os.PathLike, None] = None,
dotenv_path: Union[str, 'os.PathLike[str]', 'os.PathLike[bytes]', None] = None,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this better: Union[str, os.PathLike[Union[str, bytes]], None]?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather use os.PathLike[str], os.PathLike[bytes], as it is done in typeshed. We could even define a TypeAlias here and use Optional instead of Union[..., None].

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dotenv_path: str | os.PathLike[str | bytes] | None = None

image

is compact and nice, but currently supported from Python 3.10 and above. So yes typing.Optional would also be fine.

from typing import Optional, Union
...
dotenv_path: Optional[Union[str, os.PathLike[Union[str, bytes]]] = None

@KalleDK

Anything needed to make this pr happen? :) Came here for this exact problem :)

@eaftan

These paths can flow into `shutil.move`, which does not accept byte
paths or (int) file descriptors.  See python/typeshed#6832

@Viicos

@eaftan I think a type alias could be used, as the dotenv_path argument is also present in other places in the code, and this PR could also fix the type hint for these ones

And use it consistently in main.py.

@eaftan

@eaftan

@eaftan I think a type alias could be used, as the dotenv_path argument is also present in other places in the code, and this PR could also fix the type hint for these ones

Done. The CI seems to be failing with a coverage error, but I think that's not my fault -- it seems to have failed on the most recent commit to main as well: https://github.com/github/entitlements/pull/56799

@theskumar theskumar changed the title Fix type hint for load_dotenv Fix type hint for dotenv_path var, add StrPath alias

Jan 11, 2023

@theskumar