NotImplementedType is handled strangely
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
**Describe the bug**
Pyright seems to handle `NotImplementedType` as a special case, and type narrowing and method resolving does not work correctly.
**Code or Screenshots**
```python
from types import EllipsisType, NotImplementedType
def check1(val: int | str | NotImplementedType):
if isinstance(val, int):
print("Integer", val)
reveal_type(val) # pyright says: int | NotImplementedType
print("Does not type check (which is good)", val + 2) # But the report is incomplete
def check2(val: int | str | float):
if isinstance(val, int):
print("Integer", val)
reveal_type(val) # pyright says: int
def check3(val: int | str | EllipsisType):
if isinstance(val, int):
print("Integer", val)
reveal_type(val) # pyright says: int
def check4(val: int | NotImplementedType):
if isinstance(val, int):
print("Integer", val)
reveal_type(val) # pyright says: int | NotImplementedType
print("Type checks (but should not)", val + 2)
```
**VS Code extension or command-line**
I ran the command line version of Pyright, version 1.5.0, with: `npx pyright@latest not_implemented_test.py`
The output is:
```
/tmp/not_implemented_test.py
/tmp/not_implemented_test.py:7:21 - information: Type of "val" is "int | NotImplementedType"
/tmp/not_implemented_test.py:8:22 - error: Operator "+" not supported for types "int | NotImplementedType | str" and "Literal[2]"
Operator "+" not supported for types "str" and "Literal[2]" when expected type is "object" (reportOperatorIssue)
/tmp/not_implemented_test.py:14:21 - information: Type of "val" is "int"
/tmp/not_implemented_test.py:20:21 - information: Type of "val" is "int"
/tmp/not_implemented_test.py:26:21 - information: Type of "val" is "int | NotImplementedType"
1 error, 0 warnings, 4 informations
```
Contributor guide
Research direction
Start by reproducing the behavior in not_implemented_test.py with the provided npx pyright command. Trace how NotImplementedType is handled during isinstance narrowing and operator checking, comparing the results with the float and EllipsisType cases. Done means check1 and check4 narrow to int and report the invalid addition, with regression coverage for the examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100