Skip to content
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

Specialized methods on generic classes #14243

Open
Tinche opened this issue Dec 4, 2022 · 0 comments
Open

Specialized methods on generic classes #14243

Tinche opened this issue Dec 4, 2022 · 0 comments
Labels
bug mypy got something wrong topic-self-types Types for self topic-type-variables

Comments

@Tinche
Copy link
Contributor

Tinche commented Dec 4, 2022

Bug Report

Let's say I have a generic class G.

from __future__ import annotations

from collections.abc import Sized
from typing import Generic, TypeVar

T = TypeVar("T")

class G(Generic[T]):
    pass

I want to have a single method on this class that's only legal for a subset of T.

If I want to limit it to, say, int, it works:

class G(Generic[T]):
    def test(self: G[int]) -> G[int]:
        return self

G[int]().test()  # Works, as it should
G[str]().test()  # Error, as it should

But if I want to limit it to, say, a protocol like Sized, I get a false negative:

S = TypeVar("S", bound=Sized)

class G(Generic[T]):
    def test(self: G[S]) -> G[S]:
        return self


G[int]().test()  # False negative - works, but it shouldn't

This is a pity, since it'd unlock some cool features I was working on vis-a-vis using attrs attributes. Are there any workarounds? Would this be handled by intersection types maybe?

Your Environment

  • Mypy version used: mypy 1.0.0+dev.b8c03ab6809aab56928f3cd865edb44944a600a2
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.10.6
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-self-types Types for self topic-type-variables
Projects
None yet
Development

No branches or pull requests

2 participants