See sympy.core.compatibility, it already contains this.