Validate that a number to be applied to a trait is between bounds. If value is not between min_bound and max_bound, this raises a TraitError with an error message appropriate for this trait.
(
trait: Int[t.Any, t.Any] | Float[t.Any, t.Any], obj: t.Any, value: t.Any
)
| 2564 | |
| 2565 | |
| 2566 | def _validate_bounds( |
| 2567 | trait: Int[t.Any, t.Any] | Float[t.Any, t.Any], obj: t.Any, value: t.Any |
| 2568 | ) -> t.Any: |
| 2569 | """ |
| 2570 | Validate that a number to be applied to a trait is between bounds. |
| 2571 | |
| 2572 | If value is not between min_bound and max_bound, this raises a |
| 2573 | TraitError with an error message appropriate for this trait. |
| 2574 | """ |
| 2575 | if trait.min is not None and value < trait.min: |
| 2576 | raise TraitError( |
| 2577 | f"The value of the '{trait.name}' trait of {class_of(obj)} instance should " |
| 2578 | f"not be less than {trait.min}, but a value of {value} was " |
| 2579 | "specified" |
| 2580 | ) |
| 2581 | if trait.max is not None and value > trait.max: |
| 2582 | raise TraitError( |
| 2583 | f"The value of the '{trait.name}' trait of {class_of(obj)} instance should " |
| 2584 | f"not be greater than {trait.max}, but a value of {value} was " |
| 2585 | "specified" |
| 2586 | ) |
| 2587 | return value |
| 2588 | |
| 2589 | |
| 2590 | # I = t.TypeVar('I', t.Optional[int], int) |