Comments (11)
hey there -
this is not reasonable for the reason that Mypy and Python 3.9 don't yet support variadic generics so this cannot be implemented. The case of Query(ASingleClass) is a degenerate case, any number of arguments are accepted.
from sqlalchemy2-stubs.
How is this issue here different from how the other project handles it: https://github.com/dropbox/sqlalchemy-stubs/blob/55470ceab8149db983411d5c094c9fe16343c58b/sqlalchemy-stubs/orm/query.pyi#L13
from sqlalchemy2-stubs.
I think sqlalchemy-stubs is doing it incorrectly. query does not accept a single type it accepts any number of types. Eg.:
q1: Query[User] = session.query(User)
q2: Query[???] = session.query(User.id, User.name)
q3: Query[???] = session.query(User, func.count(Address.id))
etc.
from sqlalchemy2-stubs.
Query is also legacy API. the issue is even more difficult to implement for 1.4/2.0 style queries which return Row() objects.
from sqlalchemy2-stubs.
That seems solvable with a generic on the Row object, no?
from sqlalchemy2-stubs.
And FWIW, being able to get typing on these is quite convenient: https://github.com/dropbox/sqlalchemy-stubs/blob/55470ceab8149db983411d5c094c9fe16343c58b/sqlalchemy-stubs/orm/query.pyi#L80-L86:
def all(self) -> List[_T]: ...
def first(self) -> Optional[_T]: ...
def one_or_none(self) -> Optional[_T]: ...
def one(self) -> _T: ...
def __iter__(self) -> Iterator[_T]: ...
from sqlalchemy2-stubs.
good mypy support is one of the main reasons why I went through the effort to replace Query entirely. Query is legacy stuff and I want people to migrate towards 1.4/2.0 style usage. in the new API, all of those methods above return a Row, not an object. If you want a list of scalars, you call the .scalars() method on Result. So you can't type Result at all against a particular user defined class, because the return type is always Row, and without variadic generics, mypy has no way to know what the individual elements of Row are.
I know it's possible because tuple(), which is currently the only type that acts this way, can do it:
from typing import Tuple
a: Tuple[int, str, bool] = (5, 'hi', True)
a[1] + 'foo'
if people are writing new code w/ pep 484 typing I want them on the new stuff. I don't want to give people any reason to stay on Query and implementing this only for Query and not the new API would feel very wrong to me.
from sqlalchemy2-stubs.
Understood, but that does present a bit of a challenge when thinking of migrating from 1.3 to 1.4 on the way to 2.0. Currently I am on 1.3 using the existing stubs + mypy. When I attempted to upgrade to 1.4 and move to use the new stubs all of a sudden code that used to work with mypy no longer does.
from sqlalchemy2-stubs.
There is already a non-compatible difference in how we implement Column, in terms of the TypeEngine e.g. Column[Integer] as opposed to Column[int]. Maybe that doesn't cause problems.
from sqlalchemy2-stubs.
because Query already has the behavior of returning a single object from .all() / one()/ etc. if it's against a single entity, and a tuple only if there is a column or multiple entities, means the way they have it at https://github.com/dropbox/sqlalchemy-stubs/blob/master/sqlalchemy-stubs/orm/session.pyi#L68 is the only way to do it anyway, and as they say the *entities
case would need a plugin for a more accurate type. That "Any" they have there should be Row.
the whole thing would be much nicer with select() and Result because select() can be truly variadic in all cases.
from sqlalchemy2-stubs.
cc @layday who is playing with the variadic stuff right now
from sqlalchemy2-stubs.
Related Issues (20)
- Incorrect keyword argument name 'parameters' for AsyncSession.scalars, AsyncEngine.scalars HOT 1
- Improve declarative_mixin stub
- TupleType missing from sqlalchemy/types.pyi
- Make Column and relationship inherit from Mapped HOT 5
- MemoizedHasCacheKey import path is wrong
- Using compiles leads to untyped decorator HOT 1
- Type error when attempting to use a Literal[True] with sa.and_() HOT 1
- Error importing plugin "sqlalchemy.ext.mypy.plugin": cannot import name 'Optional' from 'mypy.plugin' HOT 1
- Invalid base class "Base" when returned by a function HOT 9
- Incomplete typing for Engine.dispose's close parameter HOT 1
- TypeEngine.with_variant should accept TypeEngine for type argument instances as well as Type[TypeEngine] HOT 1
- Cannot access member "add" for type "AsyncSession" (pylance) SQLAlchemy 1.4 HOT 2
- pre-commit config generates 100s of errors HOT 1
- Cannot access "sqlalchemy.orm.mapped_column" HOT 1
- MutableBase.coerce incorrectly returns None HOT 1
- Type checking fails when using `joins_implicitly` with `column_valued()` HOT 1
- [0.0.2a36 regression] `.column_valued()` without a `name` is no longer accepted
- TypeError: format_type() missing 1 required positional argument: 'options' HOT 1
- Pyright: Mapped is considered unhashable
- Incompatible types in assignment when base declaration is in a separate file HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlalchemy2-stubs.