Mercurial > hg
view mercurial/thirdparty/attr/validators.py @ 45830:c102b704edb5
global: use python3 in shebangs
Python 3 is the future. We want Python scripts to be using Python 3
by default.
This change updates all `#!/usr/bin/env python` shebangs to use
`python3`.
Does this mean all scripts use or require Python 3: no.
In the test environment, the `PATH` environment variable in tests is
updated to guarantee that the Python executable used to run
run-tests.py is used. Since test scripts all now use
`#!/usr/bin/env python3`, we had to update this code to install
a `python3` symlink instead of `python`.
It is possible there are some random scripts now executed with the
incorrect Python interpreter in some contexts. However, I would argue
that this was a pre-existing bug: we should almost always be executing
new Python processes using the `sys.executable` from the originating
Python script, as `python` or `python3` won't guarantee we'll use the
same interpreter.
Differential Revision: https://phab.mercurial-scm.org/D9273
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Fri, 06 Nov 2020 13:58:59 -0800 |
parents | 765eb17a7eb8 |
children | e1c586b9a43c |
line wrap: on
line source
""" Commonly useful validators. """ from __future__ import absolute_import, division, print_function from ._make import attr, attributes, and_, _AndValidator __all__ = [ "and_", "in_", "instance_of", "optional", "provides", ] @attributes(repr=False, slots=True, hash=True) class _InstanceOfValidator(object): type = attr() def __call__(self, inst, attr, value): """ We use a callable class to be able to change the ``__repr__``. """ if not isinstance(value, self.type): raise TypeError( "'{name}' must be {type!r} (got {value!r} that is a " "{actual!r})." .format(name=attr.name, type=self.type, actual=value.__class__, value=value), attr, self.type, value, ) def __repr__(self): return ( "<instance_of validator for type {type!r}>" .format(type=self.type) ) def instance_of(type): """ A validator that raises a :exc:`TypeError` if the initializer is called with a wrong type for this particular attribute (checks are perfomed using :func:`isinstance` therefore it's also valid to pass a tuple of types). :param type: The type to check for. :type type: type or tuple of types :raises TypeError: With a human readable error message, the attribute (of type :class:`attr.Attribute`), the expected type, and the value it got. """ return _InstanceOfValidator(type) @attributes(repr=False, slots=True, hash=True) class _ProvidesValidator(object): interface = attr() def __call__(self, inst, attr, value): """ We use a callable class to be able to change the ``__repr__``. """ if not self.interface.providedBy(value): raise TypeError( "'{name}' must provide {interface!r} which {value!r} " "doesn't." .format(name=attr.name, interface=self.interface, value=value), attr, self.interface, value, ) def __repr__(self): return ( "<provides validator for interface {interface!r}>" .format(interface=self.interface) ) def provides(interface): """ A validator that raises a :exc:`TypeError` if the initializer is called with an object that does not provide the requested *interface* (checks are performed using ``interface.providedBy(value)`` (see `zope.interface <https://zopeinterface.readthedocs.io/en/latest/>`_). :param zope.interface.Interface interface: The interface to check for. :raises TypeError: With a human readable error message, the attribute (of type :class:`attr.Attribute`), the expected interface, and the value it got. """ return _ProvidesValidator(interface) @attributes(repr=False, slots=True, hash=True) class _OptionalValidator(object): validator = attr() def __call__(self, inst, attr, value): if value is None: return self.validator(inst, attr, value) def __repr__(self): return ( "<optional validator for {what} or None>" .format(what=repr(self.validator)) ) def optional(validator): """ A validator that makes an attribute optional. An optional attribute is one which can be set to ``None`` in addition to satisfying the requirements of the sub-validator. :param validator: A validator (or a list of validators) that is used for non-``None`` values. :type validator: callable or :class:`list` of callables. .. versionadded:: 15.1.0 .. versionchanged:: 17.1.0 *validator* can be a list of validators. """ if isinstance(validator, list): return _OptionalValidator(_AndValidator(validator)) return _OptionalValidator(validator) @attributes(repr=False, slots=True, hash=True) class _InValidator(object): options = attr() def __call__(self, inst, attr, value): if value not in self.options: raise ValueError( "'{name}' must be in {options!r} (got {value!r})" .format(name=attr.name, options=self.options, value=value) ) def __repr__(self): return ( "<in_ validator with options {options!r}>" .format(options=self.options) ) def in_(options): """ A validator that raises a :exc:`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``. :param options: Allowed options. :type options: list, tuple, :class:`enum.Enum`, ... :raises ValueError: With a human readable error message, the attribute (of type :class:`attr.Attribute`), the expected options, and the value it got. .. versionadded:: 17.1.0 """ return _InValidator(options)