Use `any` to find elements in iterable (#11053)

This commit is contained in:
danieleades 2023-01-02 04:52:46 +00:00 committed by GitHub
parent da6a20d50b
commit 2759c2c76b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 39 additions and 37 deletions

View File

@ -13,7 +13,6 @@ ignore =
SIM102,
SIM103,
SIM105,
SIM110,
SIM113,
SIM114,
SIM115,

View File

@ -168,6 +168,7 @@ external = [ # Whitelist for RUF100 unkown code warnings
"E704",
"W291",
"W293",
"SIM110",
]
select = [
"E", # pycodestyle

View File

@ -382,11 +382,11 @@ class HyperlinkAvailabilityCheckWorker(Thread):
return 'redirected', new_url, 0
def allowed_redirect(url: str, new_url: str) -> bool:
for from_url, to_url in self.config.linkcheck_allowed_redirects.items():
if from_url.match(url) and to_url.match(new_url):
return True
return False
return any(
from_url.match(url) and to_url.match(new_url)
for from_url, to_url
in self.config.linkcheck_allowed_redirects.items()
)
def check(docname: str) -> tuple[str, str, int]:
# check for various conditions without bothering the network

View File

@ -48,10 +48,10 @@ template_dir = path.join(package_dir, 'templates', 'apidoc')
def is_initpy(filename: str) -> bool:
"""Check *filename* is __init__ file or not."""
basename = path.basename(filename)
for suffix in sorted(PY_SUFFIXES, key=len, reverse=True):
if basename == '__init__' + suffix:
return True
return False
return any(
basename == '__init__' + suffix
for suffix in sorted(PY_SUFFIXES, key=len, reverse=True)
)
def module_join(*modnames: str) -> str:
@ -225,11 +225,10 @@ def walk(rootpath: str, excludes: list[str], opts: Any
def has_child_module(rootpath: str, excludes: list[str], opts: Any) -> bool:
"""Check the given directory contains child module/s (at least one)."""
for _root, _subs, files in walk(rootpath, excludes, opts):
if files:
return True
return False
return any(
files
for _root, _subs, files in walk(rootpath, excludes, opts)
)
def recurse_tree(rootpath: str, excludes: list[str], opts: Any,
@ -292,10 +291,10 @@ def is_excluded(root: str, excludes: list[str]) -> bool:
Note: by having trailing slashes, we avoid common prefix issues, like
e.g. an exclude "foo" also accidentally excluding "foobar".
"""
for exclude in excludes:
if fnmatch(root, exclude):
return True
return False
return any(
fnmatch(root, exclude)
for exclude in excludes
)
def get_parser() -> argparse.ArgumentParser:

View File

@ -130,10 +130,10 @@ class CoverageBuilder(Builder):
op.write('\n')
def ignore_pyobj(self, full_name: str) -> bool:
for exp in self.py_ignorexps:
if exp.search(full_name):
return True
return False
return any(
exp.search(full_name)
for exp in self.py_ignorexps
)
def build_py_coverage(self) -> None:
objects = self.env.domaindata['py']['objects']

View File

@ -536,7 +536,7 @@ class GoogleDocstring:
return [(' ' * n) + line for line in lines]
def _is_indented(self, line: str, indent: int = 1) -> bool:
for i, s in enumerate(line):
for i, s in enumerate(line): # noqa: SIM110
if i >= indent:
return True
elif not s.isspace():

View File

@ -333,10 +333,10 @@ class SphinxSmartQuotes(SmartQuotes, SphinxTransform):
# confirm selected language supports smart_quotes or not
language = self.env.settings['language_code']
for tag in normalize_language_tag(language):
if tag in smartchars.quotes:
return True
return False
return any(
tag in smartchars.quotes
for tag in normalize_language_tag(language)
)
def get_tokens(self, txtnodes: list[Text]) -> Generator[tuple[str, str], None, None]:
# A generator that yields ``(texttype, nodetext)`` tuples for a list

View File

@ -180,11 +180,14 @@ class ReferencesResolver(SphinxPostTransform):
warn = False
if self.config.nitpick_ignore_regex:
def matches_ignore(entry_type: str, entry_target: str) -> bool:
for ignore_type, ignore_target in self.config.nitpick_ignore_regex:
if re.fullmatch(ignore_type, entry_type) and \
re.fullmatch(ignore_target, entry_target):
return True
return False
return any(
(
re.fullmatch(ignore_type, entry_type)
and re.fullmatch(ignore_target, entry_target)
)
for ignore_type, ignore_target
in self.config.nitpick_ignore_regex
)
if matches_ignore(dtype, target):
warn = False
# for "std" types also try without domain name

View File

@ -220,10 +220,10 @@ def isstaticmethod(obj: Any, cls: Any = None, name: str | None = None) -> bool:
def isdescriptor(x: Any) -> bool:
"""Check if the object is some kind of descriptor."""
for item in '__get__', '__set__', '__delete__':
if callable(safe_getattr(x, item, None)):
return True
return False
return any(
callable(safe_getattr(x, item, None))
for item in ['__get__', '__set__', '__delete__']
)
def isabstractmethod(obj: Any) -> bool: