mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Migrate to py3 style type annotation: sphinx.util.tags
This commit is contained in:
parent
f3e45e485e
commit
39c7ee955b
@ -6,26 +6,23 @@
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
|
||||
# (ab)use the Jinja parser for parsing our boolean expressions
|
||||
from typing import Iterator, List
|
||||
|
||||
from jinja2 import nodes
|
||||
from jinja2.environment import Environment
|
||||
from jinja2.nodes import Node
|
||||
from jinja2.parser import Parser
|
||||
|
||||
env = Environment()
|
||||
|
||||
if False:
|
||||
# For type annotation
|
||||
from typing import Iterator, List # NOQA
|
||||
|
||||
|
||||
class BooleanParser(Parser):
|
||||
"""
|
||||
Only allow condition exprs and/or/not operations.
|
||||
"""
|
||||
|
||||
def parse_compare(self):
|
||||
# type: () -> nodes.Node
|
||||
node = None # type: nodes.Node
|
||||
def parse_compare(self) -> Node:
|
||||
node = None # type: Node
|
||||
token = self.stream.current
|
||||
if token.type == 'name':
|
||||
if token.value in ('true', 'false', 'True', 'False'):
|
||||
@ -46,38 +43,31 @@ class BooleanParser(Parser):
|
||||
|
||||
|
||||
class Tags:
|
||||
def __init__(self, tags=None):
|
||||
# type: (List[str]) -> None
|
||||
def __init__(self, tags: List[str] = None) -> None:
|
||||
self.tags = dict.fromkeys(tags or [], True)
|
||||
|
||||
def has(self, tag):
|
||||
# type: (str) -> bool
|
||||
def has(self, tag: str) -> bool:
|
||||
return tag in self.tags
|
||||
|
||||
__contains__ = has
|
||||
|
||||
def __iter__(self):
|
||||
# type: () -> Iterator[str]
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(self.tags)
|
||||
|
||||
def add(self, tag):
|
||||
# type: (str) -> None
|
||||
def add(self, tag: str) -> None:
|
||||
self.tags[tag] = True
|
||||
|
||||
def remove(self, tag):
|
||||
# type: (str) -> None
|
||||
def remove(self, tag: str) -> None:
|
||||
self.tags.pop(tag, None)
|
||||
|
||||
def eval_condition(self, condition):
|
||||
# type: (str) -> bool
|
||||
def eval_condition(self, condition: str) -> bool:
|
||||
# exceptions are handled by the caller
|
||||
parser = BooleanParser(env, condition, state='variable')
|
||||
expr = parser.parse_expression()
|
||||
if not parser.stream.eos:
|
||||
raise ValueError('chunk after expression')
|
||||
|
||||
def eval_node(node):
|
||||
# type: (nodes.Node) -> bool
|
||||
def eval_node(node: Node) -> bool:
|
||||
if isinstance(node, nodes.CondExpr):
|
||||
if eval_node(node.test): # type: ignore
|
||||
return eval_node(node.expr1) # type: ignore
|
||||
|
Loading…
Reference in New Issue
Block a user