An int trait.
| 2591 | |
| 2592 | |
| 2593 | class Int(TraitType[G, S]): |
| 2594 | """An int trait.""" |
| 2595 | |
| 2596 | default_value = 0 |
| 2597 | info_text = "an int" |
| 2598 | |
| 2599 | @t.overload |
| 2600 | def __init__( |
| 2601 | self: Int[int, int], |
| 2602 | default_value: int | Sentinel = ..., |
| 2603 | allow_none: Literal[False] = ..., |
| 2604 | read_only: bool | None = ..., |
| 2605 | help: str | None = ..., |
| 2606 | config: t.Any | None = ..., |
| 2607 | **kwargs: t.Any, |
| 2608 | ) -> None: |
| 2609 | ... |
| 2610 | |
| 2611 | @t.overload |
| 2612 | def __init__( |
| 2613 | self: Int[int | None, int | None], |
| 2614 | default_value: int | Sentinel | None = ..., |
| 2615 | allow_none: Literal[True] = ..., |
| 2616 | read_only: bool | None = ..., |
| 2617 | help: str | None = ..., |
| 2618 | config: t.Any | None = ..., |
| 2619 | **kwargs: t.Any, |
| 2620 | ) -> None: |
| 2621 | ... |
| 2622 | |
| 2623 | def __init__( |
| 2624 | self, |
| 2625 | default_value: t.Any = Undefined, |
| 2626 | allow_none: bool = False, |
| 2627 | read_only: bool | None = None, |
| 2628 | help: str | None = None, |
| 2629 | config: t.Any | None = None, |
| 2630 | **kwargs: t.Any, |
| 2631 | ) -> None: |
| 2632 | self.min = kwargs.pop("min", None) |
| 2633 | self.max = kwargs.pop("max", None) |
| 2634 | super().__init__( |
| 2635 | default_value=default_value, |
| 2636 | allow_none=allow_none, |
| 2637 | read_only=read_only, |
| 2638 | help=help, |
| 2639 | config=config, |
| 2640 | **kwargs, |
| 2641 | ) |
| 2642 | |
| 2643 | def validate(self, obj: t.Any, value: t.Any) -> G: |
| 2644 | if not isinstance(value, int) and isinstance(value, numbers.Number): |
| 2645 | # allow casting integer-valued numbers to int |
| 2646 | # allows for more concise assignment like `4e9` which is a float |
| 2647 | try: |
| 2648 | int_value = int(value) |
| 2649 | if int_value == value: |
| 2650 | value = int_value |
no outgoing calls