Skip to content

[1.16] Regression in inferring type via unittest.mock.Mock #19184

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

Closed
Dreamsorcerer opened this issue May 31, 2025 · 2 comments · Fixed by #19191
Closed

[1.16] Regression in inferring type via unittest.mock.Mock #19184

Dreamsorcerer opened this issue May 31, 2025 · 2 comments · Fixed by #19191
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations topic-protocols

Comments

@Dreamsorcerer
Copy link
Contributor

Dreamsorcerer commented May 31, 2025

Bug Report

Regression on type inference found in aiohttp:master.

Seems like the regression may be related to inheriting from Any (which mypy complains about, but is done in typeshed regardless, and has been for many years).

To Reproduce

This is a fairly minimal reproducer of the issue:

from typing import Any, Generic, Protocol, TypeVar
#from unittest.mock import Mock

_T = TypeVar("_T", covariant=True)

class Mock(Any):
    def __init__(self, **kwargs: Any) -> None: ...
    def __call__(self, *args: Any, **kwargs: Any) -> Any: ...


class _Factory(Protocol[_T]):
    def __call__(self, **kwargs: Any) -> _T: ...


class TraceConfig(Generic[_T]):
    def __init__(self, trace_config_ctx_factory: _Factory[_T]) -> None:
        ...

t = TraceConfig(Mock(return_value="foo"))

1.15 Behavior

(First error disappears if we actually import Mock.)

test.py:6: error: Class cannot subclass "Any" (has type "Any")  [misc]
Found 1 error in 1 file (checked 1 source file)

1.16 Behavior

test.py:6: error: Class cannot subclass "Any" (has type "Any")  [misc]
test.py:19: error: Need type annotation for "t"  [var-annotated]
Found 2 errors in 1 file (checked 1 source file)
@Dreamsorcerer Dreamsorcerer added the bug mypy got something wrong label May 31, 2025
@sterliakov sterliakov added topic-protocols topic-inference When to infer types or require explicit annotations labels May 31, 2025
@ilevkivskyi
Copy link
Member

Hm, if subclassing Any is important here, I think there is a chance this is related to #8829 (on which I am going to work soon).

@ilevkivskyi
Copy link
Member

Actually it looks like it was caused by #18587 because we consider that a class with fallback to any may be a metaclass. Maybe we can not do this here, since that special case was added specifically to ignore spurious Anys from type.__call__() IIRC. I will play with this.

hauntsaninja pushed a commit that referenced this issue Jun 1, 2025
Fixes #19184

This fixes an (edge-case) regression introduced in 1.16. Fix is
straightforward, only ignore `__call__` if it comes from an _actual_
metaclass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations topic-protocols
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants