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

reportMissingTypeStubs error when src directory isn't in extraPaths #4771

Closed
adamhl8 opened this issue Mar 14, 2023 · 5 comments
Closed

reportMissingTypeStubs error when src directory isn't in extraPaths #4771

adamhl8 opened this issue Mar 14, 2023 · 5 comments
Labels
as designed Not a bug, working as intended

Comments

@adamhl8
Copy link

adamhl8 commented Mar 14, 2023

I have a directory structure like so:

project_root/
└── src/
    ├── module_a.py
    └── module_b.py

When I run pyright src from project_root, I get reportMissingTypeStubs errors for my modules. I read over this discussion and in that case the solution was to add the directory to extraPaths so it could be resolved. But reading over the import resolution docs it seems like my src folder should be resolved automatically: If no execution environment or extraPaths are configured, try to resolve using the local directory src

Setting extraPaths = ["src"] solves the issue, but I don't really understand why (seems like it might be a bug which is why I'm opening this issue).

Separately, if it's intended that I have to set extraPaths like that, would it make sense for directories in include to be effectively appended to extraPaths under the hood? (or in other words directories in include would be part of import resolution)
For example: include = ["some_dir"] would automatically consider some_dir during import resolution.
Likewise, running pyright some_dir would do the same.
I don't understand enough about how pyright works to know if that would cause issues though.

@erictraut
Copy link
Collaborator

erictraut commented Mar 14, 2023

I'm not ale to repro the problem as reported. I've created the directory hierarchy and files as you've indicated. When I run pyright src from project_root, I get the following output:

No configuration file found.
No pyproject.toml file found.
stubPath /<redacted>/project_root/typings is not a valid directory.
Assuming Python platform Darwin
Searching for source files
Found 2 source files
pyright 1.1.298
0 errors, 0 warnings, 0 informations 
Completed in 0.39sec

Do you happen to have a pyrightconfig.json file in some higher-level directory? Pyright will scan up the directory hierarchy looking for a config file. If it finds one, it assumes that is the project root.

You said that you're seeing an error reportMissingTypeStubs, but this is emitted only in cases where there is an import statement. You haven't mentioned any import statements in your repro steps, so I'm not sure what to make of that.

Also, if your project root is src, why are you running pyright src? You should simply run pyright, and it will find all of the source files under the project root.

@erictraut erictraut added the question Further information is requested label Mar 14, 2023
@adamhl8
Copy link
Author

adamhl8 commented Mar 15, 2023

After some more testing I was able to narrow down the conditions that are causing this for me. Here's the setup that allows me to reproduce the issue (no higher level pyrightconfig anywhere):

test/
├── src/
│   ├── module_a.py
│   └── module_b.py
└── pyproject.toml

module_a.py:

from module_b import foo
foo()

module_b.py:

def foo():
    pass

pyproject.toml:

[tool.pyright]
typeCheckingMode = "strict"

The issue occurs when the src directory is on my python search path:
export PYTHONPATH=/home/adam/dev/test/src

When it is, I see a reportMissingTypeStubs error. Obviously the solution is to remove that from PYTHONPATH, but in my case it's automatically added (among other things) via PDM (which I understand you all don't officially support).


Output of pyright --verbose src:

No configuration file found.
pyproject.toml file found at /home/adam/dev/test.
Loading pyproject.toml file at /home/adam/dev/test/pyproject.toml
Assuming Python version 3.10
Assuming Python platform Linux
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
stubPath /home/adam/dev/test/typings is not a valid directory.
Search paths for /home/adam/dev/test
  /home/adam/.local/share/pnpm/global/5/.pnpm/pyright@1.1.299/node_modules/pyright/dist/typeshed-fallback/stdlib
  /home/adam/dev/test
  /home/adam/dev/test/typings
  /home/adam/.local/share/pnpm/global/5/.pnpm/pyright@1.1.299/node_modules/pyright/dist/typeshed-fallback/stubs/...
  /home/adam/dev/test/src
  /usr/lib/python3.10
  /usr/lib/python3.10/lib-dynload
  /home/adam/.local/lib/python3.10/site-packages
  /usr/local/lib/python3.10/dist-packages
  /usr/lib/python3/dist-packages
Searching for source files
Found 2 source files
pyright 1.1.299
/home/adam/dev/test/src/module_a.py
  /home/adam/dev/test/src/module_a.py:1:6 - error: Stub file not found for "module_b" (reportMissingTypeStubs)
1 error, 0 warnings, 0 informations 

@erictraut
Copy link
Collaborator

Any paths included sys.paths are assumed to be resolved as external libraries even if they happen to be under your project path. That's by design, as it allows for editable installs of libraries.

If you don't want your src directory to be considered a library that is external to your project, you should not include it in your PYTHONPATH.

@erictraut erictraut added as designed Not a bug, working as intended and removed question Further information is requested labels Mar 15, 2023
@yozachar
Copy link

yozachar commented Aug 1, 2023

Hello, I've the same problem, but here's my PYTHONPATH:

> (.venv) |> python -c "import sys; print('\n'.join(sys.path))"
C:\Users\us-er\AppData\Local\Programs\Python\Python38\python38.zip
C:\Users\us-er\AppData\Local\Programs\Python\Python38\DLLs
C:\Users\us-er\AppData\Local\Programs\Python\Python38\lib
C:\Users\us-er\AppData\Local\Programs\Python\Python38
C:\Users\us-er\Projects\pyvalidators\.venv
C:\Users\us-er\Projects\pyvalidators\.venv\lib\site-packages

my project structure:

> (.venv) |> tree .
pyvalidators/
├── .venv/
├── src/
│   ├── validators/    
│       ├── __init__.py
│       ├── py.typed
│       ├── module_a.py
│       ├── module_b.py
│       ├── module_c.py
│       ├── .
│       ├── .
│       ├── .
│       └── module_z.py
├── tests/
│       └── test_module_a.py
│       ├── test_module_b.py
│       ├── test_module_c.py
│       ├── .
│       ├── .
│       ├── .
│       └── test_module_z.py
└── pyproject.toml

and pyright configuration in pyproject.toml.

[tool.pyright]
exclude = ["**/__pycache__", ".pytest_cache/", ".tox/", ".venv/", "site/"]
pythonVersion = "3.8"
pythonPlatform = "All"
typeCheckingMode = "strict"

pyright . throws a couple of errors (like reportGeneralTypeIssues, reportUnknownVariableType, reportMissingImport) along with reportMissingTypeStubs unless I add extraPaths = ["src"] to pyproject.toml.

As mentioned in the docs:


"If no execution environment or extraPaths are configured, try to resolve using the local directory src. It is common for Python projects to place local source files within a directory of this name."

Aren't sub directories of src/ considered?


Edit I think my problem is very much like: https://stackoverflow.com/questions/50155464/using-pytest-with-a-src-layer

@erictraut
Copy link
Collaborator

@joe733, I'm guessing that you're using the command-line version of pyright, as opposed to the language server. The language server supports a setting called "python.analysis.autoSearchPaths" which defaults to true. When this setting is enabled, it causes pyright to automatically add "src" as an extraPath.

The problem is that the command-line (CLI) version of pyright doesn't support language server settings, so this internal setting wasn't enabled. I think it's reasonable to enable this by default for the CLI. The only downside is that there may be cases where users of the pyright CLI don't want "src" to be treated implicitly as an extraPath, but I think this will be rare. I've made this change, and it will be included in the next release of pyright.

erictraut pushed a commit that referenced this issue Aug 1, 2023
…nable "auto search paths", so it didn't automatically add "src" as an extra path. This addresses #4771.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

3 participants