Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for named expressions in contracts
Named expressions were [introduced recently in Python 3.8][1]. This patch updates icontract's representation module so that they can be properly recomputed and represented in the violation messages. [1]: https://www.python.org/dev/peps/pep-0572/
- Loading branch information
Showing
6 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
""" | ||
Test Python 3.8-specific features. | ||
For example, one such feature is walrus operator used in named expressions. | ||
We have to exclude these tests running on prior versions of Python since the syntax would be considered | ||
invalid. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env python3 | ||
# pylint: disable=missing-docstring,invalid-name,too-many-public-methods,no-self-use | ||
# pylint: disable=unused-argument | ||
|
||
import textwrap | ||
import unittest | ||
from typing import Optional # pylint: disable=unused-import | ||
|
||
import icontract._represent | ||
import tests.error | ||
import tests.mock | ||
|
||
|
||
class TestReprValues(unittest.TestCase): | ||
def test_named_expression(self) -> None: | ||
@icontract.require(lambda x: (t := x + 1) and t > 1) # pylint: disable=undefined-variable | ||
def func(x: int) -> int: | ||
return x | ||
|
||
violation_err = None # type: Optional[icontract.ViolationError] | ||
try: | ||
func(x=0) | ||
except icontract.ViolationError as err: | ||
violation_err = err | ||
|
||
self.assertIsNotNone(violation_err) | ||
self.assertEqual( | ||
textwrap.dedent('''\ | ||
(t := x + 1) and t > 1: | ||
t was 1 | ||
x was 0'''), tests.error.wo_mandatory_location(str(violation_err))) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |