microsoft / microsoft/pyright

@runtime_checkable should require a direct inheritance from Protocol

Open
#11,131 0 comments 1 reaction 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**

See snippet below. ~~An error is reported for checking whether `No()` is of instance `UnsafeProtocol` but not of instance `ChildUnsafeProtocol`.~~ The snippet will trigger a runtime `TypeError` since `ChildUnsafeProtocol` does not directly inherit from Protocol.

**Code or Screenshots**

```python
from typing import Protocol, runtime_checkable

@runtime_checkable
class UnsafeProtocol(Protocol):
def foo(self) -> int: ...

@runtime_checkable
class ChildUnsafeProtocol(UnsafeProtocol): # No error
def bar(self) -> str: ...

class No:
def foo(self) -> str:
return "not an int"
def bar(self) -> int:
return 42

isinstance(No(), UnsafeProtocol)
isinstance(No(), ChildUnsafeProtocol)
```

https://pyright-play.net/?strict=true&analyzeUnannotatedFunctions=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAK4MYAxmADYA0UIArijFgKYD6pAFs6QNYCGAIwrMAUCIAC9Ri3Zdeg4SNIU%2BAZ1VQAqilV9gzImBLkKACkPHKASgBcIqA6gATZsCjAwYU6uYVgVqABaAD5MRhsoADposUkGJgg2Tm5%2BIVFlNQ0AYQ4kCidtXX0LMkpTQr0DYlKKW3tHFzcBPhBvX38g0NUYEAjoyLEM9SgAOTA7R2dXd082vwCQqG7e%2BsmHEGYYOhAUKAAiFCMoPl3UGD3VqaaWuY7Fs4m19c3t3YAWACYxJFVUbpPSMxTGNTFYaBVitUTFYRD8-jAAUCQWCoDk8gUdJUStCHABiUZgKDMEDgEBAA

**VS Code extension or command-line**
Are you running pyright as a VS Code extension, a language server in another editor, integrated into Pylance, or the command-line tool? Which version?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the provided Python snippet and inspect pyright's handling of @runtime_checkable and Protocol inheritance. The change is complete when a runtime-checkable protocol that does not directly inherit from Protocol is diagnosed consistently, with behavior covered by an appropriate test.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.