Name | typeguards JSON |
Version |
0.2.0
JSON |
| download |
home_page | |
Summary | Utilities to help with type checking. |
upload_time | 2022-12-06 02:33:39 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.10 |
license | The MIT License (MIT) Copyright © 2022 Narvin Singh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
type checker
type checking
type utility
typeguard
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
typeguards
==========
This is a Python library of TypeGuard_ functions that can be used for static type
narrowing, and runtime type checking.
.. _TypeGuard: https://docs.python.org/3/library/typing.html#typing.TypeGuard
.. code-block:: python
is_json(
{
"str_key": "str_val",
"int_key": 1,
"float_key": 1.0,
"bool_key": True,
"none_key": None,
"list_key": [1, 2, "3", {"foo": "bar"}],
"dict_key": {"foo": "bar"},
},
) # True
Installation
------------
.. code-block:: shell
pip install typeguards
Usage
-----
Assert that an object is valid JSON.
.. code-block:: python
from typeguards.json import is_json
assert is_json(
{
"str_key": "str_val",
"int_key": 1,
"float_key": 1.0,
"bool_key": True,
"none_key": None,
"list_key": [1, 2, "3", {"foo": "bar"}],
"dict_key": { # dict values can be nested infinitely
"foo": "bar",
},
},
) # OK
assert is_json("a string") # AssertionError
assert is_json([1, 2, 3]) # AssertionError
Assert that an object conforms to a JSON schema.
.. code-block:: python
from typing import List, NamedTuple, TypedDict
from typeguards.json import is_json_schema
class HobbySchema(NamedTuple):
name: str
is_fun: bool
class UserSchema(TypedDict):
id: int
username: str
hobbies: List[HobbySchema] # Nested schema
assert is_json_schema(
{
"id": 7,
"username": "charlotte",
"hobbies": [{"name": "Hyrule Warriors: Age of Calamity", "is_fun": True}],
},
UserSchema,
) # OK
assert is_json_schema(
{
# No id, but still conforms to schema
"username": "oscar",
"hobbies": [{"name": "Whacking things", "is_fun": True}],
},
UserSchema,
) # OK
assert is_json_schema(
{
"bad-id": 123, # Doesn't conform to schema
"username": "narvin",
"hobbies": [{"name": "Watching coding videos", "is_fun": False}],
},
UserSchema,
) # AssertionError
Raw data
{
"_id": null,
"home_page": "",
"name": "typeguards",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "",
"keywords": "type checker,type checking,type utility,typeguard",
"author": "",
"author_email": "Narvin Singh <Narvin.A.Singh@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/1b/6f/ea9a065815ff0587e263e4738b84d50918c3c81ef88a4ede807109946ae8/typeguards-0.2.0.tar.gz",
"platform": null,
"description": "typeguards\n==========\n\nThis is a Python library of TypeGuard_ functions that can be used for static type\nnarrowing, and runtime type checking.\n\n.. _TypeGuard: https://docs.python.org/3/library/typing.html#typing.TypeGuard\n\n.. code-block:: python\n\n is_json(\n {\n \"str_key\": \"str_val\",\n \"int_key\": 1,\n \"float_key\": 1.0,\n \"bool_key\": True,\n \"none_key\": None,\n \"list_key\": [1, 2, \"3\", {\"foo\": \"bar\"}],\n \"dict_key\": {\"foo\": \"bar\"},\n },\n ) # True\n\nInstallation\n------------\n\n.. code-block:: shell\n\n pip install typeguards\n\nUsage\n-----\n\nAssert that an object is valid JSON.\n\n.. code-block:: python\n\n from typeguards.json import is_json\n\n assert is_json(\n {\n \"str_key\": \"str_val\",\n \"int_key\": 1,\n \"float_key\": 1.0,\n \"bool_key\": True,\n \"none_key\": None,\n \"list_key\": [1, 2, \"3\", {\"foo\": \"bar\"}],\n \"dict_key\": { # dict values can be nested infinitely\n \"foo\": \"bar\",\n },\n },\n ) # OK\n\n assert is_json(\"a string\") # AssertionError\n assert is_json([1, 2, 3]) # AssertionError\n\nAssert that an object conforms to a JSON schema.\n\n.. code-block:: python\n\n from typing import List, NamedTuple, TypedDict\n\n from typeguards.json import is_json_schema\n\n\n class HobbySchema(NamedTuple):\n name: str\n is_fun: bool\n\n\n class UserSchema(TypedDict):\n id: int\n username: str\n hobbies: List[HobbySchema] # Nested schema\n\n\n assert is_json_schema(\n {\n \"id\": 7,\n \"username\": \"charlotte\",\n \"hobbies\": [{\"name\": \"Hyrule Warriors: Age of Calamity\", \"is_fun\": True}],\n },\n UserSchema,\n ) # OK\n\n assert is_json_schema(\n {\n # No id, but still conforms to schema\n \"username\": \"oscar\",\n \"hobbies\": [{\"name\": \"Whacking things\", \"is_fun\": True}],\n },\n UserSchema,\n ) # OK\n\n assert is_json_schema(\n {\n \"bad-id\": 123, # Doesn't conform to schema\n \"username\": \"narvin\",\n \"hobbies\": [{\"name\": \"Watching coding videos\", \"is_fun\": False}],\n },\n UserSchema,\n ) # AssertionError\n",
"bugtrack_url": null,
"license": "The MIT License (MIT) Copyright \u00a9 2022 Narvin Singh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \u201cSoftware\u201d), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \u201cAS IS\u201d, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "Utilities to help with type checking.",
"version": "0.2.0",
"split_keywords": [
"type checker",
"type checking",
"type utility",
"typeguard"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "8e55f40ec7890e6e8ed93a2c4f94d50d",
"sha256": "60e4d531c309cbe0029e982c524748888f2ad1c73f06a20dd0966e7721d41788"
},
"downloads": -1,
"filename": "typeguards-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8e55f40ec7890e6e8ed93a2c4f94d50d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 6623,
"upload_time": "2022-12-06T02:33:37",
"upload_time_iso_8601": "2022-12-06T02:33:37.430346Z",
"url": "https://files.pythonhosted.org/packages/e9/35/934b7fd1182fb0ce074d39bb23b8ece6b8bccbcb29b8db6a139bb3beb44c/typeguards-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "e045b1c5082ff52762e04d2e7f48fb60",
"sha256": "d504a6bafe9020bbe02e0ab22fd466850cae1c7598b82313e80cf0b229509cc2"
},
"downloads": -1,
"filename": "typeguards-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "e045b1c5082ff52762e04d2e7f48fb60",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 7270,
"upload_time": "2022-12-06T02:33:39",
"upload_time_iso_8601": "2022-12-06T02:33:39.127443Z",
"url": "https://files.pythonhosted.org/packages/1b/6f/ea9a065815ff0587e263e4738b84d50918c3c81ef88a4ede807109946ae8/typeguards-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-06 02:33:39",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "typeguards"
}