Replies: 1 comment 1 reply
-
So, of course, as soon as I submitted this, I dug into the PEP for generics and discovered the I'm not yet sure if that will address my second issue, but it definitely won't address the first issue. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have two closely (I think) related issues I'm encountering:
To start, I have a function I pass a dictionary to, and I want to provide a type hint for the argument that's a typed dict (subclass of a
typing.TypedDict
). I'm fine with having the underlying C++ function just take apy::dict
, but I'd still like to add type-hint annotation in the exposed function. My first attempt was to create an accompanying .pyi file where I defined the typed dict and annotated the function with it. This works only as long as python itself never attempts to resolve the typed dict, which results in a runtime exception because the .so itself doesn't actually have a definition for it. I know I can use the trick of putting type hints in quotes to keep python from doing this, but I want to avoid having to use that "solution."I think the better answer would be to create typed dict in the .so, then use the .pyi only for annotating the function. However, I haven't found any examples of creating a typedict in pybind or a more general example of manually building up a native python class object that's attached to the module.
The second situation above is similar to the first, in that I have a class in the .so that I want to treat as a generic. The implementation class has a
py::dict
field, but at the python level, I want to be able to annotate with the class asmyclass[MyTypedDict]
. Again, .pyi manipulation suffices to make tools like pylance happy, but python itself raises an exception that the real type isn't subscriptable.I'm not sure that the solution for both of these issues is the same. But I think it speaks to a more general issue of providing type hints of this nature (i.e. of types that exist only for type-hinting purposes).
My hope is that someone has already figured this out and can share how they did it. So, recapping, my question(s) are:
Beta Was this translation helpful? Give feedback.
All reactions