microsoft / microsoft/pyright

allow non-descriptor subtypes

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

Description

### Description

consider the following example, where we want to define a `Protocol` for any object that has `system: str` and a `code: str` attributes:

```python
from typing import Protocol, runtime_checkable

from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass

class Base(MappedAsDataclass, DeclarativeBase):
pass

@runtime_checkable
class ConceptProto(Protocol):
@property
def system(self) -> str: ...

@property
def code(self) -> str: ...

class Concept(Base):
__tablename__ = "concept"

system: Mapped[str]
code: Mapped[str]

def fun(c: ConceptProto):
print(c.system, c.code)

fun(Concept(system="SNOMED", code="123456789"))
```

this currently fails with pyright:

```
#########################/proto.py
#########################/proto.py:30:5 - error: Argument of type "Concept" cannot be assigned to parameter "c" of type "ConceptProto" in function "fun"
  "Concept" is incompatible with protocol "ConceptProto"
    "system" is an incompatible type
      "Mapped[str]" is not assignable to "str"
    "code" is an incompatible type
      "Mapped[str]" is not assignable to "str" (reportArgumentType)
1 error, 0 warnings, 0 notes
```

it would be nice if sqlalchemy models could be used within that context, and manipulated by duck-typed libs without ever having to import sqlalchemy

you can see the initial discussion with the maintainer of sqlalchemy here: https://github.com/sqlalchemy/sqlalchemy/discussions/12889

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the supplied Python and SQLAlchemy example with pyright, then review the linked SQLAlchemy discussion. Trace how Protocol properties are checked against Mapped[str]; done means the example type-checks successfully while existing protocol diagnostics remain intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlalchemy
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.