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

Code before:

    @cached_property
    def resolution(self):
        return self.dtype.type(10)**-self.precision

    @cached_property
    def machep(self):
        return int(math.log2(self.eps))

    @cached_property
    def negep(self):
        return int(math.log2(self.epsneg))

    @cached_property
    def nexp(self):
        # considering all ones (inf/nan) and all zeros (subnormal/zero)
        return math.ceil(math.log2(self.maxexp - self.minexp + 2))

    @cached_property
    def iexp(self):
        # Calculate exponent bits from it's range:
        return math.ceil(math.log2(self.maxexp - self.minexp))

    def __str__(self):
        if hasattr(self, "_fmt") and self._fmt is not None:
            return self._fmt

        def get_str(name, pad=None):
            if (val := getattr(self, name)) is None:
                return "<undefined>"
            if pad is not None:
                s = str(val).ljust(pad)
            return str(val)

        precision = get_str("precision", 3)
        machep = get_str("machep", 6)
        negep = get_str("negep", 6)
        minexp = get_str("minexp", 6)
        maxexp = get_str("maxexp", 6)
        resolution = get_str("resolution")
        eps = get_str("eps")
        epsneg = get_str("epsneg")
        tiny = get_str("tiny")
        smallest_normal = get_str("smallest_normal")
        smallest_subnormal = get_str("smallest_subnormal")
        nexp = get_str("nexp", 6)
        max_ = get_str("max")
        if hasattr(self, "min") and hasattr(self, "max") and -self.min == self.max:
            min_ = "-max"
        else:
            min_ = get_str("min")

Review comment (suggestion):
```suggestion
        if (fmt := getattr(self, "_fmt", None)) is not None:
            return fmt
```

Code after:

    @cached_property
    def resolution(self):
        return self.dtype.type(10)**-self.precision

    @cached_property
    def machep(self):
        return int(math.log2(self.eps))

    @cached_property
    def negep(self):
        return int(math.log2(self.epsneg))

    @cached_property
    def nexp(self):
        # considering all ones (inf/nan) and all zeros (subnormal/zero)
        return math.ceil(math.log2(self.maxexp - self.minexp + 2))

    @cached_property
    def iexp(self):
        # Calculate exponent bits from it's range:
        return math.ceil(math.log2(self.maxexp - self.minexp))

    def __str__(self):
        if (fmt := getattr(self, "_fmt", None)) is not None:
            return fmt

        def get_str(name, pad=None):
            if (val := getattr(self, name, None)) is None:
                return "<undefined>"
            if pad is not None:
                s = str(val).ljust(pad)
            return str(val)

        precision = get_str("precision", 3)
        machep = get_str("machep", 6)
        negep = get_str("negep", 6)
        minexp = get_str("minexp", 6)
        maxexp = get_str("maxexp", 6)
        resolution = get_str("resolution")
        eps = get_str("eps")
        epsneg = get_str("epsneg")
        tiny = get_str("tiny")
        smallest_normal = get_str("smallest_normal")
        smallest_subnormal = get_str("smallest_subnormal")
        nexp = get_str("nexp", 6)
        max_ = get_str("max")
        if hasattr(self, "min") and hasattr(self, "max") and -self.min == self.max:
            min_ = "-max"
        else:
            min_ = get_str("min")