-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix issue #3912 : invalid docstring generation of std::array <-> list caster #3916
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
Fix issue #3912 : invalid docstring generation of std::array <-> list caster #3916
Conversation
This PR achieves MyPy compliance in exchange for potentially very confusing situations where a pybind11-bound function does not accept an input array, and the error message provides zero context why that is (input has the wrong size) |
These signatures in pybind11 precede the time where MyPy was even a thing, so "non-standard non-spec compliant" seems a bit strong of a qualifier. |
@wjacob, in >=3.9 we can use the Annotated typing extension to specify the length of the list. Hmm, not sure if there is a standard compliant way to specify the type for anything besides Tuples though. |
const_name("[") + const_name<Size>() | ||
+ const_name("]")) | ||
+ const_name("]")); | ||
PYBIND11_TYPE_CASTER(ArrayType, const_name("List[") + value_conv::name + const_name("]")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you make the string typing.Annotated[List[type], size]
then pybind11-stubgen will generate the correct thing from this (and if it doesn't then you can just modify it to do so).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also note, it's just a string. You don't need typing extensions in pybind11, that's the responsibility of whoever interprets the string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annotated[List[type], size]
appears to be the intended standard way to hint the length of a list. PEP 593 shows this use case as an example.
I would just remove typing.
from your suggestion since List
is not prefixed either.
This PR achieves MyPy compliance in exchange for potentially very confusing situations where a pybind11-bound function does not accept an input array, and the error message provides zero context why that is (input has the wrong size)
@wjakob My understanding is that this PR only changes the type hint and does not affect the error message otherwise. Is there a situation where Annotated[List[str], 2]
would be inferior to List[str[2]]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a situation where Annotated[List[str], 2] would be inferior to List[str[2]]?
According to #4679 (comment) , plain 2 does not look good
@Skylion007 @rwgk can we close this one? |
Closing after #4679 was merged. |
Description
std::array
? #3912, by removing size information from list, while this information is helpful, it generates invalid type signatures for mypy and other type checkers.Suggested changelog entry: