Type for arbitrary JSON data.
Very often while writing web-related scripts, I have a function that either accepts or returns some arbitrary JSON data. Sometimes I know that this will be an object, so I annotate it as `dict[str, Any]` - which isn't quite accurate, since the values can't be `Any`. Sometimes I'm feeling lazy and annotate it as `dict[str, Any]` even when it may be any other JSON type. Very occasionally do I go to the effort of making something more accurate, maybe JsonData = Union[dict[str, 'JsonData'], list['JsonData'], int, float, bool, None] I think adding something like this called `Json`, `JsonData`, `JsonEncodable` or similar to `typing` or `json` would be very helpful for people working with web APIs. Perhaps a `JsonObject`/`JsonDict` aliased to `dict[JsonData]` and a `JsonArray`/`JsonList` aliased to `list[JsonData]` (like in Java) would also be helpful.
El mar, 10 ago 2021 a las 14:37, Artemis (< artemisdev21+python-ideas@gmail.com>) escribió:
Very often while writing web-related scripts, I have a function that either accepts or returns some arbitrary JSON data.
Sometimes I know that this will be an object, so I annotate it as `dict[str, Any]` - which isn't quite accurate, since the values can't be `Any`. Sometimes I'm feeling lazy and annotate it as `dict[str, Any]` even when it may be any other JSON type.
Very occasionally do I go to the effort of making something more accurate, maybe
JsonData = Union[dict[str, 'JsonData'], list['JsonData'], int, float, bool, None]
I think adding something like this called `Json`, `JsonData`, `JsonEncodable` or similar to `typing` or `json` would be very helpful for people working with web APIs. Perhaps a `JsonObject`/`JsonDict` aliased to `dict[JsonData]` and a `JsonArray`/`JsonList` aliased to `list[JsonData]` (like in Java) would also be helpful.
This has previously been discussed in great detail in https://github.com/python/typing/issues/182 .
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: jelle.zijlstra@gmail.com
It's still worth discussing -- that issue is still open. @vanburgerberg posted a workaround that relies on an undocumented feature of mypy, so there's definitely room for better support from the type system here. In particular, recursive unions like those needed to define JSON object types would be useful for all sorts of data structures, notably binary trees! Perhaps this thread should be renamed "Recursive types". On Tue, Aug 10, 2021, at 5:46 PM, Jelle Zijlstra wrote:
El mar, 10 ago 2021 a las 14:37, Artemis (<artemisdev21+python-ideas@gmail.com <mailto:artemisdev21%2Bpython-ideas@gmail.com>>) escribió:
Very often while writing web-related scripts, I have a function that either accepts or returns some arbitrary JSON data.
Sometimes I know that this will be an object, so I annotate it as `dict[str, Any]` - which isn't quite accurate, since the values can't be `Any`. Sometimes I'm feeling lazy and annotate it as `dict[str, Any]` even when it may be any other JSON type.
Very occasionally do I go to the effort of making something more accurate, maybe
JsonData = Union[dict[str, 'JsonData'], list['JsonData'], int, float, bool, None]
I think adding something like this called `Json`, `JsonData`, `JsonEncodable` or similar to `typing` or `json` would be very helpful for people working with web APIs. Perhaps a `JsonObject`/`JsonDict` aliased to `dict[JsonData]` and a `JsonArray`/`JsonList` aliased to `list[JsonData]` (like in Java) would also be helpful. This has previously been discussed in great detail in https://github.com/python/typing/issues/182 .
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: jelle.zijlstra@gmail.com
Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: rbt@sent.as
Agree, the case of (recursive) JSON data structures seems pretty common now, and often just winds-up as `Any`. Out of curiosity, what is the undocumented feature in mypy? On Tue, 2021-08-10 at 18:02 -0400, Rebecca Turner wrote:
It's still worth discussing -- that issue is still open. @vanburgerberg posted a workaround that relies on an undocumented feature of mypy, so there's definitely room for better support from the type system here. In particular, recursive unions like those needed to define JSON object types would be useful for all sorts of data structures, notably binary trees!
Perhaps this thread should be renamed "Recursive types".
On Tue, Aug 10, 2021, at 5:46 PM, Jelle Zijlstra wrote:
El mar, 10 ago 2021 a las 14:37, Artemis (<artemisdev21+python-ideas@gmail.com>) escribió:
Very often while writing web-related scripts, I have a function that either accepts or returns some arbitrary JSON data.
Sometimes I know that this will be an object, so I annotate it as `dict[str, Any]` - which isn't quite accurate, since the values can't be `Any`. Sometimes I'm feeling lazy and annotate it as `dict[str, Any]` even when it may be any other JSON type.
Very occasionally do I go to the effort of making something more accurate, maybe
JsonData = Union[dict[str, 'JsonData'], list['JsonData'], int, float, bool, None]
I think adding something like this called `Json`, `JsonData`, `JsonEncodable` or similar to `typing` or `json` would be very helpful for people working with web APIs. Perhaps a `JsonObject`/`JsonDict` aliased to `dict[JsonData]` and a `JsonArray`/`JsonList` aliased to `list[JsonData]` (like in Java) would also be helpful.
This has previously been discussed in great detail in https://github.com/python/typing/issues/182 .
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: jelle.zijlstra@gmail.com
Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: rbt@sent.as
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: pbryan@anode.ca
Out of curiosity, what is the undocumented feature in mypy?
The use of a typing.Protocol with a __class__ attribute, I believe. On Tue, Aug 10, 2021, at 6:25 PM, Paul Bryan wrote:
Agree, the case of (recursive) JSON data structures seems pretty common now, and often just winds-up as `Any`.
Out of curiosity, what is the undocumented feature in mypy?
On Tue, 2021-08-10 at 18:02 -0400, Rebecca Turner wrote:
It's still worth discussing -- that issue is still open. @vanburgerberg posted a workaround that relies on an undocumented feature of mypy, so there's definitely room for better support from the type system here. In particular, recursive unions like those needed to define JSON object types would be useful for all sorts of data structures, notably binary trees!
Perhaps this thread should be renamed "Recursive types".
On Tue, Aug 10, 2021, at 5:46 PM, Jelle Zijlstra wrote:
El mar, 10 ago 2021 a las 14:37, Artemis (<artemisdev21+python-ideas@gmail.com <mailto:artemisdev21%2Bpython-ideas@gmail.com>>) escribió:
Very often while writing web-related scripts, I have a function that either accepts or returns some arbitrary JSON data.
Sometimes I know that this will be an object, so I annotate it as `dict[str, Any]` - which isn't quite accurate, since the values can't be `Any`. Sometimes I'm feeling lazy and annotate it as `dict[str, Any]` even when it may be any other JSON type.
Very occasionally do I go to the effort of making something more accurate, maybe
JsonData = Union[dict[str, 'JsonData'], list['JsonData'], int, float, bool, None]
I think adding something like this called `Json`, `JsonData`, `JsonEncodable` or similar to `typing` or `json` would be very helpful for people working with web APIs. Perhaps a `JsonObject`/`JsonDict` aliased to `dict[JsonData]` and a `JsonArray`/`JsonList` aliased to `list[JsonData]` (like in Java) would also be helpful. This has previously been discussed in great detail in https://github.com/python/typing/issues/182 .
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: jelle.zijlstra@gmail.com
Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: rbt@sent.as
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: pbryan@anode.ca
Thanks, sorry. On Tue, 10 Aug 2021, 22:47 Jelle Zijlstra, <jelle.zijlstra@gmail.com> wrote:
El mar, 10 ago 2021 a las 14:37, Artemis (< artemisdev21+python-ideas@gmail.com>) escribió:
Very often while writing web-related scripts, I have a function that either accepts or returns some arbitrary JSON data.
Sometimes I know that this will be an object, so I annotate it as `dict[str, Any]` - which isn't quite accurate, since the values can't be `Any`. Sometimes I'm feeling lazy and annotate it as `dict[str, Any]` even when it may be any other JSON type.
Very occasionally do I go to the effort of making something more accurate, maybe
JsonData = Union[dict[str, 'JsonData'], list['JsonData'], int, float, bool, None]
I think adding something like this called `Json`, `JsonData`, `JsonEncodable` or similar to `typing` or `json` would be very helpful for people working with web APIs. Perhaps a `JsonObject`/`JsonDict` aliased to `dict[JsonData]` and a `JsonArray`/`JsonList` aliased to `list[JsonData]` (like in Java) would also be helpful.
This has previously been discussed in great detail in https://github.com/python/typing/issues/182 .
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: jelle.zijlstra@gmail.com
For what it's worth, pyright supports recursive type aliases specifically to handle cases like this. There is an open issue in the mypy issue tracker that was created over six years ago: https://github.com/python/mypy/issues/731. It has over 250 upvotes, so I think it's safe to say that if someone were to implement this in mypy, they'd be a hero to many! If someone wants to try to implement it in mypy, I'd be happy to provide tips and pointers from my experience in implementing this in pyright. It was a relatively complicated feature, and it took several tries to get it right. -Eric -- Eric Traut Contributor to pylance & pyright Microsoft Corp.
Yeah, Pyre supports them as well. Recursive type aliases were a much-requested feature, both for things like JSON and for tree-like data structures. On Tue, Aug 10, 2021 at 5:10 PM Eric Traut <eric@traut.com> wrote:
For what it's worth, pyright supports recursive type aliases specifically to handle cases like this.
There is an open issue in the mypy issue tracker that was created over six years ago: https://github.com/python/mypy/issues/731. It has over 250 upvotes, so I think it's safe to say that if someone were to implement this in mypy, they'd be a hero to many!
If someone wants to try to implement it in mypy, I'd be happy to provide tips and pointers from my experience in implementing this in pyright. It was a relatively complicated feature, and it took several tries to get it right.
-Eric
-- Eric Traut Contributor to pylance & pyright Microsoft Corp. _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: gohanpra@gmail.com
-- S Pradeep Kumar
participants (7)
-
Artemis -
Artemis Dev -
Eric Traut -
Jelle Zijlstra -
Paul Bryan -
Rebecca Turner -
S Pradeep Kumar