Collapse `.startswith and .endswith` tests

This commit is contained in:
Adam Turner
2023-02-09 03:36:17 +00:00
parent ecfd08d325
commit fd3c619dbf
5 changed files with 7 additions and 10 deletions

View File

@@ -190,8 +190,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
# toctree-l[1-8] on the parent node?
if isinstance(doctree, nodes.reference) and doctree.get('refuri'):
refuri = doctree['refuri']
if refuri.startswith('http://') or refuri.startswith('https://') \
or refuri.startswith('irc:') or refuri.startswith('mailto:'):
if refuri.startswith(('http://', 'https://', 'irc:', 'mailto:')):
return result
classes = doctree.parent.attributes['classes']
for level in range(8, 0, -1): # or range(1, 8)?
@@ -343,8 +342,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
nr = 1
for node in list(tree.findall(nodes.reference)):
uri = node.get('refuri', '')
if (uri.startswith('http:') or uri.startswith('https:') or
uri.startswith('ftp:')) and uri not in node.astext():
if uri.startswith(('http:', 'https:', 'ftp:')) and uri not in node.astext():
idx = node.parent.index(node) + 1
if show_urls == 'inline':
uri = self.link_target_template % {'uri': uri}

View File

@@ -407,7 +407,7 @@ class BuildEnvironment:
containing document.
"""
filename = os_path(filename)
if filename.startswith('/') or filename.startswith(os.sep):
if filename.startswith(('/', os.sep)):
rel_fn = filename[1:]
else:
docdir = path.dirname(self.doc2path(docname or self.docname,

View File

@@ -991,14 +991,14 @@ def _token_type(token: str, location: str | None = None) -> str:
location=location,
)
type_ = "literal"
elif token.startswith("'") or token.startswith('"'):
elif token.startswith(("'", '"')):
logger.warning(
__("malformed string literal (missing closing quote): %s"),
token,
location=location,
)
type_ = "literal"
elif token.endswith("'") or token.endswith('"'):
elif token.endswith(("'", '"')):
logger.warning(
__("malformed string literal (missing opening quote): %s"),
token,

View File

@@ -8,7 +8,7 @@ from sphinx.util import logging
from sphinx.util.console import bold, colorize, term_width_line # type: ignore
if False:
from types import TracebackType # NoQA: TCH003
from types import TracebackType
logger = logging.getLogger(__name__)

View File

@@ -300,8 +300,7 @@ class ManualPageTranslator(SphinxTranslator, BaseTranslator):
self.body.append(self.defs['reference'][1])
uri = node.get('refuri', '')
if uri.startswith('mailto:') or uri.startswith('http:') or \
uri.startswith('https:') or uri.startswith('ftp:'):
if uri.startswith(('mailto:', 'http:', 'https:', 'ftp:')):
# if configured, put the URL after the link
if self.config.man_show_urls and node.astext() != uri:
if uri.startswith('mailto:'):