-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mypy fails to determine function's type in icontract.ensure() #243
Comments
Thanks for the issue, @claudio-ebel ! I'm busy till this Thu, but afterwards I should be able to find some time to have a look at it. |
Wow, thank you @mristin for your super fast reply! Please take your time, I've even found a trick to bypass this issue! Just introduce an annotated helper function forwarding the arguments: # ––– file: trick.py –––
from icontract import ensure
def _myadd(a: int, b: int) -> int:
"""helper function to circumvent icontract issue"""
return myadd(a, b)
@ensure(lambda a, b, result: result == _myadd(b, a), "Commutativity violated!")
def myadd(a: int, b: int) -> int:
return a + b resulting to no issue: $ mypy --config-file= trick.py
Success: no issues found in 1 source file |
@claudio-ebel please apologize again for the delay. I'm having some tough time at work. |
@claudio-ebel Now this is a coincidence :-) -- they are just about to fix this issue in mypy: python/mypy#8645 Let's wait for a couple of days for the next mypy release. You'll need Python 3.10 for this feature to work, though. |
@mristin absolutely no problem and thank you very much for linking that issue! Sadly, even with the new $ python --version
Python 3.10.4
$ mypy --version
mypy 0.960 (compiled: yes)
$ pip freeze | grep icontract
icontract==2.6.1
$ mypy --config-file= bug.py
bug.py:5: error: Cannot determine type of "myadd"
Found 1 error in 1 file (checked 1 source file) I hope that the work of the |
@claudio-ebel I had another look at the issue. Finally I understood what's going on -- I totally missed the point in the original comments. Let me document it here, in case somebody else needs to look it up in the future. The issue is not with I suppose this is one of the cases where it is appropriate to write
I'll file an issue with mypy; perhaps there are other people relying on this. |
Bug Report
When a type-annotated function is (re-)used in its own postcondition
icontract.ensure
decorator,mypy
is not able to determine its type. Sincemypy
does successfully infers the types of an annotated function before the function's actual definition, this bug is part of theicontract
library and notmypy
.To Reproduce
mypy
on itExpected Behavior
In general,
mypy
does not have a problem with using an annotated function before its definition. To see this,mypy
on itFailed solution attempts
Using a forward declaration of
myadd
did not help:mypy
on itAn alternative use of a cast of
myadd
's return type did not help either:mypy
on itYour Environment
mypy 0.950 (compiled: yes)
--config-file=
icontract==2.6.1
Python 3.10.4
The text was updated successfully, but these errors were encountered: