Skip to content

Fix decorator in retry module - #5482

Merged
srittau merged 3 commits into
python:masterfrom
fperrin:retry-decorator
May 17, 2021
Merged

srittau merged 3 commits into
python:masterfrom
fperrin:retry-decorator

Conversation

@fperrin

@fperrin fperrin commented May 17, 2021

Copy link
Copy Markdown
Contributor

Hi,

I'm afraid the type annotations for the retry module are incorrect, although it's possible that the real issue is in mypy; it's possible I don't understand typing annotations enough to be 100% sure.

I want to typecheck the following program with mypy:

from retry import retry

@retry(tries=5)
def foo(bar: str) -> int: ...

# Valid code
bar: int
bar = foo("abc")

# Should fail (twice): both argument and return value have incompatible types
baz: dict
baz = foo([])

reveal_type(retry)

Running with python3.7 (as provided by Debian) or 3.9.4 (latest version compiled locally) with mypy 0.820 and types-retry 0.1.1, I get the following:

% mypy demo.py
demo.py:3: error: Argument 1 has incompatible type "Callable[[str], int]"; expected <nothing>
demo.py:8: error: <nothing> not callable
demo.py:12: error: <nothing> not callable
demo.py:14: note: Revealed type is "def [_T <: def (*Any, **Any) -> Any] (exceptions: Union[Type[builtins.Exception], builtins.tuple[Type[builtins.Exception]]] =, tries: builtins.int =, delay: builtins.float =, max_delay: Union[builtins.float, None] =, backoff: builtins.float =, jitter: Union[Tuple[builtins.float, builtins.float], builtins.float] =, logger: Union[logging.Logger, None] =) -> def (_T`-1) -> _T`-1"
Found 3 errors in 1 file (checked 1 source file)

Which is not right.

With the changes included in this PR, I get the expected errors from mypy:

% env MYPYPATH=$PWD/stubs/retry mypy demo.py
demo.py:12: error: Incompatible types in assignment (expression has type "int", variable has type "Dict[Any, Any]")
demo.py:12: error: Argument 1 to "foo" has incompatible type "List[<nothing>]"; expected "str"
demo.py:14: note: Revealed type is "def (exceptions: Union[Type[builtins.Exception], builtins.tuple[Type[builtins.Exception]]] =, tries: builtins.int =, delay: builtins.float =, max_delay: Union[builtins.float, None] =, backoff: builtins.float =, jitter: Union[Tuple[builtins.float, builtins.float], builtins.float] =, logger: Union[logging.Logger, None] =) -> retry.api._Decorator"
Found 2 errors in 1 file (checked 1 source file)

I copied the definition of _Decorator from stubs/click/click/decorators.pyi.

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good to me.

For the record, I think this might be a bug in mypy, since:

diff --git a/stubs/retry/retry/api.pyi b/stubs/retry/retry/api.pyi
index 88f274b9..6e1a0edd 100644
--- a/stubs/retry/retry/api.pyi
+++ b/stubs/retry/retry/api.pyi
@@ -25,4 +25,4 @@ def retry(
     backoff: float = ...,
     jitter: Union[Tuple[float, float], float] = ...,
     logger: Optional[Logger] = ...,
-) -> _Decorator[_T]: ...
+) -> Callable[[_T], _T]: ...

is also enough to fix.

However, IIRC this shorthand where the TypeVar only occurs in the return type is sort of contentious syntax between type checkers, so using the callback protocol is probably for the best.

@hauntsaninja

Copy link
Copy Markdown
Collaborator

@srittau

srittau commented May 17, 2021

Copy link
Copy Markdown
Collaborator

I extracted IdentityFunction to _typeshed in #5483, like I said we should. We can use that in this PR.

@fperrin

fperrin commented May 17, 2021

Copy link
Copy Markdown
Contributor Author

Thanks for having a look @hauntsaninja .
@srittau , should I wait for #5483 to be merged and update this PR? Or you want to make the change to retry yourself?

@srittau

srittau commented May 17, 2021

Copy link
Copy Markdown
Collaborator

Whatever you prefer, I can do it, or you can wait.

@fperrin

fperrin commented May 17, 2021

Copy link
Copy Markdown
Contributor Author

Finish your work on 5483, I'll take care of retry later.

@srittau

srittau commented May 17, 2021

Copy link
Copy Markdown
Collaborator

#5483 is merged now.

@fperrin

fperrin commented May 17, 2021

Copy link
Copy Markdown
Contributor Author

Merged master and updated to use IdentifyFunction.

@srittau
srittau merged commit a0abacd into python:master May 17, 2021
@fperrin

fperrin commented May 17, 2021

Copy link
Copy Markdown
Contributor Author

Thanks @srittau & @hauntsaninja !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants