Skip to content

Check type variable bounds of self types #2351

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 · 7 comments
Open

Check type variable bounds of self types #2351

JukkaL opened this issue Oct 27, 2016 · 7 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 27, 2016

As follow-up to #2193, check that if self is declared with a type variable type, the type variable has a compatible bound. For example, some of these should be rejected:

T = TypeVar('T')
TA = TypeVar('TA', bound='A')
TC = TypeVar('TC', bound='C')

class A:
    def f(self: T) -> T: ...  # Should be error
    def g(self: TC) -> TC: ...  # Should be error
    def h(self: TA) -> TA: ...  # OK

class C: pass

(We may also want to check variance, but it's a separate issue and less clear.)

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

elazarg commented Oct 30, 2016

#2381 should reject TC, but will accept T. I don't think I understand why should T be rejected.

@elazarg
Copy link
Contributor

elazarg commented Oct 30, 2016

Another complication - if the function is overloaded, it is the union of the self-types that should be considered.

@gvanrossum gvanrossum removed this from the Future milestone Mar 29, 2017
@elazarg
Copy link
Contributor

elazarg commented Apr 3, 2017

@JukkaL do you consider the current behavior problematic/incomplete?

@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 18, 2017

I don't think that allowing T is incorrect, but it's likely almost always not what the programmer intended, so giving a warning about it seems reasonable. Otherwise there could a more mysterious error messages generated elsewhere. This is not a very important issue, though.

@gvanrossum
Copy link
Member

I've written code like this using just T. My assumption was that for that particular method, T would behave as if it had a bound of the containing class.

@elazarg
Copy link
Contributor

elazarg commented Apr 18, 2017

Me too. I think the assumption about implicit bounds is "infer" rather than "object".

@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 18, 2017

Hmm inferring the bound might be reasonable thing, at least when there is no explicit conflicting bound. However, that's not how self types work right now, and it would be a little ad hoc.

@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

4 participants