Bugs and Implementation Errors with `CastFunc`
We have observed a number of bugs and implementation errors with CastFunc
:
-
CastFunc
, as well asTypeAtom
, instances, cannot be unpickled becauseTypeAtom
initializes its state in__init__
, instead of__new__
which SymPy requires. - When called with a
sp.Symbol
,CastFunc
turns itself into aBooleanCastFunc
. TheBooleanCastFunc
is not very well documented. As I understand it, it should be used when the cast expression will be used as a boolean. If this is the purpose, its implementation is completely wrong. Instead of what it is doing right now, it should check whether the target type is a boolean type, and only then reinterpret itself as a boolean. - The way
CastFunc
exposes its assumptions to SymPy is inconsistent with the canonical way to implement these on custom function, see the sympy docs.
There's also some cosmetic and type safety issues:
- The name
CastFunc
is quite clunky; why not justcast
ortype_cast
? -
CastFunc
allows arbitrary casts, but to improve stability in the code generator, we might want to restrict that, and disallow casts between non-related types (e.g. casting from pointers to integers, things like that) in the typifier.
Together, these issues warrant a profound refactoring of CastFunc
.