You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optional[Raw] results in an unexpected validation error at json.decode time #659
it seems that the json parser finds a "object" type in the JSON stream but can't assign it to a fields that expects "null" (air-gapped system, i can't copy paste).
class Wrapper(Struct):
result: Optional[Raw]
.. and decode some JSON into it, like { "result" : { "foo": "bar" } } - and that will repro the error
Can you say more about your use case for the Optional[Raw] annotation? How do you plan on consuming this field post decode (or producing this field for encoding)?
The current Raw decoding logic assumed that Raw nodes won't ever be included in a union like Raw | None (same as Optional[Raw]) . What you're seeing here is a bug in this limitation actually being enforced.
That said, there's a few things we could do here:
Error if Raw is ever present in a Union. The type described above would error saying you can't include Raw inside a union like Optional[Raw].
Support Raw inside unions, but if Raw is ever present then we always decode it as a Raw value, even if it matches other types in the union. There's some precedence for this - this is how we handle Any today. In your case above mypy would let you pass a None or Raw to result, but when decoding you'd always end up with a Raw, even if the JSON value is null (in that case you'd get Raw(b'null'). The union support would then be most useful for cases where you might want to support passing in-memory objects when programatically constructing these, but on the decode-side always decode them as Raw.
Raw is not supported in unions except for with None. So result: Raw or result: Optional[Raw] both work, but result: Raw | int would error as an unsupported type. This restriction matches what we already do for custom types. In this case null would decode as None, but any other value would decode as a Raw value.
A mix of 2 and 3. In this case any union with Raw would be supported, but only Raw and None would actually be decoded, everything else would silently be ignored (like in 2).
Given these options, I'm leaning towards either 1 (easiest, best matches the main use case for Raw), or 3 (slightly harder, but may still be useful and matches a convention we had for other types). 2 also seems fine. I think option 4 would be confusing.
My particular use case is processing line-delimited json, each line may
contain an object that has a "result" property, or an "error" property.
grpc-gateway follows this protocol for streaming responses. the value of
"result" and "error" are encoded via protojson (vs regular json) so I want
to avoid decoding them using the stock json parser.
If I get a vote, it's for "Raw | None"
On Wed, Mar 20, 2024 at 8:58 PM Jim Crist-Harif ***@***.***> wrote:
Can you say more about your use case for the Optional[Raw] annotation?
How do you plan on consuming this field post decode (or producing this
field for encoding)?
------------------------------
The current Raw decoding logic assumed that Raw nodes won't ever be
included in a union like Raw | None (same as Optional[Raw]) . What you're
seeing here is a bug in this limitation actually being enforced.
That said, there's a few things we could do here:
1. Error if Raw is ever present in a Union. The type described above
would error saying you can't include Raw inside a union like
Optional[Raw].
2. Support Raw inside unions, but if Raw is ever present then we
always decode it as a Raw value, even if it matches other types in the
union. There's some precedence for this - this is how we handle Any
today. In your case above mypy would let you pass a None or Raw to
result, but when decoding you'd always end up with a Raw, even if the
JSON value is null (in that case you'd get Raw(b'null'). The union
support would then be most useful for cases where you might want to support
passing in-memory objects when programatically constructing these, but on
the decode-side always decode them as Raw.
3. Raw is not supported in unions *except* for with None. So result:
Raw or result: Optional[Raw] both work, but result: Raw | int would
error as an unsupported type. This restriction matches what we already do
for custom types. In this case null would decode as None, but any
other value would decode as a Raw value.
4. A mix of 2 and 3. In this case any union with Raw would be
supported, but only Raw and None would actually be decoded, everything
else would silently be ignored (like in 2).
Given these options, I'm leaning towards either 1 (easiest, best matches
the main use case for Raw), or 3 (slightly harder, but may still be
useful and matches a convention we had for other types). 2 also seems fine.
I think option 4 would be confusing.
—
Reply to this email directly, view it on GitHub
<#659 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAR5KLE5BTUSU3DBRIRDZT3YZIWERAVCNFSM6AAAAABFAPNLNOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMJQHE4TGOBUGI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
Sorry, 2, 3, or 4 could support that type, the question is what happens on decode.
# Given the following type definitionimportmsgspecclassWrapper(msgspec.Struct):
result: msgspec.Raw|None# And the messagemsg=b'{"result": null}'# The decode callmsgspec.json.decode(msg, type=Wrapper)
# would result in the following values:# 1. Would error as an unsupported type# 2. Would decode as `Wrapper(result=Raw(b'null'))`# 3. Would decode as `Wrapper(result=None)`# 4. Would decode as `Wrapper(result=None)`
As an aside, your use case would probably be best supported by not using Raw | None, but rather having result default to an empty Raw() instance. The following works today:
importmsgspecfromtypingimportOptionalclassWrapper(msgspec.Struct):
# result is always a Raw type, will use an empty Raw if not presentresult: msgspec.Raw=msgspec.Raw()
# you didn't say what the structure of `error` was, so guessing stringerror: str=""msgs= [
b'{"result": {"x": 1}}', # result presentb'{"result": null}', # result explicitly nullb'{"error": "an error message"}', # result missing, error present
]
formsginmsgs:
obj=msgspec.json.decode(msg, type=Wrapper)
# An empty `msgspec.Result()` is false-y, so you can branch on if# this field is Truthy to detect the presence of a result.ifobj.result:
print(f"result: {bytes(obj.result).decode()!r}")
else:
print(f"error: {obj.error!r}")
#> result: '{"x": 1}'#> result: 'null'#> error: 'an error message'
The behavior of (3) is what I was expecting, and would IMO be least
surprising.
Raw is not supported in unions *except* for with None. So result: Raw or result:
Optional[Raw] both work, but result: Raw | int would error as an
unsupported type. This restriction matches what we already do for custom
types. In this case null would decode as None, but any other value would
decode as a Raw value.
On Wed, Mar 20, 2024 at 9:18 PM Jim Crist-Harif ***@***.***> wrote:
If I get a vote, it's for "Raw | None"
Sorry, 2, 3, or 4 could support that type, the question is what happens on
decode.
# Given the following type definitionimport msgspec
class Wrapper(msgspec.Struct):
result: msgspec.Raw | None
# And the messagemsg = b'{"result": null}'
# The decode callmsgspec.json.decode(msg, type=Wrapper)
# would result in the following values:# 1. Would error as an unsupported type# 2. Would decode as `Wrapper(result=Raw(b'null'))`# 3. Would decode as `Wrapper(result=None)`# 4. Would decode as `Wrapper(result=None)`
------------------------------
As an aside, your use case would probably be best supported by *not*
using Raw | None, but rather having result default to an empty Raw()
instance. The following works today:
import msgspecfrom typing import Optional
class Wrapper(msgspec.Struct):
# result is always a Raw type, will use an empty Raw if not present
result: msgspec.Raw = msgspec.Raw()
# you didn't say what the structure of `error` was, so guessing string
error: str = ""
msgs = [
b'{"result": {"x": 1}}', # result present
b'{"result": null}', # result explicitly null
b'{"error": "an error message"}', # result missing, error present
]
for msg in msgs:
obj = msgspec.json.decode(msg, type=Wrapper)
# An empty `msgspec.Result()` is false-y, so you can branch on if
# this field is Truthy to detect the presence of a result.
if obj.result:
print(f"result: {bytes(obj.result).decode()!r}")
else:
print(f"error: {obj.error!r}")
#> result: '{"x": 1}'#> result: 'null'#> error: 'an error message'
—
Reply to this email directly, view it on GitHub
<#659 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAR5KLA2NNFTKF56BZPLB73YZIYONAVCNFSM6AAAAABFAPNLNOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMJRGAYTEMZUGE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
I agree with the empty Raw() approach if the field is optional. Using | None as the special singleton for Raw is not a good idea. Because this breaks the parsing behavior.
Activity
jcrist commentedon Mar 21, 2024
Thanks for opening this. Here's a complete reproducer:
jcrist commentedon Mar 21, 2024
Can you say more about your use case for the
Optional[Raw]annotation? How do you plan on consuming this field post decode (or producing this field for encoding)?The current
Rawdecoding logic assumed thatRawnodes won't ever be included in a union likeRaw | None(same asOptional[Raw]) . What you're seeing here is a bug in this limitation actually being enforced.That said, there's a few things we could do here:
Rawis ever present in aUnion. The type described above would error saying you can't includeRawinside a union likeOptional[Raw].Rawinside unions, but ifRawis ever present then we always decode it as aRawvalue, even if it matches other types in the union. There's some precedence for this - this is how we handleAnytoday. In your case abovemypywould let you pass aNoneorRawtoresult, but when decoding you'd always end up with aRaw, even if the JSON value isnull(in that case you'd getRaw(b'null'). The union support would then be most useful for cases where you might want to support passing in-memory objects when programatically constructing these, but on the decode-side always decode them asRaw.Rawis not supported in unions except for withNone. Soresult: Raworresult: Optional[Raw]both work, butresult: Raw | intwould error as an unsupported type. This restriction matches what we already do for custom types. In this casenullwould decode asNone, but any other value would decode as aRawvalue.Rawwould be supported, but onlyRawandNonewould actually be decoded, everything else would silently be ignored (like in 2).Given these options, I'm leaning towards either 1 (easiest, best matches the main use case for
Raw), or 3 (slightly harder, but may still be useful and matches a convention we had for other types). 2 also seems fine. I think option 4 would be confusing.jdef commentedon Mar 21, 2024
jcrist commentedon Mar 21, 2024
Sorry, 2, 3, or 4 could support that type, the question is what happens on decode.
As an aside, your use case would probably be best supported by not using
Raw | None, but rather havingresultdefault to an emptyRaw()instance. The following works today:jdef commentedon Mar 21, 2024
mmatl commentedon Jul 31, 2026
This is still an issue -- allowing for Optional[Raw] to decode null to None and anything else to Raw would be ideal.
sobolevn commentedon Nov 9, 2026
I agree with the empty
Raw()approach if the field is optional. Using| Noneas the special singleton forRawis not a good idea. Because this breaks the parsing behavior.