Skip to content

No recognition of concretized type for generic in method's self #14806

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
magwas opened this issue Mar 1, 2023 · 1 comment · Fixed by #18943
Closed

No recognition of concretized type for generic in method's self #14806

magwas opened this issue Mar 1, 2023 · 1 comment · Fixed by #18943
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@magwas
Copy link

magwas commented Mar 1, 2023

Bug Report

Mypy does not seem to recognize concretized type for a generic in its method's self.

To Reproduce

from typing import Callable, Generic, TypeVar
from typing_extensions import ParamSpec, Concatenate, Self
from unittest.mock import Mock

P = ParamSpec("P")
R = TypeVar("R")


class Check(Generic[P, R]):
    def check(self) -> None:
        pass

class Shall(
        Generic[P, R]):
    def __init__(self, callable: Callable[P, R]) -> None:
            pass
    def suchThat(self, other: Callable[Concatenate[R, P], bool]) -> Self:
        return self

def check(returnValue: None, other: Check[[int],str] ) -> bool:
    return True



Shall[[Check[[int],str]],None](Check[[int],str].check).suchThat(check)

https://mypy-play.net/?mypy=master&python=3.9&gist=0f2967cc8cff879cdfcc8280b7fca53c

Expected Behavior

No error

Actual Behavior

main.py:25: error: Argument 1 to "Shall" has incompatible type "Callable[[Check[P, R]], None]"; expected "Callable[[Check[[int], str]], None]" [arg-type]

  • Mypy version used: latest master (de26134)
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
strict = True
explicit_package_bases = True
namespace_packages = True
#mypy_path = src:test
files = src,test
mypy_path = $MYPY_CONFIG_FILE_DIR/src:$MYPY_CONFIG_FILE_DIR/test
  • Python version used: 3.9.2
@magwas magwas added the bug mypy got something wrong label Mar 1, 2023
@magwas
Copy link
Author

magwas commented Mar 1, 2023

Even smaller reproduction:

from typing import Callable, Generic, TypeVar
from typing_extensions import ParamSpec, Concatenate, Self
from unittest.mock import Mock

P = ParamSpec("P")
R = TypeVar("R")


class Check(Generic[P, R]):
    def check(self) -> None:
        pass

class Shall(
        Generic[P, R]):
    def __init__(self, callable: Callable[P, R]) -> None:
            pass

Shall[[Check[[int],str]],None](Check[[int],str].check)

https://mypy-play.net/?mypy=master&python=3.9&gist=4ed820cd660f49a4f7f3d800054576aa

magwas added a commit to kode-konveyor/cdd-python that referenced this issue Mar 1, 2023
@ilevkivskyi ilevkivskyi added the topic-paramspec PEP 612, ParamSpec, Concatenate label Aug 18, 2023
cdce8p pushed a commit to cdce8p/mypy that referenced this issue May 31, 2025
Fixes python#18024
Fixes python#18706
Fixes python#17734
Fixes python#15097
Fixes python#14814
Fixes python#14806
Fixes python#14259
Fixes python#13041
Fixes python#11993
Fixes python#9585
Fixes python#9266
Fixes python#9202
Fixes python#5481

This is a fourth "major" PR toward
python#7724. This is one is
watershed/crux of the whole series (but to set correct expectations,
there are almost a dozen smaller follow-up/clean-up PRs in the
pipeline).

The core of the idea is to set current type-checker as part of the
global state. There are however some details:
* There are cases where we call `is_subtype()` before type-checking. For
now, I fall back to old logic in this cases. In follow up PRs we may
switch to using type-checker instances before type checking phase (this
requires some care).
* This increases typeops import cycle by a few modules, but
unfortunately this is inevitable.
* This PR increases potential for infinite recursion in protocols. To
mitigate I add: one legitimate fix for `__call__`, and one temporary
hack for `freshen_all_functions_type_vars` (to reduce performance
impact).
* Finally I change semantics for method access on class objects to match
the one in old `find_member()`. Now we will expand type by instance, so
we have something like this:
  ```python
  class B(Generic[T]):
      def foo(self, x: T) -> T: ...
  class C(B[str]): ...
  reveal_type(C.foo)  # def (self: B[str], x: str) -> str
  ```
FWIW, I am not even 100% sure this is correct, it seems to me we _may_
keep the method generic. But in any case what we do currently is
definitely wrong (we infer a _non-generic_ `def (x: T) -> T`).

---------

Co-authored-by: hauntsaninja <[email protected]>
Co-authored-by: Shantanu <[email protected]>
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-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants