Skip to content

Fix @patch when new is missing #10459

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

Merged
merged 5 commits into from
Jul 14, 2023
Merged

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Jul 13, 2023

Closes: #10457

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

streamlit (https://github.com/streamlit/streamlit)
- lib/tests/streamlit/config_test.py:619:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "return_value"  [attr-defined]
+ lib/tests/streamlit/config_test.py:619:9: error: "_patch_default_new" has no attribute "return_value"  [attr-defined]
- lib/tests/streamlit/config_test.py:621:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "side_effect"  [attr-defined]
+ lib/tests/streamlit/config_test.py:621:9: error: "_patch_default_new" has no attribute "side_effect"  [attr-defined]
- lib/tests/streamlit/config_test.py:645:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "return_value"  [attr-defined]
+ lib/tests/streamlit/config_test.py:645:9: error: "_patch_default_new" has no attribute "return_value"  [attr-defined]
- lib/tests/streamlit/config_test.py:647:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "side_effect"  [attr-defined]
+ lib/tests/streamlit/config_test.py:647:9: error: "_patch_default_new" has no attribute "side_effect"  [attr-defined]
- lib/tests/streamlit/config_test.py:683:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "return_value"  [attr-defined]
+ lib/tests/streamlit/config_test.py:683:9: error: "_patch_default_new" has no attribute "return_value"  [attr-defined]
- lib/tests/streamlit/config_test.py:685:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "side_effect"  [attr-defined]
+ lib/tests/streamlit/config_test.py:685:9: error: "_patch_default_new" has no attribute "side_effect"  [attr-defined]
- lib/tests/streamlit/config_test.py:731:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "return_value"  [attr-defined]
+ lib/tests/streamlit/config_test.py:731:9: error: "_patch_default_new" has no attribute "return_value"  [attr-defined]
- lib/tests/streamlit/config_test.py:733:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "side_effect"  [attr-defined]
+ lib/tests/streamlit/config_test.py:733:9: error: "_patch_default_new" has no attribute "side_effect"  [attr-defined]
- lib/tests/streamlit/config_test.py:757:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "return_value"  [attr-defined]
+ lib/tests/streamlit/config_test.py:757:9: error: "_patch_default_new" has no attribute "return_value"  [attr-defined]
- lib/tests/streamlit/config_test.py:759:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "side_effect"  [attr-defined]
+ lib/tests/streamlit/config_test.py:759:9: error: "_patch_default_new" has no attribute "side_effect"  [attr-defined]
- lib/tests/streamlit/config_test.py:793:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "return_value"  [attr-defined]
+ lib/tests/streamlit/config_test.py:793:9: error: "_patch_default_new" has no attribute "return_value"  [attr-defined]
- lib/tests/streamlit/config_test.py:795:9: error: "_patch[Union[MagicMock, AsyncMock]]" has no attribute "side_effect"  [attr-defined]
+ lib/tests/streamlit/config_test.py:795:9: error: "_patch_default_new" has no attribute "side_effect"  [attr-defined]

@svalentin
Copy link

Thank you!

class _patch_default_new(_patch[_Mock]):
@overload
def __call__(self, func: _TT) -> _TT: ...
# Can't use the following as ParamSpec is only allowed as last parameter:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Can't use the following as ParamSpec is only allowed as last parameter:
# Can't use the following as ParamSpec is only allowed as the last parameter to Concatenate[]:

assert_type(f_default_new(1), str)
f_default_new("a") # Not an error due to ParamSpec limitations
assert_type(f_explicit_new(1), str)
f_explicit_new("a") # type: ignore[arg-type]
Copy link
Member

Choose a reason for hiding this comment

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

Nit: we currently don't use mypy error codes for type: ignores in the test_cases directory: (see rationale in #8083)

Suggested change
f_explicit_new("a") # type: ignore[arg-type]
f_explicit_new("a") # type: ignore

@srittau srittau merged commit 7ea173c into python:main Jul 14, 2023
@srittau srittau deleted the unittest-patch-fix branch July 14, 2023 10:53
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.

incorrect types for functions decorated with @mock.patch
4 participants