Metadata-Version: 2.1
Name: typeguards
Version: 0.2.0
Summary: Utilities to help with type checking.
Author-email: Narvin Singh <Narvin.A.Singh@gmail.com>
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.
        
Project-URL: Homepage, https://gitlab.com/narvin/typeguards
Project-URL: Documentation, https://typeguards.readthedocs.io/en/stable/
Project-URL: Repository, https://gitlab.com/narvin/typeguards
Project-URL: Bug Tracker, https://gitlab.com/narvin/typeguards/-/issues
Keywords: type checker,type checking,type utility,typeguard
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/x-rst
Provides-Extra: code_quality
Provides-Extra: docs
Provides-Extra: test
Provides-Extra: deploy
License-File: LICENSE

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
