-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeDecorator.process_bind_param can return more than Optional[Text] #205
Comments
The process_bind_param and process_literal_param methods are called to do decorator-specific conversion of values, before deferring to the underlying .impl's conversion methods. This means they can return any value accepted by those methods, not just str (or typing.Text). These are related to the process_result_value method, which is effectively the inverse, doing decorator-specific conversion of the output of the underlying .impl, and indeed this method accepts value: Optional[Any], representing the unknown type of the output of the underlying .impl. Unfortunately, modelling this accurately is likely to be impossible (or at least, much more difficult), because, for instance, the `impl` property itself can only be typed as `Any`, let alone the input/output type it uses. Fixes dropbox#205
The process_bind_param and process_literal_param methods are called to do decorator-specific conversion of values, before deferring to the underlying .impl's conversion methods. This means they can return any value accepted by those methods, not just str (or typing.Text). These are related to the process_result_value method, which is effectively the inverse, doing decorator-specific conversion of the output of the underlying .impl, and indeed this method accepts value: Optional[Any], representing the unknown type of the output of the underlying .impl. Unfortunately, modelling this accurately is likely to be impossible (or at least, much more difficult), because, for instance, the `impl` property itself can only be typed as `Any`, let alone the input/output type it uses. Fixes dropbox#205
I've run into this as well. My use case is a class FloatDateTime(TypeDecorator):
impl = Float
def process_bind_param(
self, value: Optional[datetime.datetime], _
) -> Optional[float]:
if value is None:
return None
return value.timestamp()
def process_result_value(
self, value: Optional[float], _
) -> Optional[datetime.datetime]:
if value is None:
return None
return datetime.datetime.fromtimestamp(value, tz=datetime.timezone.utc) I think the biggest downside to your proposed solution is that introducing
|
@watterso I tried to do something like that, but I couldn't work out how express it on the I think |
Currently, the
process_bind_param
method onTypeDecorator[_T]
has signature:sqlalchemy-stubs/sqlalchemy-stubs/sql/type_api.pyi
Line 95 in 8495c22
Unfortunately, it looks like this is incorrect: I believe it can return anything that the underlying
impl
can accept. For instance, in SQLAlchemy's tests there's type decorators that returnint
s:The
process_bind_param
return value should probably be loosened to match theOptional[Any]
of its inverse operationprocess_result_value
.sqlalchemy-stubs/sqlalchemy-stubs/sql/type_api.pyi
Line 96 in 8495c22
(This probably applies to
process_literal_param
too.)The text was updated successfully, but these errors were encountered: