PyContracts-mirror


NamePyContracts-mirror JSON
Version 2.0.1.17 PyPI version JSON
download
home_page
SummaryINCOMPATIBLE FORK OF CURRENT PYCONTRACTS ON GITHUBThe version on pypi is stale, this is a fork that is updated from github andwith added support for jax. No guarantee that this will be kept up to date, or developed furtheror kept compatible with the original.See the original repo here: http://andreacensi.github.com/contracts/
upload_time2023-04-24 21:17:36
maintainer
docs_urlNone
authorxivarri
requires_python
licenseLGPL
keywords type checking value checking contracts
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            .. image:: https://circleci.com/gh/AndreaCensi/contracts.svg?style=svg
    :target: https://circleci.com/gh/AndreaCensi/contracts

PyContracts is a Python package that allows to declare constraints on function parameters and
return values. It supports a basic type system, variables binding, arithmetic constraints, and
has several specialized contracts (notably for Numpy arrays). 


As a quick intro, please see `this presentation about PyContracts`_.

.. _`this presentation about PyContracts`: http://censi.mit.edu/pub/research/201410-pycontracts/201410-pycontracts.pdf 

.. image:: http://censi.mit.edu/pub/research/201410-pycontracts/201410-pycontracts.border.png
   :height: 100px
   :target: http://censi.mit.edu/pub/research/201410-pycontracts/201410-pycontracts.pdf 
   :alt: A presentation about PyContracts



.. container:: brief_summary
  
    A brief summary follows. See the full documentation at: <http://andreacensi.github.com/contracts/>


**Why**: The purpose of PyContracts is **not** to turn Python into a statically-typed language
(albeit you can be as strict as you wish), but, rather, to avoid the time-consuming and
obfuscating checking of various preconditions. In fact, more than the type constraints, I found
useful the ability to impose value and size constraints. For example, "I need a list of at least
3 positive numbers" can be expressed as ``list[>=3](number, >0))``. If you find that
PyContracts is overkill for you, you might want to try a simpler alternative, such as
typecheck_. If you find that PyContracts is not *enough* for you, you probably want to be
using Haskell_ instead of Python.

**Specifying contracts**: Contracts can be specified in three ways:

1. **Using the ``@contract`` decorator**: ::
   
      @contract(a='int,>0', b='list[N],N>0', returns='list[N]')
      def my_function(a, b):
          ...

2. **Using annotations** (for Python 3): :: 
  
      @contract
      def my_function(a : 'int,>0', b : 'list[N],N>0') -> 'list[N]': 
           # Requires b to be a nonempty list, and the return 
           # value to have the same length.
           ...
      
3. **Using docstrings**, with the ``:type:`` and ``:rtype:`` tags: ::
   
      @contract
      def my_function(a, b): 
          """ Function description.
              :type a: int,>0
              :type b: list[N],N>0
              :rtype: list[N]
          """
          ...
          
..
   In any case, PyContracts will include the spec in the ``__doc__`` attribute.

**Deployment**: In production, all checks can be disabled using the function ``contracts.disable_all()``, so the performance hit is 0.

**Extensions:** You can extend PyContracts with new contracts types: ::

    new_contract('valid_name', lambda s: isinstance(s, str) and len(s)>0)
    @contract(names='dict(int: (valid_name, int))')
    def process_accounting(records):
        ...

Any Python type is a contract: ::

    @contract(a=int, # simple contract
              b='int,>0' # more complicated
              )
    def f(a, b):
        ...

**Enforcing interfaces**:  ``ContractsMeta`` is a metaclass,
like ABCMeta, which propagates contracts to the subclasses: ::

    from contracts import contract, ContractsMeta, with_metaclass
    
    class Base(with_metaclass(ContractsMeta, object)):

        @abstractmethod
        @contract(probability='float,>=0,<=1')
        def sample(self, probability):
            pass

    class Derived(Base):
        # The contract above is automatically enforced, 
        # without this class having to know about PyContracts at all!
        def sample(self, probability):
            ....

**Numpy**: There is special support for Numpy: ::

    @contract(image='array[HxWx3](uint8),H>10,W>10')
    def recolor(image):
        ...

**Status:** The syntax is stable and it won't be changed. PyContracts is very well tested on Python 2.x. 

**Status on Python 3.x:** We reached feature parity! Everything works on Python 3 now.

**Contributors**:

- `Chris Beaumont`_ (Harvard-Smithsonian Center for Astrophysics): ``$var`` syntax; kwargs/args for extensions.
- `Brett Graham`_ (Rowland Institute at Harvard University):  ``attr(name:type)`` syntax for checking types of attributes.
- `William Furr`_: bug reports and performance improvements
- `Karol Kuczmarski`_ (Google Zurich):  implementation of "string" and "unicode" contracts
- `Maarten Derickx`_ (Leiden U.):  documentation fixes
- `Calen Pennington`_ (EdX):  disabling checks inside check() function.
- `Adam Palay`_ (EdX): implementation of environment variable enabling/disabling override.
- `Ryan Heimbuch`_:  bug reports 
- Bernhard Biskup:  bug reports
- `asharp`_: bug fixes
- `Dennis Kempin`_ (Google mothership): Sphinx-style constraints specs
- `Andy Hayden`_: Python 3 support, more efficient Numpy checks
- `Jonathan Sharpe`_: contracts for file-like objects, not operator

