-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
specified require and ensure to use generics
- Loading branch information
Showing
4 changed files
with
72 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# pylint: disable=missing-docstring | ||
|
||
import subprocess | ||
import tempfile | ||
import textwrap | ||
import unittest | ||
|
||
|
||
class TestMypyDecorators(unittest.TestCase): | ||
def test_mypy_me(self): | ||
with tempfile.NamedTemporaryFile(prefix="mypy_fail_case", suffix=".py") as tmp: | ||
tmp.file.write( | ||
textwrap.dedent('''\ | ||
"""Implement a fail case for mypy to test that the types are preserved with the decorators.""" | ||
import icontract | ||
@icontract.require(lambda x: x > 0) | ||
def f1(x: int): | ||
return x | ||
f1("this is wrong") | ||
@icontract.ensure(lambda result: result > 0) | ||
def f2(x: int): | ||
return x | ||
f2("this is wrong") | ||
@icontract.snapshot(lambda x: x) | ||
def f3(x: int): | ||
return x | ||
f3("this is wrong") | ||
''').encode()) | ||
|
||
tmp.file.flush() | ||
|
||
proc = subprocess.Popen(['mypy', tmp.name], universal_newlines=True, stdout=subprocess.PIPE) | ||
out, err = proc.communicate() | ||
|
||
self.assertIsNone(err) | ||
self.assertEqual( | ||
textwrap.dedent('''\ | ||
{path}:8: error: Argument 1 to "f1" has incompatible type "str"; expected "int" | ||
{path}:13: error: Argument 1 to "f2" has incompatible type "str"; expected "int" | ||
{path}:18: error: Argument 1 to "f3" has incompatible type "str"; expected "int" | ||
'''.format(path=tmp.name)), | ||
out) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |