Pyright Will Use non Stubs Package Import if `py.typed` is Present
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
**Describe the bug**
We are trying to use Pyright in a pretty non-standard way, where we can disable certain pyi files via an environment variable. We are implementing this via a conditional `.pth` file.
**Reproduction Setup**
For this example, I am using python 3.9 but any version should work
`/usr/lib/python3.9/site-packages/Foo/__init__.py`:
```
class Bar:
a = 5
```
`/usr/lib/python3.9/site-packages/Foo/py.typed` (File present)
`/usr/lib/python3.9/site-packages/lsp_redirect.pth`:
```
import sys, os;not os.environ.get("LSP_ONLY_DISABLE") and sys.path.insert(0, "/usr/lib/python3.9/site-packages/lsp")
```
`/usr/lib/python3.9/site-packages/Foo-stubs/__init__.pyi`
```python
class Bar:
a = 5
b = 4
```
`example.py`
```python
from Foo import Bar
print( Bar.b )
```
**Output**
```
➜ mypy example.py
Success: no issues found in 1 source file
➜ pyright example.py
/src/TtBase/example.py
/src/TtBase/example.py:4:12 - error: Cannot access attribute "b" for class "type[Bar]"
Attribute "b" is unknown (reportAttributeAccessIssue)
```
Currently, you can work around this by adding a py.typed file to `/usr/lib/python3.9/site-packages/Foo-stubs`, but this is confusing, and I only figured this out by reading the ImportResolver source code.
Contributor guide
Research direction
Reproduce the behavior with the Foo, Foo-stubs, lsp_redirect.pth, and example.py files shown in the issue, comparing Pyright with mypy. Start by reading Pyright's ImportResolver, which the report identifies as relevant. Done means the reproduced import scenario is handled consistently without requiring an extra py.typed file in Foo-stubs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100