nullcontext: untyped or Any enter_result type is ignored and inferred as None on __enter__
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
**Describe the bug**
The `nullcontext` context manager from stdlib lets to pass arbitrary value to be returned on `__enter__`. It's named `enter_result`.
If the value passed is untyped or `Any`, pyright infers return type of `__enter__` as `None`. But should preserve untyped/`Any` type.
**Code or Screenshots**
```python
from contextlib import nullcontext
from typing import Any, revela_type
# also with: def test(items) -> None:
def test(items: Any) -> None:
with nullcontext(items) as _items:
reveal_type(_items)
for item in _items:
print(item)
if __name__ == '__main__':
test([1, 2, 3])
```
**Expected behavior**
Revealed type is Any, no errors
**Observed behavior**
```sh
$ pyright ./test.py
test.py
test.py:9:21 - information: Type of "_items" is "None"
test.py:10:21 - error: "None" is not iterable
"__iter__" method not defined (reportGeneralTypeIssues)
1 error, 0 warnings, 1 information
```
**VS Code extension or command-line**
CLI pyright 1.1.408
Contributor guide
Research direction
Start by reproducing the report with the shown test.py snippet and the CLI command `pyright ./test.py`. Trace how `contextlib.nullcontext` is typed when its `enter_result` argument is untyped or Any, then verify that `_items` is revealed as Any and the iteration produces no error.
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
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100