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
I admit all these underscores confuse me, but maybe you wanted something like this?
(I am writing AS instead of AbstractSet to make it easier for me to read :) )
Those annotations aren't as specific as I'd like--I want to be able to use self types to select the right return type depending on the subclass of AbstractSet. So FrozenSet.__and__ and FrozenSet.__or__ should return some kind of FrozenSet.
I'm imagining something like this, except that I don't think it's possible to write SelfT[T1]:
Arguably AbstractSet[T] already has the correct return type, and if some subclasses of AbstractSet return more specific types, they should be indicated in the stubs/annotations for those classes, not in AbstractSet. Maybe MutableSet should override methods such as __or__ to return MutableSet instead.
Huh. I agree that then it makes sense for MutableSet and FrozenSet to override with their own, more specific types for these methods. I'll go post a typeshed change for that.
Is it still a useful example of the sort of annotation one might want to be able to write?
We have generic self types now. We don't have higher kinds
Just to clarify, it is still expected that we cannot yet change code like this
definverse(self) ->BidirectionalMapping[VT, KT]:
to this:
definverse(self) ->typing.Self[VT, KT]:
Is that right?
(When I try this with mypy 1.16.1, I get error: Self type cannot have type arguments. However, with that change and the removal of hacks like this, calling reveal_type(foo.inverse) (where foo is an instance of the concrete subclass bidict) actually somewhat works: Revealed type is "bidict._bidict.bidict[builtins.int, builtins.str]" -- there the key type and value type are incorrect (they should be str and int, respectively -- but the subclass type is correct.)
but that can be discussed elsewhere
I searched the tracker for another issue to subscribe to now that this is closed, and only found #6066. Is that the one you recommend following?
That doesn't work, as it discards the information that the key type and value type are swapped in the type returned by inverse, which is required for call sites to type check.
Already watching python/typing#548, thanks for making sure. Wondering if there are any other issues in the mypy tracker for the outstanding follow-up work (like allowing Self to accept type arguments).
AFAIK (I'm not a mypy maintainer, so I can't speak for them), no work on HKTs is currently planned. These days mypy generally follows the Python typing spec. A major typing feature like HKTs would likely first need to fleshed out in a PEP / typing spec revision before a mypy implementation is considered
Activity
jboning commentedon Dec 8, 2016
A motivating example is typeshed's AbstractSet:
With #2193 we can annotate
__and__, and it works beautifully: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.elazarg commentedon Apr 3, 2017
I think this issue is resolved - we can talk about the generic part if the self type. Or maybe I don't completely understand the issue.
jboning commentedon Apr 3, 2017
What would the annotated version of AbstractSet's
__or__method be using self types? I think that's a good illustrative example.elazarg commentedon Apr 3, 2017
I admit all these underscores confuse me, but maybe you wanted something like this?
(I am writing AS instead of AbstractSet to make it easier for me to read :) )
elazarg commentedon Apr 3, 2017
(actually I think
__and__should returnAS[Intersect[T1, T2]]if we had something like that)jboning commentedon Apr 3, 2017
Those annotations aren't as specific as I'd like--I want to be able to use self types to select the right return type depending on the subclass of AbstractSet. So
FrozenSet.__and__andFrozenSet.__or__should return some kind of FrozenSet.I'm imagining something like this, except that I don't think it's possible to write
SelfT[T1]:ilevkivskyi commentedon Apr 3, 2017
SelfT[T1]is this higher order kind? :-)JukkaL commentedon Apr 4, 2017
@jboning Yes, that is what this issue was originally about.
However, it's unclear if we'd want that annotation for
AbstractSet. For example, consider this example:Arguably
AbstractSet[T]already has the correct return type, and if some subclasses ofAbstractSetreturn more specific types, they should be indicated in the stubs/annotations for those classes, not inAbstractSet. MaybeMutableSetshould override methods such as__or__to returnMutableSetinstead.jboning commentedon Apr 5, 2017
Huh. I agree that then it makes sense for MutableSet and FrozenSet to override with their own, more specific types for these methods. I'll go post a typeshed change for that.
Is it still a useful example of the sort of annotation one might want to be able to write?
ilevkivskyi commentedon Aug 16, 2017
Raising priority since there were quite a few requests for this recently.
JukkaL commentedon Sep 18, 2017
Decreasing priority back to normal since this seems like a somewhat complicated feature and requires an addition to
mypy_extensions(and/or PEP 484).hauntsaninja commentedon Jun 21, 2025
We have generic self types now. We don't have higher kinds, but that can be discussed elsewhere
jab commentedon Jun 22, 2025
Just to clarify, it is still expected that we cannot yet change code like this
to this:
Is that right?
(When I try this with mypy 1.16.1, I get
error: Self type cannot have type arguments. However, with that change and the removal of hacks like this, callingreveal_type(foo.inverse)(where foo is an instance of the concrete subclassbidict) actually somewhat works:Revealed type is "bidict._bidict.bidict[builtins.int, builtins.str]"-- there the key type and value type are incorrect (they should bestrandint, respectively -- but the subclass type is correct.)I searched the tracker for another issue to subscribe to now that this is closed, and only found #6066. Is that the one you recommend following?
Thank you ❤️
brianschubert commentedon Jun 22, 2025
I think just
may be what you're looking for.
If you’re interested in proper HKTs, then python/typing#548 would be a good issue to watch.
jab commentedon Jun 22, 2025
That doesn't work, as it discards the information that the key type and value type are swapped in the type returned by
inverse, which is required for call sites to type check.Already watching python/typing#548, thanks for making sure. Wondering if there are any other issues in the mypy tracker for the outstanding follow-up work (like allowing
Selfto accept type arguments).brianschubert commentedon Jun 23, 2025
D'oh! Yeah, I completely overlooked that :)
AFAIK (I'm not a mypy maintainer, so I can't speak for them), no work on HKTs is currently planned. These days mypy generally follows the Python typing spec. A major typing feature like HKTs would likely first need to fleshed out in a PEP / typing spec revision before a mypy implementation is considered