dropbox / dropbox/sqlalchemy-stubs

TypeDecorator.process_bind_param only possible return value is Optional[str], but this doesn't work for binary types

Open
#214 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
583
Forks
96
PR merge metrics
No merged PRs in 30d

Description

Probably doing something wrong here, but I can't figure out how to make this work:

```python
class EncryptedType(TypeDecorator):
impl = LargeBinary

def __init__(self, engine: EncryptionEngine, **kwargs: Any) -> None:
super().__init__(**kwargs)
self.engine = engine

def process_bind_param(self, value: Optional[str], dialect: Dialect) -> Optional[str]:
if value is None:
return None
return self.engine.encrypt(value).decode() # convert to string, but I'd rather not

def process_result_value(self, value: Optional[bytes], dialect: Dialect) -> Optional[str]:
if value is None:
return None
return self.engine.decrypt(value)
```

Trying to insert data into a column results in an error like this:

```StatementError: (builtins.TypeError) memoryview: a bytes-like object is required, not 'str'```

Simply returning `self.engine.encrypt(value)` without the `.decode()` to convert to a string works, but then mypy complains that ```Return type "Optional[bytes]" of "process_bind_param" incompatible with return type "Optional[str]" in supertype "TypeDecorator"```

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.