Skip to content

Add overloads to unittest.mock._patch_dict.__call__ - #16310

Open
mxr wants to merge 1 commit into
python:mainfrom
mxr:patch-dict
Open

Add overloads to unittest.mock._patch_dict.__call__#16310
mxr wants to merge 1 commit into
python:mainfrom
mxr:patch-dict

Conversation

@mxr

@mxr mxr commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Resolves #16307

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Comment thread stdlib/unittest/mock.pyi
Comment on lines +305 to +310
@overload
def __call__(self, f: _TT) -> _TT: ...
@overload
def __call__(self, f: _AF) -> _AF: ...
@overload
def __call__(self, f: _F) -> _F: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it's not totally clear to me how having three overloads here is any different to doing this:

Suggested change
@overload
def __call__(self, f: _TT) -> _TT: ...
@overload
def __call__(self, f: _AF) -> _AF: ...
@overload
def __call__(self, f: _F) -> _F: ...
def __call__(self, f: _F) -> _F: ...

Any type assignable to type[Any] (upper bound of _TT) or Callable[..., Coroutine[Any, Any, Any]](upper bound of _AF) will also be assignable to Callable[..., Any] (upper bound of _F), and a type checker will always pick the most precise type when solving a generic call

@AlexWaygood

Copy link
Copy Markdown
Member

I dug through the blame a bit to see if there had been any previous churn or discussion about this signature, since annotations in unittest.mock have caused us some level of difficulty in the past... it looks like this has been unchanged since 2017! Type annotations were added in #973

@AlexWaygood

Copy link
Copy Markdown
Member

This LGTM other than the simplification I suggested above, it does seem like this preserves the wrapped function's signature. Thanks!

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.

@patch.dict(...) loses wrapped function's signature

2 participants