Skip to content

Commit

Permalink
updated to icontract 2.0.0 (#14)
Browse files Browse the repository at this point in the history
* updated to icontract 2.0.0

* bumped to 2.0.0
  • Loading branch information
mristin authored Oct 24, 2018
1 parent 89a9060 commit 465eb7c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 80 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.0.0
=====
* Updated to icontract 2.0.0

1.2.1
=====
* Updated to icontract 1.7.1 due to refactoring of tight coupling with icontract internals
Expand Down
38 changes: 20 additions & 18 deletions icontract_lint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class ErrorID(enum.Enum):
INVALID_SYNTAX = 'invalid-syntax'


@icontract.inv(lambda self: len(self.description) > 0)
@icontract.inv(lambda self: len(self.filename) > 0)
@icontract.inv(lambda self: self.lineno >= 1)
@icontract.invariant(lambda self: len(self.description) > 0)
@icontract.invariant(lambda self: len(self.filename) > 0)
@icontract.invariant(lambda self: self.lineno >= 1)
class Error:
"""
Represent a linter error.
Expand All @@ -62,9 +62,9 @@ class Error:
"""

@icontract.pre(lambda description: len(description) > 0)
@icontract.pre(lambda filename: len(filename) > 0)
@icontract.pre(lambda lineno: lineno >= 1)
@icontract.require(lambda description: len(description) > 0)
@icontract.require(lambda filename: len(filename) > 0)
@icontract.require(lambda lineno: lineno >= 1)
def __init__(self, identifier: ErrorID, description: str, filename: str, lineno: int) -> None:
"""Initialize with the given properties."""
self.identifier = identifier
Expand Down Expand Up @@ -112,7 +112,7 @@ def __init__(self, filename: str) -> None:
self._filename = filename
self.errors = [] # type: List[Error]

@icontract.pre(lambda lineno: lineno >= 1)
@icontract.require(lambda lineno: lineno >= 1)
def _verify_pre(self, func_arg_set: Set[str], condition_arg_set: Set[str], lineno: int) -> None:
"""
Verify the precondition.
Expand All @@ -132,7 +132,7 @@ def _verify_pre(self, func_arg_set: Set[str], condition_arg_set: Set[str], linen
filename=self._filename,
lineno=lineno))

@icontract.pre(lambda lineno: lineno >= 1)
@icontract.require(lambda lineno: lineno >= 1)
def _verify_post(self, func_arg_set: Set[str], func_has_result: bool, condition_arg_set: Set[str],
lineno: int) -> None:
"""
Expand Down Expand Up @@ -202,7 +202,7 @@ def _find_condition_node(self, node: astroid.nodes.Call) -> Optional[astroid.nod

return condition_node

@icontract.pre(lambda pytype: pytype in ['icontract._decorators.pre', 'icontract._decorators.post'])
@icontract.require(lambda pytype: pytype in ['icontract._decorators.require', 'icontract._decorators.ensure'])
def _verify_precondition_or_postcondition_decorator(self, node: astroid.nodes.Call, pytype: str,
func_arg_set: Set[str], func_has_result: bool) -> None:
"""
Expand Down Expand Up @@ -238,10 +238,10 @@ def _verify_precondition_or_postcondition_decorator(self, node: astroid.nodes.Ca
condition_arg_set = set(condition.argnames())

# Verify
if pytype == 'icontract._decorators.pre':
if pytype == 'icontract._decorators.require':
self._verify_pre(func_arg_set=func_arg_set, condition_arg_set=condition_arg_set, lineno=node.lineno)

elif pytype == 'icontract._decorators.post':
elif pytype == 'icontract._decorators.ensure':
self._verify_post(
func_arg_set=func_arg_set,
func_has_result=func_has_result,
Expand Down Expand Up @@ -321,10 +321,12 @@ def _check_func_decorator(self, node: astroid.nodes.Call, decorator: astroid.bas
pytype = decorator.pytype()

# Ignore non-icontract decorators
if pytype not in ["icontract._decorators.pre", "icontract._decorators.snapshot", "icontract._decorators.post"]:
if pytype not in [
"icontract._decorators.require", "icontract._decorators.snapshot", "icontract._decorators.ensure"
]:
return

if pytype in ['icontract._decorators.pre', 'icontract._decorators.post']:
if pytype in ['icontract._decorators.require', 'icontract._decorators.ensure']:
self._verify_precondition_or_postcondition_decorator(
node=node, pytype=pytype, func_arg_set=func_arg_set, func_has_result=func_has_result)

Expand Down Expand Up @@ -392,7 +394,7 @@ def visit_FunctionDef(self, node: astroid.nodes.FunctionDef) -> None: # pylint:
pytypes = [decorator.pytype() for decorator in decorators if decorator is not None] # type: List[str]
assert all(isinstance(pytype, str) for pytype in pytypes)

if 'icontract._decorators.snapshot' in pytypes and 'icontract._decorators.post' not in pytypes:
if 'icontract._decorators.snapshot' in pytypes and 'icontract._decorators.ensure' not in pytypes:
self.errors.append(
Error(
identifier=ErrorID.SNAPSHOT_WO_POST,
Expand Down Expand Up @@ -420,7 +422,7 @@ def _check_class_decorator(self, node: astroid.Call) -> None:

pytype = decorator.pytype()

if pytype != 'icontract._decorators.inv':
if pytype != 'icontract._decorators.invariant':
return

condition_node = self._find_condition_node(node=node)
Expand Down Expand Up @@ -469,7 +471,7 @@ def visit_ClassDef(self, node: astroid.nodes.ClassDef) -> None: # pylint: disab
_DISABLED_DIRECTIVE_RE = re.compile(r'^\s*#\s*pyicontract-lint\s*:\s*disabled\s*$')


@icontract.pre(lambda path: path.is_file())
@icontract.require(lambda path: path.is_file())
def check_file(path: pathlib.Path) -> List[Error]:
"""
Parse the given file as Python code and lint its contracts.
Expand Down Expand Up @@ -508,7 +510,7 @@ def check_file(path: pathlib.Path) -> List[Error]:
return lint_visitor.errors


@icontract.pre(lambda path: path.is_dir())
@icontract.require(lambda path: path.is_dir())
def check_recursively(path: pathlib.Path) -> List[Error]:
"""
Lint all ``*.py`` files beneath the directory (including subdirectories).
Expand All @@ -523,7 +525,7 @@ def check_recursively(path: pathlib.Path) -> List[Error]:
return errs


@icontract.post(lambda paths, result: len(paths) > 0 or len(result) == 0, "no paths implies no errors")
@icontract.ensure(lambda paths, result: len(paths) > 0 or len(result) == 0, "no paths implies no errors")
def check_paths(paths: List[pathlib.Path]) -> List[Error]:
"""
Lint the given paths.
Expand Down
2 changes: 1 addition & 1 deletion pyicontract_lint_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__title__ = 'pyicontract-lint'
__description__ = 'Lint contracts defined with icontract library.'
__url__ = 'https://github.com/Parquery/pyicontract-lint'
__version__ = '1.2.1'
__version__ = '2.0.0'
__author__ = 'Marko Ristin'
__author_email__ = 'marko.ristin@gmail.com'
__license__ = 'MIT'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
license='License :: OSI Approved :: MIT License',
keywords='design-by-contract precondition postcondition validation lint',
packages=find_packages(exclude=['tests']),
install_requires=['icontract>=1.7.1,<2', 'astroid>=2.0.4,<3'],
install_requires=['icontract>=2.0.0,<3', 'astroid>=2.0.4,<3'],
extras_require={
'dev': [
# yapf: disable
Expand Down
Loading

0 comments on commit 465eb7c

Please sign in to comment.