Repository: numpy/numpy
File: numpy/_core/_exceptions.py (Python)

Code before:


@_display_as_base
class _UFuncCastingError(UFuncTypeError):
    def __init__(self, ufunc, casting, from_, to):
        super().__init__(ufunc)
        self.casting = casting
        self.from_ = from_
        self.to = to


@_display_as_base
class _UFuncInputCastingError(_UFuncCastingError):
    """ Thrown when a ufunc input cannot be casted """
    def __init__(self, ufunc, casting, from_, to, i):
        super().__init__(ufunc, casting, from_, to)
        self.in_i = i

    def __str__(self):
        # only show the number if more than one input exists
        i_str = f"{self.in_i} " if self.ufunc.nin != 1 else ""
        return (
            f"Cannot cast ufunc {self.ufunc.__name__!r} input {i_str} "
            f"from {self.from_!r} to {self.to!r} with casting rule {self.casting!r}"
        )


@_display_as_base
class _UFuncOutputCastingError(_UFuncCastingError):
    """ Thrown when a ufunc output cannot be casted """
    def __init__(self, ufunc, casting, from_, to, i):
        super().__init__(ufunc, casting, from_, to)
        self.out_i = i

    def __str__(self):
        # only show the number if more than one output exists
        i_str = f"{self.out_i} " if self.ufunc.nout != 1 else ""
        return (
            f"Cannot cast ufunc {self.ufunc.__name__!r} output {i_str} "
            f"from {self.from_!r} to {self.to!r} with casting rule {self.casting!r}"
        )


@_display_as_base
class _ArrayMemoryError(MemoryError):
    """ Thrown when an array cannot be allocated"""
    def __init__(self, shape, dtype):
        self.shape = shape
        self.dtype = dtype

    @property

Review comment (suggestion):
I thought the "missing" space in `input {}from` is an error, but it's actually required because the argument `i_str` itself ends with a space. Will fix this.

Code after:


@_display_as_base
class _UFuncCastingError(UFuncTypeError):
    def __init__(self, ufunc, casting, from_, to):
        super().__init__(ufunc)
        self.casting = casting
        self.from_ = from_
        self.to = to


@_display_as_base
class _UFuncInputCastingError(_UFuncCastingError):
    """ Thrown when a ufunc input cannot be casted """
    def __init__(self, ufunc, casting, from_, to, i):
        super().__init__(ufunc, casting, from_, to)
        self.in_i = i

    def __str__(self):
        # only show the number if more than one input exists
        i_str = f"{self.in_i} " if self.ufunc.nin != 1 else ""
        return (
            f"Cannot cast ufunc {self.ufunc.__name__!r} input {i_str}from "
            f"{self.from_!r} to {self.to!r} with casting rule {self.casting!r}"
        )


@_display_as_base
class _UFuncOutputCastingError(_UFuncCastingError):
    """ Thrown when a ufunc output cannot be casted """
    def __init__(self, ufunc, casting, from_, to, i):
        super().__init__(ufunc, casting, from_, to)
        self.out_i = i

    def __str__(self):
        # only show the number if more than one output exists
        i_str = f"{self.out_i} " if self.ufunc.nout != 1 else ""
        return (
            f"Cannot cast ufunc {self.ufunc.__name__!r} output {i_str}from "
            f"{self.from_!r} to {self.to!r} with casting rule {self.casting!r}"
        )


@_display_as_base
class _ArrayMemoryError(MemoryError):
    """ Thrown when an array cannot be allocated"""
    def __init__(self, shape, dtype):
        self.shape = shape
        self.dtype = dtype

    @property