{ "info": { "author": "Timothy Moore", "author_email": "mtimothy984@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Topic :: Utilities" ], "description": "# PyTypeUtils\n\nA runtime type-checking library which is not based on annotations and has\nsupport for numpy and scipy.\n\n## Features\n\nThis has documentation, unit tests for all API functions, and readable error\nmessages. This detects numpy and torch and, if they exist, provides\nadditional functions for each.\n\n## Installation\n\n`pip install pytypeutils`\n\n## Usage\n\nAs a precondition test:\n\n```py\nimport pytypeutils as tus\nimport typing\n\ndef foo(a: int, b: typing.Union[int, float]):\n tus.check(a=(a, int), b=(b, (int, float)))\n return 7\n\nfoo(3, 3) # no error!\nfoo(3, 3.0) # no error!\nfoo(3.0, 3) # ValueError: expected a is but got 3.0 (type(a)=)\n```\n\nInside unit tests:\n\n```py\nimport unittest\nimport pytypeutils as tus\n\ndef foo(a: int):\n return 2 * a\n\nclass MyTester(unittest.TestCase):\n def test_foo_giveint_returnsint(self):\n res = foo(2)\n tus.check(res=(res, int))\n```\n\nThis detects if numpy is installed and adds `pytypeutils.check_ndarrays` if\nit is:\n\n```py\nimport pytypeutils as tus\nimport typing\nimport numpy as np\n\ndef feature_means(pts: np.ndarray):\n \"\"\"Returns the mean of the features for the given points,\n where pts is in the shape (num_samples, num_features)\"\"\"\n tus.check_ndarrays(\n pts=(pts, ('num_samples', 'num_features'), ('float32', 'float64')))\n res = pts.mean(0)\n tus.check_ndarrays(\n res=(res, [('num_features', pts.shape[1])], pts.dtype)\n )\n return res\n\nfeature_means(np.random.uniform(0, 1, (10, 4))) # works\n\n\nfeature_means(np.random.uniform(0, 1, (10, 4, 2)))\n# ValueError: expected pts.shape is (num_samples=any, num_features=any)\n# but has shape (10, 4, 2)\n```\n\nThis detects if torch is installed and adds `pytypeutils.check_tensors` if\nit is:\n\n```py\nimport pytypeutils as tus\nimport typing\nimport torch\n\ndef feature_means(pts: torch.tensor):\n '''Returns the mean of the features for the given points,\n where pts is in the shape (num_samples, num_features)'''\n tus.check_tensors(\n pts=(pts, ('num_samples', 'num_features'), ('float32', 'float64')))\n res = pts.mean(0)\n tus.check_tensors(\n res=(res, [('num_features', pts.shape[1])], pts.dtype)\n )\n return res\n\nfeature_means(torch.randn(10, 4)) # works\n\nfeature_means(torch.randn(10, 4, 2))\n# ValueError: expected pts.shape is (num_samples=any, num_features=any)\n# but has shape (10, 4, 2)\n```\n\n## Why this type library?\n\nAnnotation type libraries, such as [pytypes](https://pypi.org/project/pytypes/) and\n[typeguard](https://github.com/agronholm/typeguard) can be great if it's reasonable\nto express things in terms of typing hints. However, this can often be tedious and\nit doesn't translate well to unit tests, quick snippets, or for a quick debug\ncommand in the middle of a function call. This library lets you get reasonable type\nerrors which you can write and optionally remove quickly, without having to do\ntype checking on an entire functions parameters. Furthermore, the built-in extensions\nfor numpy and pytorch reduce a tremendous amount of boilerplate if you are using\nthose libraries and want some sanity checks.\n\n## API\n\n```Text\n>>> help(pytypeutils)\nHelp on package pytypeutils:\n\nNAME\n pytypeutils - Various generic functions and imports for pytypeutils.\n\nPACKAGE CONTENTS\n np_checks\n torch_checks\n utils\n\nFUNCTIONS\n check(**kwargs)\n Each keyword argument corresponds to an argument whose type will be\n checked. The key is used for printing out error messages and the value\n is a tuple describing the object to be checked and how it is to be checked.\n\n Example:\n\n ```\n import pytypeutils as tus\n import typing\n\n def foo(a: int, b: typing.Union[int, float]):\n tus.check(a=(a, int), b=(b, (int, float)))\n ```\n\n check_callable(**kwargs)\n Verifies that every value is callable. If not, raises an error\n\n check_listlike(**kwargs)\n Verifies that the given list-like objects have contents of a particular\n type. The keys are used for error messages, the values should be a tuple\n of the form (list, content types, optional length or (minl, maxl)).\n\n If there are more than 100 elements in any of the lists, 100 elements are\n sampled at random.\n\n Example:\n\n import pytypeutils as tus\n import typing\n\n def foo(a: typing.List[str], b: typing.List[typing.Union[int, float]]):\n tus.check(a=(a, (tuple, list)), b=(b, (tuple, list)))\n tus.check_listlike(a=(a, str), b=(b, (int, float), (0, 5)))\n # all elements of a are str\n # b has 0-5 elements inclusive, each of which is an int or float\n```\n\n```\n>>> help(pytypeutils.check_ndarrays)\nHelp on function check_ndarrays in module pytypeutils.np_checks:\n\ncheck_ndarrays(**kwargs)\n Checks to verify the given arguments are numpy arrays with the given\n specifications. The keys are used for error messages and the values are\n tuples of the form (arr, expected shape, expected dtype). The expected\n shape may be None not to check shape information, or a tuple of dimension\n descriptions which can be None (for an any-size dimension), a str (for\n an any-size dimension with a name for error messages), an int (for a\n dimension of a particular size), or a tuple (str, int) where the str is\n the name of the dimension and the int is the size of the dimension. The\n dtype may be a tuple of numpy datatypes as strings or types, or None for\n any dtype.\n\n Example:\n\n\n import pytypeutils as tus\n import typing\n import numpy as np\n\n def feature_means(pts: np.ndarray):\n '''Returns the mean of the features for the given points,\n where pts is in the shape (num_samples, num_features)'''\n tus.check_ndarrays(\n pts=(pts, ('num_samples', 'num_features'), ('float32', 'float64')))\n res = pts.mean(0)\n tus.check_ndarrays(\n res=(res, [('num_features', pts.shape[1])], pts.dtype)\n )\n return res\n```\n\n```\n>>> help(pytypeutils.check_tensors)\nHelp on function check_tensors in module pytypeutils.torch_checks:\n\ncheck_tensors(**kwargs)\n Checks to verify the given arguments are torch tensors with the given\n specifications. The keys are used for error messages and the values are\n tuples of the form (arr, expected shape, expected dtype). The expected\n shape may be None not to check shape information, or a tuple of dimension\n descriptions which can be None (for an any-size dimension), a str (for\n an any-size dimension with a name for error messages), an int (for a\n dimension of a particular size), or a tuple (str, int) where the str is\n the name of the dimension and the int is the size of the dimension. The\n dtype may be a tuple of numpy datatypes as strings or types, or None for\n any dtype.\n\n Example:\n\n import pytypeutils as tus\n import typing\n import torch\n\n def feature_means(pts: torch.tensor):\n '''Returns the mean of the features for the given points,\n where pts is in the shape (num_samples, num_features)'''\n tus.check_tensors(\n pts=(pts, ('num_samples', 'num_features'), ('float32', 'float64')))\n res = pts.mean(0)\n tus.check_tensors(\n res=(res, [('num_features', pts.shape[1])], pts.dtype)\n )\n return res\n\n```\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/tjstretchalot/pytypeutils", "keywords": "pytypeutils typechecking", "license": "CC0", "maintainer": "", "maintainer_email": "", "name": "pytypeutils", "package_url": "https://pypi.org/project/pytypeutils/", "platform": "", "project_url": "https://pypi.org/project/pytypeutils/", "project_urls": { "Homepage": "https://github.com/tjstretchalot/pytypeutils" }, "release_url": "https://pypi.org/project/pytypeutils/0.0.1/", "requires_dist": [ "numpy ; extra == 'test'", "torch ; extra == 'test'" ], "requires_python": ">=3.6", "summary": "Runtime typechecking without annotations", "version": "0.0.1" }, "last_serial": 5574441, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "b75aded1442c931d11355431e5f893bd", "sha256": "7fba56d0c540e2843f73a08f049a1889e730843597b876f383080403c9777b1c" }, "downloads": -1, "filename": "pytypeutils-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "b75aded1442c931d11355431e5f893bd", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6", "size": 10695, "upload_time": "2019-07-23T20:55:07", "url": "https://files.pythonhosted.org/packages/d7/d3/13ad18e458a5f36a10accae9967ffa68ea4e964b0914767eb503a34eaf97/pytypeutils-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "43bd53c90cd01b3693559114d2029ffc", "sha256": "1701ac1d5404bf877fc1e9634bbcaaec7b0fe25f1295b2ac5d02ec19f8fe84d6" }, "downloads": -1, "filename": "pytypeutils-0.0.1.tar.gz", "has_sig": false, "md5_digest": "43bd53c90cd01b3693559114d2029ffc", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 6601, "upload_time": "2019-07-23T20:55:09", "url": "https://files.pythonhosted.org/packages/bd/59/6ce6815b9d20aa62498a177c06ffe239874f6b7ab43dff3890bccdfae91b/pytypeutils-0.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b75aded1442c931d11355431e5f893bd", "sha256": "7fba56d0c540e2843f73a08f049a1889e730843597b876f383080403c9777b1c" }, "downloads": -1, "filename": "pytypeutils-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "b75aded1442c931d11355431e5f893bd", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6", "size": 10695, "upload_time": "2019-07-23T20:55:07", "url": "https://files.pythonhosted.org/packages/d7/d3/13ad18e458a5f36a10accae9967ffa68ea4e964b0914767eb503a34eaf97/pytypeutils-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "43bd53c90cd01b3693559114d2029ffc", "sha256": "1701ac1d5404bf877fc1e9634bbcaaec7b0fe25f1295b2ac5d02ec19f8fe84d6" }, "downloads": -1, "filename": "pytypeutils-0.0.1.tar.gz", "has_sig": false, "md5_digest": "43bd53c90cd01b3693559114d2029ffc", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 6601, "upload_time": "2019-07-23T20:55:09", "url": "https://files.pythonhosted.org/packages/bd/59/6ce6815b9d20aa62498a177c06ffe239874f6b7ab43dff3890bccdfae91b/pytypeutils-0.0.1.tar.gz" } ] }