-
Notifications
You must be signed in to change notification settings - Fork 0
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
Exceptions? #5
Comments
if isinstance(payload, EncodeError):
return payload is simple, explicit, and unambiguous. |
More boilerplate, but one could consider a rust-like Half-baked sketch: @dataclasses.dataclass(frozen=True, slots=True)
class Error:
msg: str
def unwrap() -> Never:
raise ValueError(self.msg)
@dataclasses.dataclass(frozen=True, slots=True)
class Success[T]:
value: T
def unwrap() -> T:
return self.value
def encode_to_document(obj: object, fmt: SerialisationFormat) -> Error | Success[Document]:
... |
Unfortunately you get the explosion: # hypothetical
return [encode(x, fmt) for x in some_list]
# current
res: list[JsonType] = []
for x in some_list:
tmp = encode(x, fmt)
if isinstance(tmp, EncodeError):
return tmp
res.append(tmp)
return res but it's also true that the comprehension case won't be encountered by downstream users, since if they have an attribute they want to encode that is a list, then they can can rely on edit: I don't really care about boilerplate at all within this library. So on those grounds error types are actually not so bad. |
I think |
So it seems like someone made a library roughly along these lines. |
The current early prototype uses error value types rather than exceptions.
The ergonomics of this, unfortunately, are not great, because Python has no equivalent to rust's
?
operator which would allow immediately returning from the current value with any error value encountered.This adds boilerplate in
encode
anddecode
, including in the coders that users will write (since they may have to encode attributes of a type).Exceptions are "easier", at the expense of harder-to-follow control flow in error cases.
The text was updated successfully, but these errors were encountered: