-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Typing support for capsules #109562
Comments
Another example where capsules may be part of a protocol: apache/arrow#35531 |
Right now I don't think there's any way to refer to the type of capsules from Python. A good first step would be to add You also ask for a syntax like |
I'm not sure there's actually much benefit to exposing |
I'm quite new to typing, but the context is the definition of a protocol, not a single library, so a common way to spell out the type would be welcome. Contrast: >>> a = typing.NewType("DLTensorCapsule", object)
>>> b = typing.NewType("DLTensorCapsule", object)
>>> a == b
False
>>> c = typing.List[int]
>>> d = typing.List[int]
>>> c == d
True As for the fact that capsules don't have an accessible Python API, well, it's something that might be changed if desired. |
You can define |
@serhiy-storchaka , please, read the discussion above instead of reiterating previous comments. |
So, I submitted #109599 and #109600 for There, I get some pushback because it's supposedly not useful (??). But on this issue it's also explained that adding a parametric So what would be the solution to the problem described in the issue description above? The solution cannot be to define our own type in a third-party library, since there is no third-party library to speak of. It's a protocol specification that is independently implemented by different libraries. |
Why not just use |
If there's a need for a centralised type somewhere that can be shared between third-party libraries, I think I'd be okay with adding I still don't feel 100% clear about the use case, though, so I'd want to see good documentation about how the feature would be used. |
Well, for example, any library implementing the DLPack protocol (such as Numpy) could annotate its Array type like this: from typing import Capsule, Protocol
class DLPackExportable(Protocol):
def __dlpack__(self) -> Capsule["dltensor"]:
pass
class MyArray(object):
def __dlpack__(self) -> Capsule["dltensor"]:
...
@classmethod
def from_dlpack(self, obj: DLPackExportable) -> MyArray:
... (here I'm reusing the originally suggested notation, but you get the idea) I'll note the typing docs now promote proper typing annotations for objects supporting the buffer protocol. While it's true that the buffer protocol also provides a Python API through the |
I missed this part when I wrote my first reply; that does make the NewType solution harder. However, your suggestion also seems to rely on the string For your use case, wouldn't putting |
Since the pointer is type-erased in the capsule object (a In the DLPack case, the capsule name is part of the Python spec, so it is well-known by definition: """The producer must set the PyCapsule name to "dltensor" so that it can be inspected by name [...]""" Another in-progress spec, this time to convey the Arrow C Data Interface structs in Python, proposes a similar mechanism: (cc @wjones127 :-)
I suppose that would work. |
We added CapsuleType (#109600); can we close this? |
Ah, sorry, I had forgotten to close the issue. |
Feature or enhancement
Proposal:
While not common, capsule objects can appear in APIs. For example the Python DLPack protocol specifies a
__dlpack__
method returning a capsule object.It would therefore be nice to add typing support for capsules. Ideally it should be parameterable by the capsule name, because it often serves as a discriminator for the actual C type under the capsule.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: