microsoft / microsoft/pyright

Type narrowing not done correctly for bitwise operations with `int` and instance of subclass of `enum.IntFlag`

Open
#11,515 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**

When &-ing an `int` and a member of an `enum.IntFlag`, another member of the flags is returned, but Pyright reveals it as an `int`. I believe this isn't an issue with typeshed, since mypy [has the correct behaviour](https://gist.github.com/mypy-play/0c29d5580d780221622b7d8af6221996).

**Code or Screenshots**

Code sample in [pyright playground](https://pyright-play.net/?code=GYJw9gtgBApgdgV2gSwgBzCALlAhgrMAGigEk4sAxAG1wHMBYAKAGNaBndqG%2B9gCnJVadAJQAuZlClQAglAC8eAmD4jJ0gEIKlhVcxAwAbjFzUA%2BlgCeaGH2QVVUAGTdh7AHQyRQA)

```python
from enum import auto, IntFlag
class Flags(IntFlag):
A = auto()
B = auto()
reveal_type(int() & Flags.A)
```

Contributor guide

Open the contributing guide

Research direction

Begin with the linked Pyright playground and reproduce the `int` bitwise operation with the `IntFlag` member. Trace the type-narrowing path for bitwise operations, then verify that `reveal_type` reports the appropriate flag type rather than `int` and that the behavior is covered by regression coverage.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.