sqlalchemy / sqlalchemy

The Database Toolkit for Python
https://www.sqlalchemy.org
MIT License
8.97k stars 1.35k forks source link

ColumnCollection.get(col, default) types as Optional even with default #11328

Closed mark-thm closed 3 weeks ago

mark-thm commented 1 month ago

Ensure stubs packages are not installed

Verify if the api is typed

Describe the typing issue

ColumnCollection.get(column, default) returns an Optional even when a default is specified, but the return is non-None when default is specified.

To Reproduce

table: sa.Table = ...

col: sa.ColumnElement = table.c.get(“foo”, sa.literal(“bar”)) # disallowed, result is possibly None

Error

No error — the typings result in .get returning an Optional

Versions

Additional context

https://github.com/sqlalchemy/sqlalchemy/blob/046ebffed77aff2f5e039763af4a3eb8a122c9c8/lib/sqlalchemy/sql/base.py#L1644 should likely have an overload for when default is present.

sqla-tester commented 1 month ago

Mark Elliot has proposed a fix for this issue in the main branch:

Add overload for ColumnCollection.get(col, default) https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5286

sqla-tester commented 4 weeks ago

Mark Elliot has proposed a fix for this issue in the rel_2_0 branch:

Add overload for ColumnCollection.get(col, default) https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5289