Immutable Sequence of float with length 3 as argument #2163
RubendeBruin
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I need some guidance on implementing type-hinting for 3d vectors.
We are trying to add type-hits to our 3d vector math functions. Many of these functions require a sequence of 3 floats as input. For example:
tuple[float, float, float] defines all that we need: The input argument shall have three indexable elements point[0], point[1], point[2] and their values shall be float.
Issue is that while duck-type compatible is also fine as input: so [1,2,3] (list of int) would also work without issues:
but in those case the type checker does raise a warning about incompatible types.
From the best practices I read that in cases like this a Generic type would be preferable:
but that does not cover the length.
If I now try:
the type checker no longer warns, but the code does not run as point[2] is not available.
Is there a way to get the best of both? Ie the strictness of tuple[float, float, float] but without the type-checker complaining when trying to pass a duck compatible input argument?
Bonus:
I would also like to hint that the values of the input argument are not mutated, (like const in c).
Beta Was this translation helpful? Give feedback.
All reactions