{ "info": { "author": "Yipit Coders", "author_email": "coders@yipit.com", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python" ], "description": "relaxed_types\n=============\n\nThis library provides a DSL to do type check in Python. The following is provided:\n\n* ``typed_return``: Decorator used to verify the type of the return value\n* ``check_type``: Checks if a value matches to type and predicate specifications\n* ``Any``: A sentinel object that matches any python object used with ``check_type`` or ``typed_returned``\n* ``Values``: A predicate function that matches the specified values instead of specifications\n* ``Or``: A predicate function that performs ensures that one of the specifications match\n* ``And``: A predicate function that performs ensures all specifications match\n* ``ReturnTypeError``: The exception that ``check_type`` raises if a type check fails\n\n\n\nThe main goal of this library is to have a simple way to ensure return types dynamically via ``typed_return``.\n\n\ntyped_return\n------------\n\n\n\nLists\n+++++\n\nThe following snippet shows how to perform a type check (list of integers):\n\n.. code:: python\n\n\n >>> @typed_return([int])\n ... def func(v):\n ... return v + [3, 4]\n ...\n >>> func([1, 2])\n [1, 2, 3, 4]\n >>> func([1, 2.0])\n Traceback (most recent call last):\n ...\n relaxed_types.ReturnTypeError: Type mismatch for 2.0, expected . Outer value: [1, 2.0, 3, 4]\n\n\nTuples\n++++++\n\n\nDifferent from lists, tuples have a fixed size. The tuple specification length has to match the value length.\n\n\n.. code:: python\n\n >>> @typed_return( (str, int) )\n ... def func(v):\n ... return v\n ...\n >>> func( ('hello', 123) )\n ('hello', 123)\n >>> func( ('hello', 'world') )\n Traceback (most recent call last):\n ...\n relaxed_types.ReturnTypeError: Type mismatch for 'world', expected . Outer value: ('hello', 'world')\n\n\nSets\n++++\n\nSets behave the same as lists:\n\n\n.. code:: python\n\n >>> @typed_return({str})\n ... def func(x):\n ... return x.union({\"test\"})\n ...\n >>> func({\"a\", \"b\"})\n set(['a', 'test', 'b'])\n >>> func({\"a\", \"b\", 1, 2, 3})\n Traceback (most recent call last):\n ...\n relaxed_types.ReturnTypeError: Type mismatch for 1, expected . Outer value: set(['a', 1, 2, 3, 'test', 'b'])\n\n\nDictionaries\n++++++++++++\n\nIt is possible to specify the expected types for dictionary key values. All keys specified must exist in the dictionary \u2014- the value ``Any`` can be specified as a key in order to validate additional keys.\n\n\n.. code:: python\n\n >>> @typed_return({\"name\": str, \"age\": int})\n ... def func(v):\n ... v['test'] = 'test'\n ... return v\n ...\n >>> func({\"name\": \"John Doe\", \"age\": 21})\n {'test': 'test', 'age': 21, 'name': 'John Doe'}\n >>> func({\"name\": \"Guy\", \"age\": \"47\"})\n Traceback (most recent call last):\n ...\n relaxed_types.ReturnTypeError: Type mismatch for '47', expected . Outer value: {'test': 'test', 'age': '47', 'name': 'Guy'}\n\n\n\nThe following example shows how to specify a dictionary with key ``name`` as ``str`` and any other key as ``int``.\n\n.. code:: python\n\n >>> from relaxed_types import *\n >>> @typed_return({\"name\": str, Any: int})\n ... def func(x):\n ... return x\n ...\n >>> func({\"name\": \"John Doe\", \"b\": 2, \"c\": 3})\n {\"name\": \"John Doe\", \"b\": 2, \"c\": 3}\n\n\n\nPredicates\n++++++++++\n\nPredicates allow you to create custom type checks.\nA predicate is a function that expects an object and returns a boolean: ``True`` means the object passed in matches the expectations and ``False`` means it does not.\n\nThe following snippet ensures `func` only returns odd numbers:\n\n.. code:: python\n\n >>> def odd(x):\n ... return x % 2 != 0\n ...\n >>> @typed_return(odd)\n ... def func(v):\n ... return v * 3\n ...\n >>> func(1)\n 3\n >>> func(2)\n Traceback (most recent call last):\n ...\n relaxed_types.ReturnTypeError: Type mismatch for 6, expected . Outer value: 6\n\n\nBecause of predicate support, you can integrate ``relaxed_types`` with other libraries, such as voluptuous_:\n\n.. code:: python\n\n >>> from voluptuous import Length\n >>> @typed_return([int], Length(min=10, max=100))\n ... def func(l):\n ... return l * 2\n ...\n >>> func(range(10))\n [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n >>> func(range(3))\n Traceback (most recent call last):\n ...\n voluptuous.LengthInvalid: length of value must be at least 10\n\nThe only issue with this integration is that it might either raise ``ReturnTypeError`` or\nan exception that inherits from ``voluptuous.errors.Invalid``.\n\n\nValues\n++++++\n\nPredicate function that matches the specified values (not specifications). This is useful to test for literals:\n\n\n.. code:: python\n\n >>> func(0)\n 0\n >>> func(1)\n 1\n >>> func(2)\n Traceback (most recent call last):\n File \"\", line 1, in \n File \"relaxed_types/__init__.py\", line 16, in newfn\n check_type(result, expected_type, outer_value=result, extra=extra)\n File \"relaxed_types/checks.py\", line 22, in check_type\n _check_predicate(value, expected_type, outer_value)\n File \"relaxed_types/checks.py\", line 35, in _check_predicate\n _fail(value, expected_type, outer_value, msg=expected_type.__doc__)\n File \"relaxed_types/checks.py\", line 85, in _fail\n raise ReturnTypeError(msg, value)\n relaxed_types.exceptions.ReturnTypeError: Expected \"2\" to be in (0, 1)\n\n\nOr\n++\n\nPredicate function that matches at least one specification:\n\n.. code:: python\n\n >>> @typed_return(Or(int, float))\n ... def func(x):\n ... return x\n ...\n >>> func(1)\n 1\n >>> func(1.0)\n 1.0\n >>> func(\"1\")\n Traceback (most recent call last):\n File \"\", line 1, in \n File \"relaxed_types/__init__.py\", line 16, in newfn\n check_type(result, expected_type, outer_value=result, extra=extra)\n File \"relaxed_types/checks.py\", line 22, in check_type\n _check_predicate(value, expected_type, outer_value)\n File \"relaxed_types/checks.py\", line 35, in _check_predicate\n _fail(value, expected_type, outer_value, msg=expected_type.__doc__)\n File \"relaxed_types/checks.py\", line 85, in _fail\n raise ReturnTypeError(msg, value)\n relaxed_types.exceptions.ReturnTypeError: '1' did not match Or(, ).\n More details about the last check: Type mismatch for '1', expected . Outer value: '1'\n\n\n\nAnd\n+++\n\nPredicate function that matches all specifications:\n\n.. code:: python\n\n >>> from relaxed_types import *\n >>> @typed_return({\"i\": And(int, lambda x: x > 0)})\n ... def func(x):\n ... return {\"i\": x}\n ...\n >>> func(1)\n {'i': 1}\n >>> func(1.0)\n Traceback (most recent call last):\n ...\n relaxed_types.exceptions.ReturnTypeError: 1.0 did not match And(, at 0x105f7a848>).\n More details about the last check: Type mismatch for 1.0, expected . Outer value: 1.0\n >>> func(-1)\n Traceback (most recent call last):\n ...\n relaxed_types.exceptions.ReturnTypeError: -1 did not match And(, at 0x105f7a848>).\n More details about the last check: Type mismatch for -1, expected at 0x105f7a848>. Outer value: -1\n\n\nCombining all together\n++++++++++++++++++++++\n\nIt's possible to combine lists, tuples, dictionaries, predicates, and any Python type.\n\n.. code:: python\n\n >>> @typed_return(int, lambda x: x > 0)\n ... def func1(x):\n ... return x + 10\n ...\n >>>\n >>> func1(10)\n 20\n >>> func1(-100)\n Traceback (most recent call last):\n ...\n relaxed_types.ReturnTypeError: Type mismatch for -90, expected . Outer value: -90\n\n\n\n >>> @typed_return([int], lambda x: len(x) > 0)\n ... def func1(x):\n ... return x\n ...\n >>>\n >>> func1([1, 2])\n [1, 2]\n >>> func1([])\n Traceback (most recent call last):\n ...\n relaxed_types.ReturnTypeError: Type mismatch for [], expected []. Outer value: []\n\n\n >>> @typed_return([ {\"name\": lambda x: x.upper() == x} ])\n ... def func2(x):\n ... return x\n ...\n >>>\n >>> func2([{\"name\": \"JOHN DOE\"}])\n [{'name': 'JOHN DOE'}]\n >>> func2([{\"name\": \"test\"}])\n Traceback (most recent call last):\n ...\n relaxed_types.ReturnTypeError: Type mismatch for 'test', expected at 0x10e325758>. Outer value: [{'name': 'test'}]\n\n\n >>> @typed_return([{\"data\": Any, \"id\": And(int, lambda x: x > 0)}])\n ... def func3(x):\n ... return x\n ...\n >>> func3([{\"data\": \"price=10\", \"id\": 1}])\n [{'data': 'price=10', 'id': 1}]\n >>> func3([{\"data\": 10, \"id\": 2}])\n [{'data': 10, 'id': 2}]\n >>> func3([{\"data\": {\"price\": 10}, \"id\": 2}])\n [{'data': {'price': 10}, 'id': 2}]\n\n\n.. _voluptuous: https://github.com/alecthomas/voluptuous", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Yipit/relaxed_types", "keywords": null, "license": "UNKNOWN", "maintainer": null, "maintainer_email": null, "name": "relaxed_types", "package_url": "https://pypi.org/project/relaxed_types/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/relaxed_types/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/Yipit/relaxed_types" }, "release_url": "https://pypi.org/project/relaxed_types/1.0.1/", "requires_dist": null, "requires_python": null, "summary": "relaxed_types", "version": "1.0.1" }, "last_serial": 2947756, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "d41989f68342f678cacc5438f0e90524", "sha256": "83f7e8e9d7177cf9017dc95e61963a4ae4ee1205fa3da0021732a236f9a42148" }, "downloads": -1, "filename": "relaxed_types-1.0.0.tar.gz", "has_sig": false, "md5_digest": "d41989f68342f678cacc5438f0e90524", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6585, "upload_time": "2016-07-15T21:05:26", "url": "https://files.pythonhosted.org/packages/c3/3c/8d40aa18986734f48dd5edcecff199ea86c38f3e6210603434aeeb8c2964/relaxed_types-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "6fd5e813930867fecbecec5fc87dab0e", "sha256": "41fce54c6b9ca635e3e958e1a2d8832c819545c48595e35e29f6ca34d6abbad9" }, "downloads": -1, "filename": "relaxed_types-1.0.1.tar.gz", "has_sig": false, "md5_digest": "6fd5e813930867fecbecec5fc87dab0e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6589, "upload_time": "2016-07-19T17:03:13", "url": "https://files.pythonhosted.org/packages/5c/66/172b9d3ce670b743d64be802e00bfa1a00e0ae2564f7290989b29c46c1a2/relaxed_types-1.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "6fd5e813930867fecbecec5fc87dab0e", "sha256": "41fce54c6b9ca635e3e958e1a2d8832c819545c48595e35e29f6ca34d6abbad9" }, "downloads": -1, "filename": "relaxed_types-1.0.1.tar.gz", "has_sig": false, "md5_digest": "6fd5e813930867fecbecec5fc87dab0e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6589, "upload_time": "2016-07-19T17:03:13", "url": "https://files.pythonhosted.org/packages/5c/66/172b9d3ce670b743d64be802e00bfa1a00e0ae2564f7290989b29c46c1a2/relaxed_types-1.0.1.tar.gz" } ] }