Skip to content

Bugs and Implementation Errors with `CastFunc`

We have observed a number of bugs and implementation errors with CastFunc:

  • CastFunc, as well as TypeAtom, instances, cannot be unpickled because TypeAtom initializes its state in __init__, instead of __new__ which SymPy requires.
  • When called with a sp.Symbol, CastFunc turns itself into a BooleanCastFunc. The BooleanCastFunc 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 just cast or type_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.