Replies: 1 comment
-
Slices as type parameters could be very neat for expressing multi-dimensional array types in the scientific Python stack, but I think it needs buy-in from e.g. the NumPy typing folks and a formalised proposal. I don't think this'll happen for a while, so I'd draft up one or both of the following as a proof-of-concept:
As for dynamically-created classes/types, a hook is exposed under the mypy plugin system for this (see A more serious issue would be that Cython performs a lot of type coersions for both C-Python-C and C++-Python-C++, so it is unclear whether a passing e.g. Python I guess the conclusion is, getting Cython to work robustly isn't possible in type-checkers without a plugin system, and since mypy seems to be the only widely-used type-checker with plugins, there's nothing to be done here for other type-checkers, which would just have to deal with |
Beta Was this translation helpful? Give feedback.
-
In the Cython project, I've been discussing an issue with how they implement their type annotations (cython/cython#6272). Cython does some absolute magic where they hook into the annotations for certain specific types written in Python code, which when passed to the cython builder re-interpret the python code as cython code.
There are two specific issues I've noticed with writing type hints for their code so far (I've not used their pure python side until recently).
Issue 1 - Cython memoryviews
One is with regards to Cython memoryviews. It is valid "pure Python" Cython code to write the following function
The wanted annotations here are something like
x: float
,y_arr: Buffer
.But I am having trouble trying to write a type stub that allows you to use
cython.double
as an alias for one type, andcython.double[:]
as an alias for another type.cython.double
to a generic type gives "Expected type expression but received 'slice'" issues forcython.double
.typing.Any
does stop type checkers throwing the error, but not sure if this is really expected behaviour or just lucky short-circuiting.cython.double = CythonDouble
, whereBut in this case, both
x
andy
have revealed type ofCythonDouble
, so trying to get different type semantics is hard. And it feels super hacky.As I mentioned in the Cython issue, I'm not sure if
SomeType[:]
is ever intended type expression syntax, so they might be fighting a losing battle. But Cython has an old codebase and there is a lot of code that uses this, so if there's a way to make it work that's better than just usingtyping.Any
, that would be great.Issue 2 - dynamically created classes
Cython also supports the instantiation of C structs, and their use as return types. The following is valid Cython
But I have not been able to find a way to type
cython.struct
to allow this behaviour. Even returningtyping.Any
gives a "Variable not allowed in type expression" error in the function return value annotation.Any ideas on how this could be achieved?
Beta Was this translation helpful? Give feedback.
All reactions