aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/attrs/py3/attr/validators.py
diff options
context:
space:
mode:
authorAlexSm <alex@ydb.tech>2024-01-18 11:28:56 +0100
committerGitHub <noreply@github.com>2024-01-18 11:28:56 +0100
commit9d0a3761b3201e0d9db879a7adf91876ebdb0564 (patch)
tree541d11ac878c18efd7ebca81e35112aa0fef995b /contrib/python/attrs/py3/attr/validators.py
parent404ef8886ecc9736bc58ade6da2fbd83b486a408 (diff)
downloadydb-9d0a3761b3201e0d9db879a7adf91876ebdb0564.tar.gz
Library import 8 (#1074)
* Library import 8 * Add contrib/libs/cxxsupp/libcxx/include/__verbose_abort
Diffstat (limited to 'contrib/python/attrs/py3/attr/validators.py')
-rw-r--r--contrib/python/attrs/py3/attr/validators.py121
1 files changed, 41 insertions, 80 deletions
diff --git a/contrib/python/attrs/py3/attr/validators.py b/contrib/python/attrs/py3/attr/validators.py
index 1488554f78..34d6b761d3 100644
--- a/contrib/python/attrs/py3/attr/validators.py
+++ b/contrib/python/attrs/py3/attr/validators.py
@@ -97,23 +97,21 @@ class _InstanceOfValidator:
We use a callable class to be able to change the ``__repr__``.
"""
if not isinstance(value, self.type):
+ msg = "'{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,
+ )
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,
- ),
+ msg,
attr,
self.type,
value,
)
def __repr__(self):
- return "<instance_of validator for type {type!r}>".format(
- type=self.type
- )
+ return f"<instance_of validator for type {self.type!r}>"
def instance_of(type):
@@ -142,20 +140,18 @@ class _MatchesReValidator:
We use a callable class to be able to change the ``__repr__``.
"""
if not self.match_func(value):
+ msg = "'{name}' must match regex {pattern!r} ({value!r} doesn't)".format(
+ name=attr.name, pattern=self.pattern.pattern, value=value
+ )
raise ValueError(
- "'{name}' must match regex {pattern!r}"
- " ({value!r} doesn't)".format(
- name=attr.name, pattern=self.pattern.pattern, value=value
- ),
+ msg,
attr,
self.pattern,
value,
)
def __repr__(self):
- return "<matches_re validator for pattern {pattern!r}>".format(
- pattern=self.pattern
- )
+ return f"<matches_re validator for pattern {self.pattern!r}>"
def matches_re(regex, flags=0, func=None):
@@ -176,22 +172,17 @@ def matches_re(regex, flags=0, func=None):
"""
valid_funcs = (re.fullmatch, None, re.search, re.match)
if func not in valid_funcs:
- raise ValueError(
- "'func' must be one of {}.".format(
- ", ".join(
- sorted(
- e and e.__name__ or "None" for e in set(valid_funcs)
- )
- )
+ msg = "'func' must be one of {}.".format(
+ ", ".join(
+ sorted(e and e.__name__ or "None" for e in set(valid_funcs))
)
)
+ raise ValueError(msg)
if isinstance(regex, Pattern):
if flags:
- raise TypeError(
- "'flags' can only be used with a string pattern; "
- "pass flags to re.compile() instead"
- )
+ msg = "'flags' can only be used with a string pattern; pass flags to re.compile() instead"
+ raise TypeError(msg)
pattern = regex
else:
pattern = re.compile(regex, flags)
@@ -215,20 +206,18 @@ class _ProvidesValidator:
We use a callable class to be able to change the ``__repr__``.
"""
if not self.interface.providedBy(value):
+ msg = "'{name}' must provide {interface!r} which {value!r} doesn't.".format(
+ name=attr.name, interface=self.interface, value=value
+ )
raise TypeError(
- "'{name}' must provide {interface!r} which {value!r} "
- "doesn't.".format(
- name=attr.name, interface=self.interface, value=value
- ),
+ msg,
attr,
self.interface,
value,
)
def __repr__(self):
- return "<provides validator for interface {interface!r}>".format(
- interface=self.interface
- )
+ return f"<provides validator for interface {self.interface!r}>"
def provides(interface):
@@ -269,9 +258,7 @@ class _OptionalValidator:
self.validator(inst, attr, value)
def __repr__(self):
- return "<optional validator for {what} or None>".format(
- what=repr(self.validator)
- )
+ return f"<optional validator for {self.validator!r} or None>"
def optional(validator):
@@ -304,19 +291,16 @@ class _InValidator:
in_options = False
if not in_options:
+ msg = f"'{attr.name}' must be in {self.options!r} (got {value!r})"
raise ValueError(
- "'{name}' must be in {options!r} (got {value!r})".format(
- name=attr.name, options=self.options, value=value
- ),
+ msg,
attr,
self.options,
value,
)
def __repr__(self):
- return "<in_ validator with options {options!r}>".format(
- options=self.options
- )
+ return f"<in_ validator with options {self.options!r}>"
def in_(options):
@@ -402,11 +386,8 @@ class _DeepIterable:
else f" {self.iterable_validator!r}"
)
return (
- "<deep_iterable validator for{iterable_identifier}"
- " iterables of {member!r}>"
- ).format(
- iterable_identifier=iterable_identifier,
- member=self.member_validator,
+ f"<deep_iterable validator for{iterable_identifier}"
+ f" iterables of {self.member_validator!r}>"
)
@@ -477,19 +458,11 @@ class _NumberValidator:
We use a callable class to be able to change the ``__repr__``.
"""
if not self.compare_func(value, self.bound):
- raise ValueError(
- "'{name}' must be {op} {bound}: {value}".format(
- name=attr.name,
- op=self.compare_op,
- bound=self.bound,
- value=value,
- )
- )
+ msg = f"'{attr.name}' must be {self.compare_op} {self.bound}: {value}"
+ raise ValueError(msg)
def __repr__(self):
- return "<Validator for x {op} {bound}>".format(
- op=self.compare_op, bound=self.bound
- )
+ return f"<Validator for x {self.compare_op} {self.bound}>"
def lt(val):
@@ -549,11 +522,8 @@ class _MaxLengthValidator:
We use a callable class to be able to change the ``__repr__``.
"""
if len(value) > self.max_length:
- raise ValueError(
- "Length of '{name}' must be <= {max}: {len}".format(
- name=attr.name, max=self.max_length, len=len(value)
- )
- )
+ msg = f"Length of '{attr.name}' must be <= {self.max_length}: {len(value)}"
+ raise ValueError(msg)
def __repr__(self):
return f"<max_len validator for {self.max_length}>"
@@ -580,11 +550,8 @@ class _MinLengthValidator:
We use a callable class to be able to change the ``__repr__``.
"""
if len(value) < self.min_length:
- raise ValueError(
- "Length of '{name}' must be => {min}: {len}".format(
- name=attr.name, min=self.min_length, len=len(value)
- )
- )
+ msg = f"Length of '{attr.name}' must be >= {self.min_length}: {len(value)}"
+ raise ValueError(msg)
def __repr__(self):
return f"<min_len validator for {self.min_length}>"
@@ -611,22 +578,16 @@ class _SubclassOfValidator:
We use a callable class to be able to change the ``__repr__``.
"""
if not issubclass(value, self.type):
+ msg = f"'{attr.name}' must be a subclass of {self.type!r} (got {value!r})."
raise TypeError(
- "'{name}' must be a subclass of {type!r} "
- "(got {value!r}).".format(
- name=attr.name,
- type=self.type,
- value=value,
- ),
+ msg,
attr,
self.type,
value,
)
def __repr__(self):
- return "<subclass_of validator for type {type!r}>".format(
- type=self.type
- )
+ return f"<subclass_of validator for type {self.type!r}>"
def _subclass_of(type):
@@ -680,7 +641,7 @@ class _NotValidator:
def __repr__(self):
return (
- "<not_ validator wrapping {what!r}, " "capturing {exc_types!r}>"
+ "<not_ validator wrapping {what!r}, capturing {exc_types!r}>"
).format(
what=self.validator,
exc_types=self.exc_types,