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.
|
: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 import nodes
|
||||||
from jinja2.environment import Environment
|
from jinja2.environment import Environment
|
||||||
|
from jinja2.nodes import Node
|
||||||
from jinja2.parser import Parser
|
from jinja2.parser import Parser
|
||||||
|
|
||||||
env = Environment()
|
env = Environment()
|
||||||
|
|
||||||
if False:
|
|
||||||
# For type annotation
|
|
||||||
from typing import Iterator, List # NOQA
|
|
||||||
|
|
||||||
|
|
||||||
class BooleanParser(Parser):
|
class BooleanParser(Parser):
|
||||||
"""
|
"""
|
||||||
Only allow condition exprs and/or/not operations.
|
Only allow condition exprs and/or/not operations.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def parse_compare(self):
|
def parse_compare(self) -> Node:
|
||||||
# type: () -> nodes.Node
|
node = None # type: Node
|
||||||
node = None # type: nodes.Node
|
|
||||||
token = self.stream.current
|
token = self.stream.current
|
||||||
if token.type == 'name':
|
if token.type == 'name':
|
||||||
if token.value in ('true', 'false', 'True', 'False'):
|
if token.value in ('true', 'false', 'True', 'False'):
|
||||||
@ -46,38 +43,31 @@ class BooleanParser(Parser):
|
|||||||
|
|
||||||
|
|
||||||
class Tags:
|
class Tags:
|
||||||
def __init__(self, tags=None):
|
def __init__(self, tags: List[str] = None) -> None:
|
||||||
# type: (List[str]) -> None
|
|
||||||
self.tags = dict.fromkeys(tags or [], True)
|
self.tags = dict.fromkeys(tags or [], True)
|
||||||
|
|
||||||
def has(self, tag):
|
def has(self, tag: str) -> bool:
|
||||||
# type: (str) -> bool
|
|
||||||
return tag in self.tags
|
return tag in self.tags
|
||||||
|
|
||||||
__contains__ = has
|
__contains__ = has
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self) -> Iterator[str]:
|
||||||
# type: () -> Iterator[str]
|
|
||||||
return iter(self.tags)
|
return iter(self.tags)
|
||||||
|
|
||||||
def add(self, tag):
|
def add(self, tag: str) -> None:
|
||||||
# type: (str) -> None
|
|
||||||
self.tags[tag] = True
|
self.tags[tag] = True
|
||||||
|
|
||||||
def remove(self, tag):
|
def remove(self, tag: str) -> None:
|
||||||
# type: (str) -> None
|
|
||||||
self.tags.pop(tag, None)
|
self.tags.pop(tag, None)
|
||||||
|
|
||||||
def eval_condition(self, condition):
|
def eval_condition(self, condition: str) -> bool:
|
||||||
# type: (str) -> bool
|
|
||||||
# exceptions are handled by the caller
|
# exceptions are handled by the caller
|
||||||
parser = BooleanParser(env, condition, state='variable')
|
parser = BooleanParser(env, condition, state='variable')
|
||||||
expr = parser.parse_expression()
|
expr = parser.parse_expression()
|
||||||
if not parser.stream.eos:
|
if not parser.stream.eos:
|
||||||
raise ValueError('chunk after expression')
|
raise ValueError('chunk after expression')
|
||||||
|
|
||||||
def eval_node(node):
|
def eval_node(node: Node) -> bool:
|
||||||
# type: (nodes.Node) -> bool
|
|
||||||
if isinstance(node, nodes.CondExpr):
|
if isinstance(node, nodes.CondExpr):
|
||||||
if eval_node(node.test): # type: ignore
|
if eval_node(node.test): # type: ignore
|
||||||
return eval_node(node.expr1) # type: ignore
|
return eval_node(node.expr1) # type: ignore
|
||||||
|
Loading…
Reference in New Issue
Block a user