def set_number_of_neighbors_with_bounds(
        self, number_of_neighbors, allowed_range=None, allowed_set=None
    ):
        """
        Set the numberOfNeighbors parameter for the KNearestNeighborsClassifier model.

        Parameters
        ----------
        allowed_range
                Tuple of (``min_value``, ``max_value``) defining the range of allowed values.

        allowed_values
                Set of allowed values for the number of neighbors.

        Returns
        -------
        None
        """
        if number_of_neighbors <= 0:
            raise ValueError("number_of_neighbors must be > 0")
        if allowed_range is None and allowed_set is None:
            raise ValueError(
                "Exactly one of allowed_range or allowed_values must be provided"
            )
        if allowed_range is not None and allowed_set is not None:
            raise ValueError(
                "Exactly one of allowed_range or allowed_values must be provided"
            )

        if allowed_range is not None:
            if not isinstance(allowed_range, tuple):
                raise TypeError(
                    "allowed_range expects a tuple of (min_value, max_value)"
                )
            if len(allowed_range) != 2:
                raise TypeError(
                    "allowed_range expects a tuple of (min_value, max_value)"
                )

            (min_value, max_value) = allowed_range
            if min_value <= 0:
                raise ValueError("allowed_range minimum must be > 0")
            if max_value < min_value:
                raise ValueError("allowed_range max_value must be >= min_value")
            if number_of_neighbors < min_value or number_of_neighbors > max_value:
                raise ValueError("number_of_neighbors is not within allowed range")

            self.spec.kNearestNeighborsClassifier.numberOfNeighbors.defaultValue = (
                number_of_neighbors
            )
            self.spec.kNearestNeighborsClassifier.numberOfNeighbors.range.minValue = (
                min_value
            )
            self.spec.kNearestNeighborsClassifier.numberOfNeighbors.range.maxValue = (
                max_value
            )

        elif allowed_set is not None:
            if not isinstance(allowed_set, set):
                raise TypeError("allowed_values expects 'set' type")
            if len(allowed_set) == 0:
                raise TypeError("allowed_values cannot be empty")

            found_match = False
            for v in allowed_set:
                if not self._is_valid_number_type(v):
                    raise TypeError("allowed_values must contain only integer types")
                if v <= 0:
                    raise TypeError("allowed_values must only contain values > 0")
                if number_of_neighbors == v:
                    found_match = True

            if found_match:
                self.spec.kNearestNeighborsClassifier.numberOfNeighbors.defaultValue = (
                    number_of_neighbors
                )
                for v in allowed_set:
                    self.spec.kNearestNeighborsClassifier.numberOfNeighbors.set.values.append(
                        v
                    )
            else:
                raise ValueError("number_of_neighbors is not a valid value")

        Set the numberOfNeighbors parameter for the KNearestNeighborsClassifier model.

        Parameters
        ----------
        allowed_range
                Tuple of (``min_value``, ``max_value``) defining the range of allowed values.

        allowed_values
                Set of allowed values for the number of neighbors.

        Returns
        -------
        None
        
coremltools/models/nearest_neighbors/builder.py