{ "info": { "author": "Yahya Abou Imran", "author_email": "yahya-abou-imran@protonmail.com", "bugtrack_url": null, "classifiers": [], "description": "# CheckTypes package\n\nLibrary for creating utility classes giving a nice abstraction for type\nchecking and data validation.\n\n# Basic examples\n\n## Creation\n\n### Object Oriented API\n\nChoose a base class alongside `CheckType` to inherit from and define a `predicate()` staticmethod or classmethod\n\n```python\n>>> from checktypes import CheckType\n>>> class PositiveInteger(int, CheckType):\n... \"'int' > 0\"\n... @staticmethod\n... def predicate(n):\n... return n > 0\n...\n>>> class HumanAge(int, CheckType):\n... \"'int' between 0 and 125\"\n... minval = 0\n... maxval = 125\n... @classmethod\n... def predicate(cls, val):\n... return cls.minval <= val <= cls.maxval\n...\n\n```\n\nAlternatively, you can also use lambdas for shorter class definitions.\n\n```python\n>>> class PositiveInteger(int, CheckType):\n... \"'int' > 0\"\n... predicate = lambda n: n > 0\n...\n>>> class HumanAge(int, CheckType):\n... \"'int' between 0 and 125\"\n... minval = 0\n... maxval = 125\n... predicate = classmethod(lambda cls, n: cls.minval <= n <= cls.maxval)\n...\n\n```\n\n### Functional API\n\nAnother way is to use the `checktype()` factory.\n\n```python\n>>> from checktypes import checktype\n>>> PositiveInteger = checktype('PositiveInteger', int, lambda n: n > 0, \"'int' > 0\")\n>>> HumanAge = checktype(\n... 'HumanAge', int, doc=\"'int' between 0 and 125\", minval=0, maxval=125,\n... predicate=classmethod(lambda cls, n: cls.minval <= n < cls.maxval)\n... )\n...\n\n```\n\n## Usage\n\n### `isinstance()` overload\n\n```python\n>>> isinstance(1, PositiveInteger)\nTrue\n>>> isinstance(-1, PositiveInteger)\nFalse\n>>> isinstance('a', PositiveInteger)\nFalse\n\n```\n\n### `validate()` classmethod\n\n```python\n>>> PositiveInteger.validate(1) # No output => the value is a valid one\n>>> PositiveInteger.validate(-1)\nTraceback (most recent call last):\n ...\nValueError: expected 'PositiveInteger' ('int' > 0) but got -1\n>>> PositiveInteger.validate('a')\nTraceback (most recent call last):\n ...\nTypeError: expected 'PositiveInteger' ('int' > 0) but got 'str'\n\n```\n\n### `register()` classmethod\n\n```python\n>>> isinstance(0, PositiveInteger)\nFalse\n>>> PositiveInteger.validate(0)\nTraceback (most recent call last):\n ...\nValueError: expected 'PositiveInteger' ('int' > 0) but got 0\n>>> PositiveInteger.register(0) # Now let pass 0\n>>> isinstance(0, PositiveInteger)\nTrue\n>>> PositiveInteger.validate(0)\n\n```\n\n### `as_descriptor()` classmethod\n\n```python\n>>> class Circle:\n... radius = PositiveInteger.as_descriptor()\n...\n>>> c = Circle()\n>>> c.radius = 1\n>>> c.radius = -1\nTraceback (most recent call last):\n ...\nValueError: expected 'PositiveInteger' ('int' > 0) but got -1 for 'radius' attribute of 'Circle' object\n>>> c.radius = 'a'\nTraceback (most recent call last):\n ...\nTypeError: expected 'PositiveInteger' ('int' > 0) but got 'str' for 'radius' attribute of 'Circle' object\n\n```\n\n### `checktyped` decorator with type hints (3.6+ style)\n\n```python\n>>> from checktypes import checktyped\n>>> @checktyped\n... class Point2D:\n... x: float\n... y: float\n...\n>>> p = Point2D()\n>>> p.x = 0.0\n>>> p.y = 1.0\n>>> p.x = 'a'\nTraceback (most recent call last):\n ...\nTypeError: expected 'float' but got 'str' for 'x' attribute of 'Point2D' object\n\n```\n\n### Instantiation\n\nBy concept `CheckType`s are not originally meant to be instantiated. But since it's a common task\nto cast a value into another type, support have been added to make the constructor return a value\nwith the same rules as a standard class in python except three things:\n\n#### 1 - The returned value will never be an instance of the class but an instance of one a its bases.\n\n```python\n>>> PositiveInteger(1)\n1\n>>> n = PositiveInteger(1)\n>>> print(n, type(n), sep=': ')\n1: \n\n```\n\n#### 2 - If the value doesn't satisfy the `isinstance()` check, a `ValueError` will be raise.\n\n```python\n>>> PositiveInteger(-1)\nTraceback (most recent call last):\n ...\nValueError: -1 cannot be interpreted as a 'PositiveInteger' ('int' > 0)\n\n```\n\n#### 3 - `__init__()` and `__new__()` are ignored.\n\n\n```python\n>>> class MyInt(int, CheckType):\n... def __new__(cls, x):\n... return 'unexpected thing'\n... def __init__(self, x):\n... self.my_attr = 'some value'\n...\n>>> x = MyInt(1)\n>>> x\n1\n>>> x.my_attr\nTraceback (most recent call last):\n ...\nAttributeError: 'int' object has no attribute 'my_attr'\n\n\n```\n\n#### Still, two class attributes can be supplied to add support for better instantiation:\n\n#### 1 - `default`\n\nIt provides a value to be returned if the class is called without argument. \nOne of its merit: it fixes the problem of unfit default value.\n\n```python\n>>> class NegativeInteger(int, CheckType):\n... default = -1\n... predicate = lambda n: n < 0\n...\n>>> NegativeInteger()\n-1\n>>> del NegativeInteger.default\n>>> NegativeInteger() # int() -> 0\nTraceback (most recent call last):\n ...\nValueError: 0 cannot be interpreted as a 'NegativeInteger'\n\n```\n\n#### 2 - `factory()`\n\nIt is designed to be a callable with the responsibility of returning the new object. \nSpecially useful when inheriting from an ABC.\n\n```python\n>>> from collections.abc import Sized\n>>> class ThreePlace(Sized, CheckType):\n... factory = tuple\n... predicate = lambda s: len(s) == 3\n...\n>>> ThreePlace(range(1, 4))\n(1, 2, 3)\n>>> ThreePlace([4, 5, 6])\n(4, 5, 6)\n>>> ThreePlace('789')\n('7', '8', '9')\n\n```\n\n#### Be aware that the returned value will still be checked.\n\n```python\n>>> def badfactory(*args, **kwarg):\n... return 'bad value'\n...\n>>> ThreePlace.factory = badfactory\n>>> ThreePlace((1, 2, 3))\nTraceback (most recent call last):\n ...\nValueError: 'bad value' cannot be interpreted as a 'ThreePlace'\n>>> del ThreePlace.factory\n>>> ThreePlace.default = 0\n>>> ThreePlace()\nTraceback (most recent call last):\n ...\nTypeError: 'int' object cannot be interpreted as a 'ThreePlace'\n\n```\n\n#### For other examples, see [Recipes.md](https://gitlab.com/yahya-abou-imran/checktypes/blob/master/docs/Recipes.md).", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://gitlab.com/yahya-abou-imran/checktypes", "keywords": "", "license": "GPLv3", "maintainer": "", "maintainer_email": "", "name": "checktypes", "package_url": "https://pypi.org/project/checktypes/", "platform": "", "project_url": "https://pypi.org/project/checktypes/", "project_urls": { "Homepage": "https://gitlab.com/yahya-abou-imran/checktypes" }, "release_url": "https://pypi.org/project/checktypes/0.5.post1/", "requires_dist": null, "requires_python": ">=2.7", "summary": "Library for creating utility classes giving a nice abstraction for type checking and data validation", "version": "0.5.post1" }, "last_serial": 5193524, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "3eb80617f46ee49efcc38c2aa0b570ce", "sha256": "8a564a79ab5b66da020fb040d05237ca68daaec717354fed14b62d8f56702146" }, "downloads": -1, "filename": "checktypes-0.1.tar.gz", "has_sig": false, "md5_digest": "3eb80617f46ee49efcc38c2aa0b570ce", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 2607, "upload_time": "2018-02-07T21:01:33", "url": "https://files.pythonhosted.org/packages/d9/f7/443e89205011d7594f695e57480c719e67b93ca7768db31339fe59122121/checktypes-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "b0b8f6fe2b6fa28159d4ed0e1261d86e", "sha256": "14ad00bf8433115a6b39c39eb11ed6602cea7f1376edecf1e69ff9afe4e8e8ae" }, "downloads": -1, "filename": "checktypes-0.2.tar.gz", "has_sig": false, "md5_digest": "b0b8f6fe2b6fa28159d4ed0e1261d86e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6679, "upload_time": "2018-02-08T14:00:19", "url": "https://files.pythonhosted.org/packages/19/d2/6298db5deab3d427114c1cf86622683986209569c9f5ba2cfc395ab2d4e9/checktypes-0.2.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "c418a4c3c90c6dd290035db861471d34", "sha256": "99ac49b393f7b30e926e5117e432e703e4ff4a8bdf7ae3cf40c2589fce04deee" }, "downloads": -1, "filename": "checktypes-0.2.1.tar.gz", "has_sig": false, "md5_digest": "c418a4c3c90c6dd290035db861471d34", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6667, "upload_time": "2018-02-10T14:38:27", "url": "https://files.pythonhosted.org/packages/fb/a8/864e13b4de6016d2302e447fe8c31da6450d1c5a77c70d2c95fe6eb2d87b/checktypes-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "7dd1885496c122543bb6409059423143", "sha256": "1576b6cc8f86174f8cd34bc70b9dcd342879180a3819f5e57939cc6b3c66de92" }, "downloads": -1, "filename": "checktypes-0.2.2.tar.gz", "has_sig": false, "md5_digest": "7dd1885496c122543bb6409059423143", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7096, "upload_time": "2018-02-10T16:42:25", "url": "https://files.pythonhosted.org/packages/f9/2d/da59dcc28f166a2d49014a2cf40c57d039e25acfafbabcc105299c907ab1/checktypes-0.2.2.tar.gz" } ], "0.2.3": [ { "comment_text": "", "digests": { "md5": "acb318f47a52c0309933c1d0f5b0a735", "sha256": "1cfd2db9b8ad9e07bff69c5c9be93fa1d064c1e417572e47eccf4a90ab0bf52c" }, "downloads": -1, "filename": "checktypes-0.2.3.tar.gz", "has_sig": false, "md5_digest": "acb318f47a52c0309933c1d0f5b0a735", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5204, "upload_time": "2018-11-22T15:46:39", "url": "https://files.pythonhosted.org/packages/35/b0/e7b9576bab268204ca627041d6f351b600ecc5ef27d320948360fa996b12/checktypes-0.2.3.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "029f214748ccc095b28c8e2c85e70463", "sha256": "c9c0bceaf0323f5c69c1d5efd389cab2a39a8dd19177d717f63a0abc2458880a" }, "downloads": -1, "filename": "checktypes-0.3.0.tar.gz", "has_sig": false, "md5_digest": "029f214748ccc095b28c8e2c85e70463", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5235, "upload_time": "2018-11-22T18:45:45", "url": "https://files.pythonhosted.org/packages/2a/53/8af0745b8b635a34f8cd20f1dd3a7166f6d835f32052a4c69a96150c7af7/checktypes-0.3.0.tar.gz" } ], "0.4": [ { "comment_text": "", "digests": { "md5": "bf2bae84cb4cd30269b5ffa04df4da84", "sha256": "b7045a915d32fb46de4dc1d60fab7f86316b90aa8a189fd918f0cbddc1e80995" }, "downloads": -1, "filename": "checktypes-0.4.tar.gz", "has_sig": false, "md5_digest": "bf2bae84cb4cd30269b5ffa04df4da84", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 24154, "upload_time": "2018-11-24T16:20:18", "url": "https://files.pythonhosted.org/packages/91/2f/09701320cd7ca270f463189e8f86413fe199ebe54643cc342d7a39772cb7/checktypes-0.4.tar.gz" } ], "0.4.post1": [ { "comment_text": "", "digests": { "md5": "9f88fb00a947ffddb76b6bb0fa7c3453", "sha256": "cf85e9cfc0d85bf73f86e19850b969f8f85a2e16dd0e32ca7b22e578dfffe6d8" }, "downloads": -1, "filename": "checktypes-0.4.post1.tar.gz", "has_sig": false, "md5_digest": "9f88fb00a947ffddb76b6bb0fa7c3453", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 24220, "upload_time": "2018-11-24T16:46:37", "url": "https://files.pythonhosted.org/packages/35/ac/0a614a49ed3f420dd85317462eca5fc549fe9e5fb415edbb6016d3939d1e/checktypes-0.4.post1.tar.gz" } ], "0.4rc1": [ { "comment_text": "", "digests": { "md5": "e8d9ae4e72017a0f769f3422e81026e6", "sha256": "7a79fbd71c4522e98ca37f6962615bf0457156ac39e0f21113a96c2f637cc023" }, "downloads": -1, "filename": "checktypes-0.4rc1.tar.gz", "has_sig": false, "md5_digest": "e8d9ae4e72017a0f769f3422e81026e6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 19747, "upload_time": "2018-11-24T13:58:28", "url": "https://files.pythonhosted.org/packages/d4/7f/d229e5ca76741bc41ab694a0cd0256eeae1e2f4608389ec6160b4aea02af/checktypes-0.4rc1.tar.gz" } ], "0.4rc2": [ { "comment_text": "", "digests": { "md5": "b759f05594ea3d99623e68c41f80b4f1", "sha256": "e472d35f7c51a664645e927f486d5e2500bf558ff286d1799f357c8e1cf45c7d" }, "downloads": -1, "filename": "checktypes-0.4rc2.tar.gz", "has_sig": false, "md5_digest": "b759f05594ea3d99623e68c41f80b4f1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 24162, "upload_time": "2018-11-24T16:10:11", "url": "https://files.pythonhosted.org/packages/08/9c/c91c44ec3a2b2333226be9b55101147568f478d5020ce61f22444e71e5ca/checktypes-0.4rc2.tar.gz" } ], "0.5": [ { "comment_text": "", "digests": { "md5": "e1cb50484024881edc91d4d82e23de4a", "sha256": "d9a55fde79de62018af8e26bc7e02e22a0092f32400c92d80ac4feb98367d1d6" }, "downloads": -1, "filename": "checktypes-0.5.tar.gz", "has_sig": false, "md5_digest": "e1cb50484024881edc91d4d82e23de4a", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 25050, "upload_time": "2019-04-26T14:51:03", "url": "https://files.pythonhosted.org/packages/cb/8e/d6adfd2269c5909abc8bac8ad4fb6572770c18aa182a030d051b676d6b01/checktypes-0.5.tar.gz" } ], "0.5.post1": [ { "comment_text": "", "digests": { "md5": "1919b4fee8de279bdccfe6803ea442f3", "sha256": "6ecf8501ef71d8c3e54651ef2bba4d14c052b486299f13d967349d0e63816316" }, "downloads": -1, "filename": "checktypes-0.5.post1.tar.gz", "has_sig": false, "md5_digest": "1919b4fee8de279bdccfe6803ea442f3", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 25130, "upload_time": "2019-04-26T15:13:58", "url": "https://files.pythonhosted.org/packages/c8/56/133422a4f9af930f519fae613f6674ca7d0f6ac67b8300c78866925d9e8c/checktypes-0.5.post1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "1919b4fee8de279bdccfe6803ea442f3", "sha256": "6ecf8501ef71d8c3e54651ef2bba4d14c052b486299f13d967349d0e63816316" }, "downloads": -1, "filename": "checktypes-0.5.post1.tar.gz", "has_sig": false, "md5_digest": "1919b4fee8de279bdccfe6803ea442f3", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 25130, "upload_time": "2019-04-26T15:13:58", "url": "https://files.pythonhosted.org/packages/c8/56/133422a4f9af930f519fae613f6674ca7d0f6ac67b8300c78866925d9e8c/checktypes-0.5.post1.tar.gz" } ] }