A validator that raises a `ValueError` if the initializer is called with a value that does not belong in the *options* provided. The check is performed using ``value in options``, so *options* has to support that operation. To keep the validator hashable, dicts, lists, and set
(options)
| 257 | |
| 258 | |
| 259 | def in_(options): |
| 260 | class="st">""" |
| 261 | A validator that raises a `ValueError` if the initializer is called with a |
| 262 | value that does not belong in the *options* provided. |
| 263 | |
| 264 | The check is performed using ``value in options``, so *options* has to |
| 265 | support that operation. |
| 266 | |
| 267 | To keep the validator hashable, dicts, lists, and sets are transparently |
| 268 | transformed into a `tuple`. |
| 269 | |
| 270 | Args: |
| 271 | options: Allowed options. |
| 272 | |
| 273 | Raises: |
| 274 | ValueError: |
| 275 | With a human readable error message, the attribute (of type |
| 276 | `attrs.Attribute`), the expected options, and the value it got. |
| 277 | |
| 278 | .. versionadded:: 17.1.0 |
| 279 | .. versionchanged:: 22.1.0 |
| 280 | The ValueError was incomplete until now and only contained the human |
| 281 | readable error message. Now it contains all the information that has |
| 282 | been promised since 17.1.0. |
| 283 | .. versionchanged:: 24.1.0 |
| 284 | *options* that are a list, dict, or a set are now transformed into a |
| 285 | tuple to keep the validator hashable. |
| 286 | class="st">""" |
| 287 | repr_options = options |
| 288 | if isinstance(options, (list, dict, set)): |
| 289 | options = tuple(options) |
| 290 | |
| 291 | return _InValidator(options, repr_options) |
| 292 | |
| 293 | |
| 294 | @attrs(repr=False, slots=False, unsafe_hash=True) |