Skip to content

pyright in strict mode errors on from dotenv import load_dotenv #431

@eaftan

Description

@eaftan
Contributor

Steps to reproduce:

$ python --version
Python 3.9.13
$ mkdir repro
$ cd repro
$ cat > repro.py <<EOL
from dotenv import load_dotenv
load_dotenv()
EOL
$ echo '{ "typeCheckingMode": "strict" }' > pyrightconfig.json
$ python -m venv .venv
$ source .venv/bin/activate
$ pip install pyright python-dotenv
Collecting pyright
  Using cached pyright-1.1.276-py3-none-any.whl (16 kB)
Collecting python-dotenv
  Using cached python_dotenv-0.21.0-py3-none-any.whl (18 kB)
Collecting nodeenv>=1.6.0
  Using cached nodeenv-1.7.0-py2.py3-none-any.whl (21 kB)
Requirement already satisfied: setuptools in ./.venv/lib/python3.10/site-packages (from nodeenv>=1.6.0->pyright) (58.1.0)
Installing collected packages: python-dotenv, nodeenv, pyright
Successfully installed nodeenv-1.7.0 pyright-1.1.276 python-dotenv-0.21.0
$ pyright repro.py
Loading configuration file at /Users/eaftan/repro/pyrightconfig.json
Assuming Python version 3.9
Assuming Python platform Darwin
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
stubPath /Users/eaftan/repro/typings is not a valid directory.
Searching for source files
Found 1 source file
pyright 1.1.276
/Users/eaftan/repro/repro.py
  /Users/eaftan/repro/repro.py:1:20 - error: Type of "load_dotenv" is partially unknown
    Type of "load_dotenv" is "(dotenv_path: str | PathLike[Unknown] | None = None, stream: IO[str] | None = None, verbose: bool = False, override: bool = False, interpolate: bool = True, encoding: str | None = "utf-8") -> bool" (reportUnknownVariableType)
1 error, 0 warnings, 0 informations 
Completed in 0.579sec

I think the problem is that load_dotenv is typed as follows:

def load_dotenv(
    dotenv_path: Union[str, os.PathLike, None] = None,
    stream: Optional[IO[str]] = None,
    verbose: bool = False,
    override: bool = False,
    interpolate: bool = True,
    encoding: Optional[str] = "utf-8",
) -> bool:

os.PathLike is a generic type and should have a type argument. I think the correct typing for dotenv_path should be Union[str, os.PathLike[str], os.PathLike[bytes], None].

Activity

added a commit that references this issue on Oct 20, 2022
6e2a287
added a commit that references this issue on Jan 11, 2023
09cf4ab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @eaftan

      Issue actions

        pyright in strict mode errors on `from dotenv import load_dotenv` · Issue #431 · theskumar/python-dotenv