-
Notifications
You must be signed in to change notification settings - Fork 18
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
require decorator removes type annotations for mypy #93
Comments
|
(Let me do a couple of experiments. I think I misread python/mypy#3157) |
Hi @jamescasbon My bad, I indeed misread python/mypy#3157. Mypy indeed supports decorators which do not change the signature of the function. Would you mind reviewing the following snippet for me? I assume that I simply need to change the argument type and return type of experiment.py: from typing import TypeVar, Any, Callable
CallableT = TypeVar('CallableT', bound='Callable')
class some_decorator:
def __init__(self, some_param: int)->None:
return
def __call__(self, func: CallableT) -> CallableT:
def result(*args, **kwargs):
print("hello")
return func(*args, **kwargs)
return result # type: ignore
@some_decorator(some_param="oi")
def f(x: int, y: str)->str:
return str(x + int(y))
f(x="oi", y=1) I get the expected errors: $ mypy experiment.py
experiment.py:16: error: Argument "some_param" to "some_decorator" has incompatible type "str"; expected "int"
experiment.py:20: error: Argument "x" to "f" has incompatible type "str"; expected "int"
experiment.py:20: error: Argument "y" to "f" has incompatible type "int"; expected "str" |
@jamescasbon the PR is up: #94 If you have a spare minute, could you please check out the branch and double-check that it fixes the issue? (You might also want to review the relevant test in |
Amazing work, yes the branch works with mypy now 👍 Thanks for the quick turnaround. |
Ah, i redid the check with mypy --strict The require decorator now gives this error:
I do think this PR is a lot better - but would be very nice if we could run with strict |
Hi James,
Let me have a look at it tonight (CET).
|
Hi @jamescasbon,
class require:
def __init__(self,
condition: Callable[..., bool],
description: Optional[str] = None,
a_repr: reprlib.Repr = icontract._globals.aRepr,
enabled: bool = __debug__,
error: Optional[Union[Callable[..., Exception], type]] = None) -> None:
... As you can see, it expects condition to be of type If you replace it with Now, I'm not really sure what is the best approach. I expect that typing the condition as Please double-check the latest commit and if everything is clear/working OK, let me bump the patch version. |
Hmmmm. To be honest I don't know how many people are using mypy --strict. I am using DBC + mypy to get correctness so my aim would be strict. 3.6 can type lambdas but not anonymous ones :/ Seems to me if you expect condition to be a lambda you should match the type of a lambda but I appreciate it is not an easy choice. |
After some thinking I came to the conclusion that it makes sense to have condition return x = [] # type: List[int]
if x:
... Since this is so pervasive in Python, there is no reason why suddenly icontract would follow a "boolean-first" policy. Please have a look at 3b35de1 and let me know if we are good to merge it into the master. Thanks again for all the help! |
(@jamescasbon -- just a kind reminder; I'd like to wait for you to check that the latest commit fixes your problem before I bump the patch version.) |
Thanks very much this looks great. I think you should ship it 👍 |
Both function calls are invalid types but mypy only sees the call as f2 as wrong. The decorator loses the type annotations?
Related python/mypy#3157
The text was updated successfully, but these errors were encountered: