Skip to content

Overriding of methods with self types #2353

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

Open
JukkaL opened this issue Oct 27, 2016 · 5 comments
Open

Overriding of methods with self types #2353

JukkaL opened this issue Oct 27, 2016 · 5 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 27, 2016

It's unclear how we should check overrides of methods that use self types (e.g. self: T). There is some discussion in #2193.

A relevant comment (but there are others): #2193 (comment)

@JukkaL JukkaL mentioned this issue Oct 27, 2016
@gvanrossum gvanrossum added this to the Future milestone Oct 27, 2016
@ilevkivskyi
Copy link
Member

@JukkaL

I tried to left a (really long) comment here a week ago. But it looks like something went wrong. I suppose I just forgot to press "Comment" :-(

It is difficult to reproduce it again now, but very short summary is: Now I am in favour of option (4) you proposed in #2193 (comment)

@gvanrossum
Copy link
Member

Now I am in favour of option (4) you proposed in #2193 (comment)
#2193 (comment)

Agreed.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Nov 30, 2016

The duplicate issue #2511 has a relevant example that has a class method.

@tyrion
Copy link
Contributor

tyrion commented Aug 22, 2019

The following example seems related to this issue:

S = TypeVar("S")
T = TypeVar("T")

F = TypeVar("F", bound="Foo[Any, Any]")
class Foo(Generic[S, T]):
    def test(self: F) -> F:
        raise NotImplementedError

B = TypeVar("B", bound="Bar[Any]")
class Bar(Foo[int, T], Generic[T]):
    def test(self: B) -> B:
        return self

mypy tells me Return type "Bar[T]" of "test" incompatible with return type "Bar[int]" in supertype "Foo" But I cannot understand what it means with Bar[int].

As requested by @ilevkivskyi on gitter, I am adding a comment here.

@slessans
Copy link

slessans commented Jan 5, 2021

The following example seems related to this issue:

S = TypeVar("S")
T = TypeVar("T")

F = TypeVar("F", bound="Foo[Any, Any]")
class Foo(Generic[S, T]):
    def test(self: F) -> F:
        raise NotImplementedError

B = TypeVar("B", bound="Bar[Any]")
class Bar(Foo[int, T], Generic[T]):
    def test(self: B) -> B:
        return self

mypy tells me Return type "Bar[T]" of "test" incompatible with return type "Bar[int]" in supertype "Foo" But I cannot understand what it means with Bar[int].

As requested by @ilevkivskyi on gitter, I am adding a comment here.

@tyrion wondering if there was any progress made on this or if there is a workaround?

@AlexWaygood AlexWaygood added the topic-self-types Types for self label Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants