I read this like 10 times and I still dont understand this mess of grammar. Am I having a fucking stroke rn?
I read this like 10 times and I still dont understand this mess of grammar. Am I having a fucking stroke rn?
- change it to a new value
- set it to None
- leave it as-is
In the json the first two options are specified by sending an object with a "subscription" field, either set to a string or to null. The third case is expressed by omitting the field.
The OP asks how all three cases could be represented in python, and points out that one could not use subscription=None to represent both case 2 and 3 above.
set - no value
- new value
do_no_set
(sorry about the horrible tree) then it seems like you should represent the data like that. Pass in action (set/ignore) and value (value for set, no value for ignore). Or even (set/unset/ignore), which allows you to have some sanity checking that, if the action is set, an actual value is provided (and vice versa for unset).If the “subscription” part of the “patch” data-structure is set to “None” it is impossible to tell if this means:
1/ leave the “subscription” unchanged; or
2/ set the “subscription” to None.
—
You could solve this by requiring that a subscription is either a value or some other sentinel value meaning “not subscribed”:
from typing import Union
class T: pass
NotSubscribed = T()
Subscription = Union[int, T]
def f(s: Subscription): pass
f(44)
f(NotSubscribed)
f(None) # wrong
Is there a better way of doing this? (One that isn’t just “use Haskell” :)> Here we have a problem: for subscription, does None mean don't change or remove subscription?