microsoft / microsoft/pyright

"possibly unbound" error when redefining builtin in try-except

Open
#10,457 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**Describe the bug**

After redefining a builtin with a try-except, using the builtin in a function results in a "possibly unbound" error.

Maybe similar to https://github.com/microsoft/pyright/issues/4074?

**Code or Screenshots**

```python
try:
from rich import print
except ModuleNotFoundError:
pass

def foo() -> None:
print(1, 2, 3)
```

or

```python
import sys

if sys.stdout.isatty():
try:
from rich import print
except ModuleNotFoundError:
pass

def foo() -> None:
print(1, 2, 3)
```

Using `rich~=14.2`

**VS Code extension or command-line**

pyright 1.1.406 on the command-line

Contributor guide

Open the contributing guide

Research direction

Start by running the two Python reproductions with pyright 1.1.406 and inspect how the analyzer handles the try-except import that redefines print. Trace the possibly-unbound diagnostic for foo, then confirm the diagnostic is no longer reported while genuine unbound cases remain detected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.