{ "info": { "author": "Manuel Cer\u00f3n", "author_email": "ceronman@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: Implementation :: CPython", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "Type annotations for Python\n===========================\n\nhttps://github.com/ceronman/typeannotations\n\n\nAbout\n-----\n\nThe ``typeannotations`` module provides a set of tools for type checking and\ntype inference of Python code. It also a provides a set of types useful for\nannotating functions and objects.\n\nThese tools are mainly designed to be used by static analyzers such as linters,\ncode completion libraries and IDEs. Additionally, decorators for making\nrun-time checks are provided. Run-time type checking is not always a good\nidea in Python, but in some cases it can be very useful.\n\n\nRun-time type checking.\n-----------------------\n\nThe ``typechecked`` decorator can be used to check types specified in function\nannotations. For example:\n\n.. code-block:: pycon\n\n >>> @typechecked\n ... def test(a: int) -> int:\n ... return a\n ...\n >>> test(1)\n 1\n >>> test('string')\n Traceback (most recent call last):\n ...\n TypeError: Incorrect type for \"a\"\n\n\nStructural interfaces\n---------------------\n\nThe ``Interface`` class allows you to define interfaces that are checked\ndynamically. You don't have to explicitly indicate when an object or class\nimplements a given ``Interface``. If an object provides the methods and\nattributes specified in the ``Interface``, it's considered a valid\nimplementation.\n\nFor example, let's define a simple interface:\n\n.. code-block:: pycon\n\n >>> class Person(Interface):\n ... name = str\n ... age = int\n ... def say_hello(name: str) -> str:\n ... pass\n\nAny object defining those the ``name``, ``age`` and ``say_hello()`` members is\na valid implementation of that interface. For example:\n\n.. code-block:: pycon\n\n >>> class Developer:\n ... def __init__(self, name, age):\n ... self.name = name\n ... self.age = age\n ... def say_hello(self, name: str) -> str:\n ... return 'hello ' + name\n ...\n >>> isinstance(Developer('bill', 20), Person)\n True\n\nThis also works with built-in types:\n\n.. code-block:: pycon\n\n >>> class IterableWithLen(Interface):\n ... def __iter__():\n ... pass\n ... def __len__():\n ... pass\n ...\n >>> isinstance([], IterableWithLen)\n True\n >>> isinstance({}, IterableWithLen)\n True\n >>> isinstance(1, IterableWithLen)\n False\n\n\nTypedefs\n''''''''\n\nA ``typedef`` is similar to an ``Interface`` except that it defines a single\nfunction signature. This is useful for defining callbacks. For example:\n\n.. code-block:: pycon\n\n >>> @typedef\n ... def callback(event: Event) -> bool:\n ... pass\n ...\n\nThen it's possible to check if a function implements the same signature:\n\n.. code-block:: pycon\n\n >>> def handler(event: MouseEvent) -> bool:\n ... print('click')\n ... return True\n ...\n >>> isinstance(handler, callback)\n True\n >>> isinstance(lambda: True, callback)\n False\n\nNote that ``MouseEvent`` is a subclass of ``Event``.\n\n\nType unions\n-----------\n\nA ``union`` is a collection of types and it's a type itself. An object is an\ninstance of a ``union`` if it's an instance of any of the elements in the union.\nFor example:\n\n.. code-block:: pycon\n\n >>> NumberOrString = union(int, str)\n >>> isinstance(1, NumberOrString)\n True\n >>> isinstance('string', NumberOrString)\n True\n >>> issubclass(int, NumberOrString)\n True\n >>> issubclass(str, NumberOrString)\n True\n\n\nPredicates\n----------\n\nA ``predicate`` is a special type defined by a function that takes an object\nand returns ``True`` or ``False`` indicating if the object implements the type.\nFor example:\n\n.. code-block:: pycon\n\n >>> Positive = predicate(lambda x: x > 0)\n >>> isinstance(1, Positive)\n True\n >>> isinstance(0, Positive)\n False\n\nPredicates can also be defined using a decorator:\n\n.. code-block:: pycon\n\n >>> @predicate\n ... def Even(object):\n ... return object % 2 == 0\n\nPredicates can also be combined using the `&`` operator:\n\n.. code-block:: pycon\n\n >>> EvenAndPositive = Even & Positive\n\nPredicates are useful for defining contracts:\n\n.. code-block:: pycon\n\n >>> Positive = predicate(lambda x: x > 0)\n >>> @typechecked\n ... def sqrt(n: Positive):\n ... ...\n >>> sqrt(-1)\n Traceback (most recent call last):\n ...\n TypeError: Incorrect type for \"n\"\n\n\nThe ``optional`` predicate\n''''''''''''''''''''''''''\n\nThe ``optional`` predicate indicates that the object must be from the given type\nor `None`. For example:\n\n.. code-block:: pycon\n\n >>> isinstance(1, optional(int))\n True\n >>> isinstance(None, optional(int))\n True\n\nAnd checking types at runtime:\n\n.. code-block:: pycon\n\n >>> @typechecked\n ... def greet(name: optional(str) = None):\n ... if name is None:\n ... print('hello stranger')\n ... else:\n ... print('hello {0}'.format(name))\n ...\n >>> greet()\n hello stranger\n >>> greet('bill')\n hello bill\n\n\nThe ``only`` predicate\n''''''''''''''''''''''\n\nThe ``only`` predicate indicates that an object can **only** be of the specified\ntype, and not of any of its super classes. For example:\n\n.. code-block:: pycon\n\n >>> isinstance(True, only(bool))\n True\n >>> isinstance(1, only(bool))\n False\n\nNote that in Python `bool` is a sublcass of `int`.\n\n\nThe ``options`` predicate\n'''''''''''''''''''''''''\n\nThe ``options`` predicate indicates that the value of an object must be one of\nthe given options. For example:\n\n.. code-block:: pycon\n\n >>> FileMode = options('r', 'w', 'a', 'r+', 'w+', 'a+')\n >>> isinstance('w', FileMode)\n True\n >>> isinstance('x', FileMode)\n False\n\nThis is useful when defining a function:\n\n.. code-block:: pycon\n\n >>> @typecheck\n ... def open(filename: str, mode: options('w', 'a')):\n ...\t\t...\n\n\nTo be implemented:\n------------------\n\nCollections definitions:\n''''''''''''''''''''''''\n\n.. code-block:: python\n\n typedict({str: int})\n typeseq([int])\n typeseq(set(int))\n typeseq((int,))\n ...\n\nFunction overloading\n''''''''''''''''''''\n\n.. code-block:: python\n\n @overload\n def isinstance(object, t: type):\n ...\n\n @overload\n def isinstance(object, t: tuple):\n ...\n\nAnnotate existing functions and libraries\n'''''''''''''''''''''''''''''''''''''''''\n\n.. code-block:: python\n\n @annotate('builtins.open')\n def open_annotated(file: str,\n mode: options('r', 'w', 'a', 'r+', 'w+', 'a+'),\n buffering: optional(int)) -> IOBase:\n pass\n\n\nLicense\n-------\n\n| Licensed under the Apache License, Version 2.0 (the \"License\");\n| you may not use this file except in compliance with the License.\n| You may obtain a copy of the License at\n| \n| http://www.apache.org/licenses/LICENSE-2.0\n| \n| Unless required by applicable law or agreed to in writing, software\n| distributed under the License is distributed on an \"AS IS\" BASIS,\n| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\n| either express or implied. See the License for the specific language\n| governing permissions and limitations under the License.", "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/ceronman/typeannotations", "keywords": null, "license": "Apache License", "maintainer": null, "maintainer_email": null, "name": "typeannotations", "package_url": "https://pypi.org/project/typeannotations/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/typeannotations/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/ceronman/typeannotations" }, "release_url": "https://pypi.org/project/typeannotations/1.0.0/", "requires_dist": null, "requires_python": null, "summary": "A library with a set of tools for annotating types in Python code.", "version": "1.0.0" }, "last_serial": 1311924, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "f644f9848eac6a51e53d95da68810cca", "sha256": "e9af2d1a68484371d5216c5c510d924e00132a91058a53b22b0ce5a245a6c00f" }, "downloads": -1, "filename": "typeannotations-0.1.0.tar.gz", "has_sig": false, "md5_digest": "f644f9848eac6a51e53d95da68810cca", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8093, "upload_time": "2014-11-18T20:24:37", "url": "https://files.pythonhosted.org/packages/03/e2/2b2137dbed447174108cf9682e00cf246c8363a898df45eb0973adabb521/typeannotations-0.1.0.tar.gz" } ], "1.0.0": [] }, "urls": [] }