dropbox / dropbox/sqlalchemy-stubs

Execution contexts missing some functions

Open
#139 2 comments 0 reactions 0 assignees View on GitHub
priority-normal topic-stubs
Dominant language
Python
Stars
583
Forks
96
PR merge metrics
No merged PRs in 30d

Description

SQLAlchemy allows defining column defaults with a callable getting the insert/update context as an argument ([doc](https://docs.sqlalchemy.org/en/13/core/defaults.html?highlight=column%20default%20callable#context-sensitive-default-functions)), e.g:

```python
def get_default(context):
return context.get_current_parameters()['name'] + 'whatever'

class MyModel(Base):
__tablename__ = 'my_model'

id = Column(Integer, primary_key=True)
name = Column(Unicode, nullable=False)
something = Column(Unicode, default=get_default, nullable=False)
```

I'm trying to add type annotations to the `get_default` function in my code. For the example above, the return type would be a `str` (the column is defined as `Unicode`).

The `context` argument is (in my case, using PostgreSQL with the pyscopg2 backend) an instance of the `sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2` class. Its MRO is:

```
>>> sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2.__mro__
(,
,
,
,
)
```

The problem is none of these classes have the `get_current_parameters()` method defined in sqlalchemy-stubs.

In SQLAlchemy, it's defined in [`sqlalchemy.engine.default.DefaultExecutionContext`](https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/engine/default.py#L1324), all the child classes just inherit from it.

I'd be happy to send a pull request adding the stubs for this function, but I'm unsure about the return value, since it returns a dictionary:

> which includes entries for each column/value pair that is part
> of the INSERT or UPDATE statement. The keys of the dictionary will be
> the key value of each :class:`.Column`, which is usually synonymous
> with the name.

So it seems to me the return value would be a `Dict[str, Any]`, since the key (name) of the `Column` will be a string, and its type can be anything, depending on how the column is defined.

```
def get_current_parameters(isolate_multiinsert_groups: bool = True) -> Dict[str, Any]: ...
```

Is that correct?

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.