Skip to content

Commit

Permalink
Ran mypy --strict against icontract
Browse files Browse the repository at this point in the history
  • Loading branch information
mristin committed Jan 23, 2019
1 parent 6e2f3ee commit 928d749
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion icontract/_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class _Old:
def __init__(self, mapping: Mapping[str, Any]) -> None:
self.__dict__.update(mapping)

def __getattr__(self, item):
def __getattr__(self, item: str) -> Any:
raise AttributeError("The snapshot with the name {!r} is not available in the OLD of a postcondition. "
"Have you decorated the function with a corresponding snapshot decorator?".format(item))

Expand Down
6 changes: 3 additions & 3 deletions icontract/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self,
description: Optional[str] = None,
a_repr: reprlib.Repr = icontract._globals.aRepr,
enabled: bool = __debug__,
error: Union[Callable[..., Exception], type] = None) -> None:
error: Optional[Union[Callable[..., Exception], type]] = None) -> None:
"""
Initialize.
Expand Down Expand Up @@ -184,7 +184,7 @@ def __init__(self,
description: Optional[str] = None,
a_repr: reprlib.Repr = icontract._globals.aRepr,
enabled: bool = __debug__,
error: Union[Callable[..., Exception], type] = None) -> None:
error: Optional[Union[Callable[..., Exception], type]] = None) -> None:
"""
Initialize.
Expand Down Expand Up @@ -266,7 +266,7 @@ def __init__(self,
description: Optional[str] = None,
a_repr: reprlib.Repr = icontract._globals.aRepr,
enabled: bool = __debug__,
error: Union[Callable[..., Exception], type] = None) -> None:
error: Optional[Union[Callable[..., Exception], type]] = None) -> None:
"""
Initialize a class decorator to establish the invariant on all the public methods.
Expand Down
2 changes: 1 addition & 1 deletion icontract/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self,
condition: Callable[..., bool],
description: Optional[str] = None,
a_repr: reprlib.Repr = icontract._globals.aRepr,
error: Union[Callable[..., Exception], type] = None) -> None:
error: Optional[Union[Callable[..., Exception], type]] = None) -> None:
"""
Initialize.
Expand Down
34 changes: 20 additions & 14 deletions tests/test_mypy_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,46 @@

class TestMypyDecorators(unittest.TestCase):
def test_mypy_me(self):
with tempfile.NamedTemporaryFile(prefix="mypy_fail_case", suffix=".py") as tmp:
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):
def some_precond(x: int) -> bool: return x > 0
@icontract.require(some_precond)
def f1(x: int) -> int:
return x
f1("this is wrong")
@icontract.ensure(lambda result: result > 0)
def f2(x: int):
def some_postcond(result: int) -> bool: return result > 0
@icontract.ensure(some_postcond)
def f2(x: int) -> int:
return x
f2("this is wrong")
@icontract.snapshot(lambda x: x)
def f3(x: int):
def some_snapshot(x: int) -> int: return x
@icontract.snapshot(some_snapshot)
def f3(x: int) -> int:
return x
f3("this is wrong")
''').encode())

tmp.file.flush()

proc = subprocess.Popen(['mypy', tmp.name], universal_newlines=True, stdout=subprocess.PIPE)
proc = subprocess.Popen(['mypy', '--strict', 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"
{path}:10: error: Argument 1 to "f1" has incompatible type "str"; expected "int"
{path}:17: error: Argument 1 to "f2" has incompatible type "str"; expected "int"
{path}:24: error: Argument 1 to "f3" has incompatible type "str"; expected "int"
'''.format(path=tmp.name)),
out)

Expand Down

0 comments on commit 928d749

Please sign in to comment.