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

Generic Self Type #2537

Closed
jboning opened this issue Dec 8, 2016 · 2 comments
Closed

Generic Self Type #2537

jboning opened this issue Dec 8, 2016 · 2 comments

Comments

@jboning
Copy link
Contributor

jboning commented Dec 8, 2016

This is a followup issue to #1212. #2193 enabled method type annotations to refer to the type of self, but (intentionally) didn't address generic classes. In many cases, the self types added there meet the needs of generic classes, but not always.

A motivating example is typeshed's AbstractClass:

class AbstractSet(Sized, Iterable[_T_co], Container[_T_co], Generic[_T_co]):
    def __and__(self, s: AbstractSet[Any]) -> AbstractSet[_T_co]: ...
    def __or__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ...

With #2193 we can annotate __and__, and it works beautifully:

    def __and__(self: SelfT, s: AbstractSet[Any]) -> SelfT: ...
def f(s, fs):
    # type: (MutableSet[str], FrozenSet[str]) -> None
    reveal_type(s.__and__)  # Revealed type is 'def (typing.AbstractSet[Any]) -> typing.MutableSet[builtins.str]'
    reveal_type(fs.__and__)  # Revealed type is 'def (typing.AbstractSet[Any]) -> typing.FrozenSet[builtins.str]'

But, we don't have the tools to give __or__ a similarly specific type. To do this, we need some way to talk about the "generic" part of the self type.

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 8, 2016

This feels like a duplicate of #2354, but the example raises somewhat different concerns. Maybe we should copy the example to #2354 and close this issue?

@jboning
Copy link
Contributor Author

jboning commented Dec 8, 2016 via email

@jboning jboning closed this as completed Dec 8, 2016
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

No branches or pull requests

2 participants