You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.)
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.
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.
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.
Activity
elazarg commentedon Oct 30, 2016
#2381 should reject TC, but will accept T. I don't think I understand why should T be rejected.
elazarg commentedon Oct 30, 2016
Another complication - if the function is overloaded, it is the union of the self-types that should be considered.
elazarg commentedon Apr 3, 2017
@JukkaL do you consider the current behavior problematic/incomplete?
JukkaL commentedon Apr 18, 2017
I don't think that allowing
Tis 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 commentedon Apr 18, 2017
I've written code like this using just
T. My assumption was that for that particular method,Twould behave as if it had a bound of the containing class.elazarg commentedon Apr 18, 2017
Me too. I think the assumption about implicit bounds is "infer" rather than "object".
JukkaL commentedon 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.