A validator that wraps and logically 'inverts' the validator passed to it. It will raise a `ValueError` if the provided validator *doesn't* raise a `ValueError` or `TypeError` (by default), and will suppress the exception if the provided validator *does*. Intended to be used wi
(validator, *, msg=None, exc_types=(ValueError, TypeError))
| 669 | |
| 670 | |
| 671 | def not_(validator, *, msg=None, exc_types=(ValueError, TypeError)): |
| 672 | """ |
| 673 | A validator that wraps and logically 'inverts' the validator passed to it. |
| 674 | It will raise a `ValueError` if the provided validator *doesn't* raise a |
| 675 | `ValueError` or `TypeError` (by default), and will suppress the exception |
| 676 | if the provided validator *does*. |
| 677 | |
| 678 | Intended to be used with existing validators to compose logic without |
| 679 | needing to create inverted variants, for example, ``not_(in_(...))``. |
| 680 | |
| 681 | Args: |
| 682 | validator: A validator to be logically inverted. |
| 683 | |
| 684 | msg (str): |
| 685 | Message to raise if validator fails. Formatted with keys |
| 686 | ``exc_types`` and ``validator``. |
| 687 | |
| 688 | exc_types (tuple[type, ...]): |
| 689 | Exception type(s) to capture. Other types raised by child |
| 690 | validators will not be intercepted and pass through. |
| 691 | |
| 692 | Raises: |
| 693 | ValueError: |
| 694 | With a human readable error message, the attribute (of type |
| 695 | `attrs.Attribute`), the validator that failed to raise an |
| 696 | exception, the value it got, and the expected exception types. |
| 697 | |
| 698 | .. versionadded:: 22.2.0 |
| 699 | """ |
| 700 | try: |
| 701 | exc_types = tuple(exc_types) |
| 702 | except TypeError: |
| 703 | exc_types = (exc_types,) |
| 704 | return _NotValidator(validator, msg, exc_types) |
| 705 | |
| 706 | |
| 707 | @attrs(repr=False, slots=True, unsafe_hash=True) |