Comments (11)
@MaicoTimmerman, bump 😛
from sqlalchemy2-stubs.
SQLAlchemy's Cast accepts a TypeEngine object, not a Python type. So here the more correct thing would be cast[sqlalchemy.types.Numeric]. We don't express SQL constructs in terms of Python datatypes, there is a layer of indirection from the TypeEngine.
from sqlalchemy2-stubs.
But this still would be one type argument, not two; isn't that right?
from sqlalchemy2-stubs.
from my POV it seems like you care about the resulting type of the construct, not the originating type, so yes. but i may not be seeing the whole picture.
from sqlalchemy2-stubs.
Yeah, that makes sense that I've approached from the different direction. But nevertheless the issue still stands as present. Unless I need to provide both originating & resulting types. I'll check tomorrow.
from sqlalchemy2-stubs.
I've looked into the originating+resulting vs resulting only generic type. I believe to be fully typed, we need both. Cast
exposes both the clause
and wrapped_column_expression
properties, which should be typed based on the source type, requiring 2 generic arguments.
However we'd need to do some more thinking about the source type to be ORM compatible. Currently, it expects a source to be inherited from ClauseElement
, however ORM mapped instance attributes are python types:
from sqlalchemy import cast, Float, Column, Numeric
from sqlalchemy.orm import declarative_base
Base = declarative_base()
class X(Base):
y = Column(Float)
x = X()
x.y = 2.0
reveal_type(x.y)
reveal_type(cast(x.y, Numeric)) # error: Value of type variable "_CLE" of "Cast" cannot be "float"
reveal_type(cast(X.y, Numeric)) # works
I also noted that the generic is currently Cast[<result_type>, <source_type>]
, which I'd find to be counter-intuitive, as the generic order is reversed from the calling argument order.
from sqlalchemy2-stubs.
I've looked into the originating+resulting vs resulting only generic type. I believe to be fully typed, we need both.
Cast
exposes both theclause
andwrapped_column_expression
properties, which should be typed based on the source type, requiring 2 generic arguments.
Well the ".clause" of the Cast is really not important on the outside, it's there for informational purposes but does not determine any meaningful behavior of the construct, only the specifics of the string that is generated in compilation. There has to be some kind of boundary for what we put in the []
of a generic type. If I have an object that has ".x = int, .y = dict, .foo = Bar()" as members, we hardly would normally be passing around things like MyObject[int, dict, Bar]
- there's some threshold of what's actually important to be in the generic slot, and in this case 100% of the behavior of the object as manipulated in Python and used in idiomatic patterns is determined by the target type, not the source type.
I also noted that the generic is currently
Cast[<result_type>, <source_type>]
, which I'd find to be counter-intuitive, as the generic order is reversed from the calling argument order.
I don't think every member of an object that happens to have a SQL type needs to be part of the generic representation, and perhaps we should try to clarify what really goes into the []
and why.
from sqlalchemy2-stubs.
based on this issue I had some code like:
rows = await session.execute(
sql.select([func.pg_try_advisory_xact_lock(cast(key, BigInteger))])
)
where key
is a python integer, i.e. just a literal value for SQL and not a field expression
this worked fine in my unit tests, but mypy complains:
error: Value of type variable "_CLE" of "Cast" cannot be "int"
After a bit of experimenting I found this formulation that works and also satisfies mypy:
rows = await session.execute(
sql.select([func.pg_try_advisory_xact_lock(literal(key, BigInteger))])
)
from sqlalchemy2-stubs.
cast()
accepts the type as the first argument and the value as second arg, eg, cast(BigInteger, key)
.
from sqlalchemy2-stubs.
cast()
accepts the type as the first argument and the value as second arg, eg,cast(BigInteger, key)
.
I think that's mypy cast
... here I am talking about from sqlachemy import cast
from sqlalchemy2-stubs.
Oh my bad! Totally read it as typing cast
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.