From 8bcc738e0d8743a856ae7c8ae06210775401b509 Mon Sep 17 00:00:00 2001 From: slorg1 Date: Wed, 10 Apr 2019 09:33:32 -0400 Subject: [PATCH 1/5] + add attr & pycontracts link --- requirements.txt | 1 + setup.py | 8 +-- src/contracts/integrations/AttrValidator.py | 58 +++++++++++++++++++++ src/contracts/integrations/__init__.py | 1 + 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/contracts/integrations/AttrValidator.py create mode 100644 src/contracts/integrations/__init__.py diff --git a/requirements.txt b/requirements.txt index 2b3ce8f1..e505de37 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pyparsing==2.1.5 decorator==4.0.10 six==1.10.0 +backports.functools-lru-cache==1.5 \ No newline at end of file diff --git a/setup.py b/setup.py index d536b8b5..65925470 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,6 @@ import os from setuptools import setup, find_packages - - description = ( 'PyContracts is a Python package that allows to declare ' 'constraints on function parameters and return values. ' @@ -12,9 +10,11 @@ 'arithmetic constraints, and has several specialized ' 'contracts (notably for Numpy arrays), as well as an extension API.') + def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() + long_description = read('README.rst') @@ -32,8 +32,8 @@ def get_version(filename): raise ValueError(filename) return version -version = get_version(filename='src/contracts/__init__.py') +version = get_version(filename='src/contracts/__init__.py') setup(name='PyContracts', author="Andrea Censi", @@ -59,7 +59,7 @@ def get_version(filename): package_dir={'':'src'}, packages=find_packages('src'), - install_requires=['pyparsing', 'decorator', 'six'], + install_requires=['pyparsing', 'decorator', 'six', 'backports.functools-lru-cache'], tests_require=['nose'], entry_points={}, ) diff --git a/src/contracts/integrations/AttrValidator.py b/src/contracts/integrations/AttrValidator.py new file mode 100644 index 00000000..9673bfaa --- /dev/null +++ b/src/contracts/integrations/AttrValidator.py @@ -0,0 +1,58 @@ +import functools +try: + # python 3.5+ + from functools import lru_cache +except ImportError: + # python 2 + from backports.functools_lru_cache import lru_cache + +try: + # python 3.3+ + + object.__qualname__ + + import operator + qualname = operator.attrgetter("__qualname__") +except AttributeError: + # python 2 + from qualname import qualname + +import attr # attr is only a dependency of pycontracts if this module is imported. Otherwise, it does not depend on it. +from contracts.interface import ContractNotRespected +from contracts.main import new_contract + + +@attr.s(repr=False, slots=True, hash=True) +class AttrValidator(object): + ''' + Validator linking C{pycontracts} with C{attr}'s validators + ''' + + def __call__(self, inst, attr, value): + """ + Executes the validator by checking it against the contract it was configured with. + """ + try: + AttrValidator.__get_contract(inst.__class__, attr.name, self.contract).check( + value, + ) + except ContractNotRespected as e: + # adding formatted debug information to the error + e.error = "{!r}.{}\n{}".format(inst, attr.name, e.error) + raise e + + def __repr__(self): + return ( + "" + .format(contract=self.contract) + ) + + @staticmethod + @lru_cache(typed=True) + def __get_contract(inst_class, name, contract): + # compiling the contract takes time, we want to cache active contracts as they come up + return new_contract("{}___{}".format(qualname(inst_class).replace('.', '___'), name), contract,) + + contract = attr.ib() + """ Contract honoured by the given validator""" + diff --git a/src/contracts/integrations/__init__.py b/src/contracts/integrations/__init__.py new file mode 100644 index 00000000..0ff63401 --- /dev/null +++ b/src/contracts/integrations/__init__.py @@ -0,0 +1 @@ +# This space for rent From f8f7cdedaccfbe409ae309e5cf9d75bb20e9c6c6 Mon Sep 17 00:00:00 2001 From: slorg1 Date: Wed, 10 Apr 2019 12:04:37 -0400 Subject: [PATCH 2/5] + bump version of the lib after getting upstream's master --- src/contracts/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/contracts/__init__.py b/src/contracts/__init__.py index eea09362..813e7a76 100644 --- a/src/contracts/__init__.py +++ b/src/contracts/__init__.py @@ -1,4 +1,4 @@ -__version__ = '1.8.12' +__version__ = '1.8.13' import logging From a77ca5787ac598ca420e182888cfd8ee77be94ab Mon Sep 17 00:00:00 2001 From: slorg1 Date: Wed, 10 Apr 2019 12:26:27 -0400 Subject: [PATCH 3/5] + add missing dependency --- requirements.txt | 3 ++- setup.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 88ed9aed..ea8f47d9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,6 @@ pyparsing==2.2.2 decorator==4.0.10 six==1.10.0 backports.functools-lru-cache==1.5 +qualname=0.1.0 future -numpy<1.16 +numpy<1.16 \ No newline at end of file diff --git a/setup.py b/setup.py index 9e51ecbc..33ce604b 100644 --- a/setup.py +++ b/setup.py @@ -64,7 +64,7 @@ def get_version(filename): package_dir={'': 'src'}, packages=find_packages('src'), - install_requires=['pyparsing', 'decorator', 'six', 'future', 'backports.functools-lru-cache'], + install_requires=['pyparsing', 'decorator', 'six', 'future', 'backports.functools-lru-cache', 'qualname', ], tests_require=['nose'], entry_points={}, ) From 68661f2968c2061a0761e08a57f709064a9802cf Mon Sep 17 00:00:00 2001 From: slorg1 Date: Wed, 10 Apr 2019 12:26:56 -0400 Subject: [PATCH 4/5] Update requirements.txt --- requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index ea8f47d9..50f90c6e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,6 @@ pyparsing==2.2.2 decorator==4.0.10 six==1.10.0 backports.functools-lru-cache==1.5 -qualname=0.1.0 +qualname==0.1.0 future -numpy<1.16 \ No newline at end of file +numpy<1.16 From 7c26743151fac853a47e6064a3b5afd2f9fc0ed1 Mon Sep 17 00:00:00 2001 From: slorg1 Date: Fri, 3 May 2019 07:36:44 -0400 Subject: [PATCH 5/5] + this change seems to have go lost... --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index ea8f47d9..969b1912 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,6 @@ pyparsing==2.2.2 decorator==4.0.10 six==1.10.0 backports.functools-lru-cache==1.5 -qualname=0.1.0 +qualname==0.1.0 future numpy<1.16 \ No newline at end of file