(Please let me know if I forgot anybody.)

.. _`Jonathan Sharpe`: http://jonathansharpe.me.uk/

.. _`Chris Beaumont`: http://chrisbeaumont.org/
.. _`asharp`:  https://github.com/asharp
.. _`Maarten Derickx`: http://mderickx.nl/
.. _`Ryan Heimbuch`: https://github.com/ryanheimbuch-wf
.. _`Calen Pennington`: https://github.com/cpennington
.. _`Adam Palay`: https://github.com/adampalay
.. _`William Furr`: http://www.ccs.neu.edu/home/furrwf/
.. _`Karol Kuczmarski`:  http://xion.org.pl/
.. _`Brett Graham`: https://github.com/braingram
.. _`Dennis Kempin`: https://github.com/denniskempin
.. _`Andy Hayden`: http://careers.stackoverflow.com/hayd

.. _typecheck: http://oakwinter.com/code/typecheck/
.. _Haskell: http://www.haskell.org/



            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "PyContracts-mirror",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "type checking,value checking,contracts",
    "author": "xivarri",
    "author_email": "xamvolagis@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ac/09/5907342ff5bf85e27c597aa647248c9dc7b0e1ce9a3cd505d9a43fddcaae/PyContracts-mirror-2.0.1.17.tar.gz",
    "platform": null,
    "description": ".. image:: https://circleci.com/gh/AndreaCensi/contracts.svg?style=svg\n    :target: https://circleci.com/gh/AndreaCensi/contracts\n\nPyContracts is a Python package that allows to declare constraints on function parameters and\nreturn values. It supports a basic type system, variables binding, arithmetic constraints, and\nhas several specialized contracts (notably for Numpy arrays). \n\n\nAs a quick intro, please see `this presentation about PyContracts`_.\n\n.. _`this presentation about PyContracts`: http://censi.mit.edu/pub/research/201410-pycontracts/201410-pycontracts.pdf \n\n.. image:: http://censi.mit.edu/pub/research/201410-pycontracts/201410-pycontracts.border.png\n   :height: 100px\n   :target: http://censi.mit.edu/pub/research/201410-pycontracts/201410-pycontracts.pdf \n   :alt: A presentation about PyContracts\n\n\n\n.. container:: brief_summary\n  \n    A brief summary follows. See the full documentation at: <http://andreacensi.github.com/contracts/>\n\n\n**Why**: The purpose of PyContracts is **not** to turn Python into a statically-typed language\n(albeit you can be as strict as you wish), but, rather, to avoid the time-consuming and\nobfuscating checking of various preconditions. In fact, more than the type constraints, I found\nuseful the ability to impose value and size constraints. For example, \"I need a list of at least\n3 positive numbers\" can be expressed as ``list[>=3](number, >0))``. If you find that\nPyContracts is overkill for you, you might want to try a simpler alternative, such as\ntypecheck_. If you find that PyContracts is not *enough* for you, you probably want to be\nusing Haskell_ instead of Python.\n\n**Specifying contracts**: Contracts can be specified in three ways:\n\n1. **Using the ``@contract`` decorator**: ::\n   \n      @contract(a='int,>0', b='list[N],N>0', returns='list[N]')\n      def my_function(a, b):\n          ...\n\n2. **Using annotations** (for Python 3): :: \n  \n      @contract\n      def my_function(a : 'int,>0', b : 'list[N],N>0') -> 'list[N]': \n           # Requires b to be a nonempty list, and the return \n           # value to have the same length.\n           ...\n      \n3. **Using docstrings**, with the ``:type:`` and ``:rtype:`` tags: ::\n   \n      @contract\n      def my_function(a, b): \n          \"\"\" Function description.\n              :type a: int,>0\n              :type b: list[N],N>0\n              :rtype: list[N]\n          \"\"\"\n          ...\n          \n..\n   In any case, PyContracts will include the spec in the ``__doc__`` attribute.\n\n**Deployment**: In production, all checks can be disabled using the function ``contracts.disable_all()``, so the performance hit is 0.\n\n**Extensions:** You can extend PyContracts with new contracts types: ::\n\n    new_contract('valid_name', lambda s: isinstance(s, str) and len(s)>0)\n    @contract(names='dict(int: (valid_name, int))')\n    def process_accounting(records):\n        ...\n\nAny Python type is a contract: ::\n\n    @contract(a=int, # simple contract\n              b='int,>0' # more complicated\n              )\n    def f(a, b):\n        ...\n\n**Enforcing interfaces**:  ``ContractsMeta`` is a metaclass,\nlike ABCMeta, which propagates contracts to the subclasses: ::\n\n    from contracts import contract, ContractsMeta, with_metaclass\n    \n    class Base(with_metaclass(ContractsMeta, object)):\n\n        @abstractmethod\n        @contract(probability='float,>=0,<=1')\n        def sample(self, probability):\n            pass\n\n    class Derived(Base):\n        # The contract above is automatically enforced, \n        # without this class having to know about PyContracts at all!\n        def sample(self, probability):\n            ....\n\n**Numpy**: There is special support for Numpy: ::\n\n    @contract(image='array[HxWx3](uint8),H>10,W>10')\n    def recolor(image):\n        ...\n\n**Status:** The syntax is stable and it won't be changed. PyContracts is very well tested on Python 2.x. \n\n**Status on Python 3.x:** We reached feature parity! Everything works on Python 3 now.\n\n**Contributors**:\n\n- `Chris Beaumont`_ (Harvard-Smithsonian Center for Astrophysics): ``$var`` syntax; kwargs/args for extensions.\n- `Brett Graham`_ (Rowland Institute at Harvard University):  ``attr(name:type)`` syntax for checking types of attributes.\n- `William Furr`_: bug reports and performance improvements\n- `Karol Kuczmarski`_ (Google Zurich):  implementation of \"string\" and \"unicode\" contracts\n- `Maarten Derickx`_ (Leiden U.):  documentation fixes\n- `Calen Pennington`_ (EdX):  disabling checks inside check() function.\n- `Adam Palay`_ (EdX): implementation of environment variable enabling/disabling override.\n- `Ryan Heimbuch`_:  bug reports \n- Bernhard Biskup:  bug reports\n- `asharp`_: bug fixes\n- `Dennis Kempin`_ (Google mothership): Sphinx-style constraints specs\n- `Andy Hayden`_: Python 3 support, more efficient Numpy checks\n- `Jonathan Sharpe`_: contracts for file-like objects, not operator\n\n(Please let me know if I forgot anybody.)\n\n.. _`Jonathan Sharpe`: http://jonathansharpe.me.uk/\n\n.. _`Chris Beaumont`: http://chrisbeaumont.org/\n.. _`asharp`:  https://github.com/asharp\n.. _`Maarten Derickx`: http://mderickx.nl/\n.. _`Ryan Heimbuch`: https://github.com/ryanheimbuch-wf\n.. _`Calen Pennington`: https://github.com/cpennington\n.. _`Adam Palay`: https://github.com/adampalay\n.. _`William Furr`: http://www.ccs.neu.edu/home/furrwf/\n.. _`Karol Kuczmarski`:  http://xion.org.pl/\n.. _`Brett Graham`: https://github.com/braingram\n.. _`Dennis Kempin`: https://github.com/denniskempin\n.. _`Andy Hayden`: http://careers.stackoverflow.com/hayd\n\n.. _typecheck: http://oakwinter.com/code/typecheck/\n.. _Haskell: http://www.haskell.org/\n\n\n",
    "bugtrack_url": null,
    "license": "LGPL",
    "summary": "INCOMPATIBLE FORK OF CURRENT PYCONTRACTS ON GITHUBThe version on pypi is stale, this is a fork that is updated from github andwith added support for jax. No guarantee that this will be kept up to date, or developed furtheror kept compatible with the original.See the original repo here: http://andreacensi.github.com/contracts/",
    "version": "2.0.1.17",
    "split_keywords": [
        "type checking",
        "value checking",
        "contracts"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "51d27bd8b3ba90d97448354a66f2d4b9a57a1194262a210fe281a3f90091fe8d",
                "md5": "b4ff66d90a709fe96a7ec9daa1ba8af2",
                "sha256": "8e5221638a20c69b177364bc76dd8590025756b2b8c620936ae4e933ed404539"
            },
            "downloads": -1,
            "filename": "PyContracts_mirror-2.0.1.17-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b4ff66d90a709fe96a7ec9daa1ba8af2",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 93451,
            "upload_time": "2023-04-24T21:17:33",
            "upload_time_iso_8601": "2023-04-24T21:17:33.943854Z",
            "url": "https://files.pythonhosted.org/packages/51/d2/7bd8b3ba90d97448354a66f2d4b9a57a1194262a210fe281a3f90091fe8d/PyContracts_mirror-2.0.1.17-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ac095907342ff5bf85e27c597aa647248c9dc7b0e1ce9a3cd505d9a43fddcaae",
                "md5": "013f1fac0ba4f9ea296fb115a22d5187",
                "sha256": "f6b1cb67c4c7a505adef9336d3298c2317706be88122b5d70cf29e6a6abb071a"
            },
            "downloads": -1,
            "filename": "PyContracts-mirror-2.0.1.17.tar.gz",
            "has_sig": false,
            "md5_digest": "013f1fac0ba4f9ea296fb115a22d5187",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 67622,
            "upload_time": "2023-04-24T21:17:36",
            "upload_time_iso_8601": "2023-04-24T21:17:36.436274Z",
            "url": "https://files.pythonhosted.org/packages/ac/09/5907342ff5bf85e27c597aa647248c9dc7b0e1ce9a3cd505d9a43fddcaae/PyContracts-mirror-2.0.1.17.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-24 21:17:36",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "pycontracts-mirror"
}
        
Elapsed time: 0.07762s