dropbox / dropbox/sqlalchemy-stubs

Model descriptors as constructor kwargs are not supported

Open
#253 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
583
Forks
96
PR merge metrics
No merged PRs in 30d

Description

Hello, I've an interesting issue and a suggestion here.
Next example is OK at runtime. It's connected with how SQLAlchemy initializes models when creates them using setattr or smth similar. So whatever attribute is passed it will be set regardless of whether such a column exists or not.
```python
class D:
def __set_name__(self, _, name):
self.attr = "_" + name

def __get__(self, obj, _):
return getattr(obj, self.attr)

def __set__(self, obj, v):
setattr(obj, self.attr, v)

class M(Model):
_x = Column(Integer)
x = D()

def test_(s: Session):
m = M(x=42) # <---- Unexpected keyword argument "x" for "M" [call-arg] mypy(error)
s.add(m)
```
As it's shown we have a private field and the descriptor to get its value having some kind of additional logic.
Thanks to `sqlalchemy-stubs`, `mypy` sees problem when it is passed to `__init__` a keyword that is not a column at all.
However, in the example above `x` field is a descriptor and it will be initialized as expected at runtime. It mimics a valid parameter and an argument to initialize a model with. You can't spot the difference from the outside.
It seems possible to support such a kind of descriptor checking, am I right?
Could it be a use case for sqlalchemy-stubs?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.