Skip to content
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

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

Closed
eaftan opened this issue Oct 20, 2022 · 0 comments · Fixed by #432
Closed

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

eaftan opened this issue Oct 20, 2022 · 0 comments · Fixed by #432

Comments

@eaftan
Copy link
Contributor

eaftan commented Oct 20, 2022

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].

eaftan added a commit to eaftan/python-dotenv that referenced this issue Oct 20, 2022
theskumar pushed a commit that referenced this issue Jan 11, 2023
* Fix type hint for load_dotenv

Fixes #431

* Quote type hints to avoid runtime errors in earlier Python versions

* Revise type of dotenv_path parameter

Based on PR feedback and typeshed's type hint for the built-in open()
function:
https://github.com/python/typeshed/blob/e2d67bf7034f68c07bd35150247e58e0817725d9/stdlib/builtins.pyi#L1421

* Allow only string paths, not byte paths

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

* Create a type alias for the paths this library accepts

And use it consistently in main.py.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant