Polish matches_re docstring

This commit is contained in:
Hynek Schlawack 2019-09-09 11:05:04 +02:00
parent ef488122eb
commit 2a2e0f2ebb
1 changed files with 9 additions and 8 deletions

View File

@ -97,16 +97,16 @@ class _MatchesReValidator(object):
def matches_re(regex, flags=0, func=None): def matches_re(regex, flags=0, func=None):
""" """
A validator that raises :exc:`ValueError` if the initializer is called A validator that raises :exc:`ValueError` if the initializer is called
with a string that doesn't match the given regex, and :exc:`TypeError` with a string that doesn't match *regex*.
if the initializer is called with a non-string.
:param regex: a regex string to match against :param str regex: a regex string to match against
:param flags: flags that will be passed to the underlying re function :param int flags: flags that will be passed to the underlying re function
(default 0) (default 0)
:param func: which underlying re function to call (options are :param callable func: which underlying re function to call (options are
`re.fullmatch()`, `re.search()`, `re.match()`, default `re.fullmatch`) :func:`re.fullmatch`, :func:`re.search`, :func:`re.match`, default
:func:`re.fullmatch`)
.. versionadded:: 19.1.0 .. versionadded:: 19.2.0
""" """
fullmatch = getattr(re, "fullmatch", None) fullmatch = getattr(re, "fullmatch", None)
valid_funcs = (fullmatch, None, re.search, re.match) valid_funcs = (fullmatch, None, re.search, re.match)
@ -116,7 +116,6 @@ def matches_re(regex, flags=0, func=None):
", ".join([repr(e) for e in set(valid_funcs)]) ", ".join([repr(e) for e in set(valid_funcs)])
) )
) )
# non-int flags gives an okay error message in re, so rely on that
if func is None: if func is None:
if fullmatch: if fullmatch:
func = fullmatch func = fullmatch
@ -127,6 +126,8 @@ def matches_re(regex, flags=0, func=None):
# avoid swallowing TypeError if regex is not basestring # avoid swallowing TypeError if regex is not basestring
regex = r"(?:{})\Z".format(regex) regex = r"(?:{})\Z".format(regex)
func = re.match func = re.match
# non-int flags gives an okay error message in re, so rely on that
regex = re.compile(regex, flags) regex = re.compile(regex, flags)
if func is re.match: if func is re.match:
match_func = regex.match match_func = regex.match