DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
1.23k stars 26 forks source link

Errors from types in site-packages are misattributed to non-existent files in stubPath #786

Open benblank opened 1 month ago

benblank commented 1 month ago

I wanted to extend the TypeShed types for the defusedxml package, as they seem to be incomplete. However, types for the same package being present in both site-packages and stubPath seems to confuse basedpyright such that any errors present in the site-packages type stubs are reported as though the files were located under stubPath, even when no such files exist.

Steps to reproduce

  1. python -m venv .venv
  2. . .venv/bin/activate
  3. pip install basedpyright types-defusedxml
  4. mkdir typings/defusedxml
  5. touch typings/defusedxml/__init__.py
  6. basedpyright
  7. Holy errors, Batman!

Renaming typings/defusedxml to typings/defusedxml-foo prevents the errors, as does renaming typings or changing the stubPath setting. Changing the include setting to omit typings works, but omitting .venv doesn't. Similarly, changing exclude to contain typings works, but including .venv doesn't.

I didn't see a way to use PyPI packages with https://basedpyright.com or https://pyright-play.net, so I don't have a playground example of the problem.

Terminal output

$ python -m venv .venv
$ source .venv/bin/activate
(.venv) $ pip install basedpyright types-defusedxml
Collecting basedpyright
  Using cached basedpyright-1.18.4-py3-none-any.whl.metadata (2.1 kB)
Collecting types-defusedxml
  Using cached types_defusedxml-0.7.0.20240218-py3-none-any.whl.metadata (1.8 kB)
Collecting nodejs-wheel-binaries>=20.13.1 (from basedpyright)
  Using cached nodejs_wheel_binaries-20.18.0-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (5.2 kB)
Using cached basedpyright-1.18.4-py3-none-any.whl (11.2 MB)
Using cached types_defusedxml-0.7.0.20240218-py3-none-any.whl (7.8 kB)
Using cached nodejs_wheel_binaries-20.18.0-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.8 MB)
Installing collected packages: types-defusedxml, nodejs-wheel-binaries, basedpyright
Successfully installed basedpyright-1.18.4 nodejs-wheel-binaries-20.18.0 types-defusedxml-0.7.0.20240218
(.venv) $ mkdir -p typings/defusedxml
(.venv) $ touch typings/defusedxml/__init__.py
(.venv) $ basedpyright
/home/five35/Documents/tmp/based-phantom-repro/typings/defusedxml/cElementTree.pyi
  /home/five35/Documents/tmp/based-phantom-repro/typings/defusedxml/cElementTree.pyi:2:5 - error: "XML" is unknown import symbol (reportAttributeAccessIssue)
  /home/five35/Documents/tmp/based-phantom-repro/typings/defusedxml/cElementTree.pyi:2:12 - error: Type of "XML" is unknown (reportUnknownVariableType)
  /home/five35/Documents/tmp/based-phantom-repro/typings/defusedxml/cElementTree.pyi:3:5 - error: "ParseError" is unknown import symbol (reportAttributeAccessIssue)

        . . . many errors . . .

  /home/five35/Documents/tmp/based-phantom-repro/typings/defusedxml/xmlrpc.pyi:22:66 - error: Type annotation is missing for parameter "sysid" (reportMissingParameterType)
  /home/five35/Documents/tmp/based-phantom-repro/typings/defusedxml/xmlrpc.pyi:22:73 - error: Type of parameter "pubid" is unknown (reportUnknownParameterType)
  /home/five35/Documents/tmp/based-phantom-repro/typings/defusedxml/xmlrpc.pyi:22:73 - error: Type annotation is missing for parameter "pubid" (reportMissingParameterType)
181 errors, 0 warnings, 0 notes
(.venv) $ ls /home/five35/Documents/tmp/based-phantom-repro/typings/defusedxml/xmlrpc.pyi
ls: cannot access '/home/five35/Documents/tmp/based-phantom-repro/typings/defusedxml/xmlrpc.pyi': No such file or directory
DetachHead commented 1 month ago

interesting. i dont think pyright supports partially extending stubs like this, but yeah definitely shouldn't be reporting errors with non-existent file paths

benblank commented 1 month ago

To be fair, I'm not sure anything supports partial stubs like this; I just forgot the step where I should have uninstalled the site-packages types before writing my own. 😅

I wouldn't have created the issue at all, but reporting errors in files which actually don't exist seemed confusing enough that it probably shouldn't happen even when I'm doing something silly like this.