Migrate to py3 style type annotation: sphinx.versioning

This commit is contained in:
Takeshi KOMIYA 2019-12-25 01:20:44 +09:00
parent 12a61edd46
commit ea68fd5407

View File

@ -13,16 +13,18 @@ import warnings
from itertools import product, zip_longest
from operator import itemgetter
from os import path
from typing import Any, Dict, Iterator
from uuid import uuid4
from docutils import nodes
from docutils.nodes import Node
from sphinx.deprecation import RemovedInSphinx30Warning
from sphinx.transforms import SphinxTransform
if False:
# For type annotation
from typing import Any, Dict, Iterator # NOQA
from docutils import nodes # NOQA
from sphinx.application import Sphinx # NOQA
from sphinx.application import Sphinx
try:
import Levenshtein
@ -34,8 +36,7 @@ except ImportError:
VERSIONING_RATIO = 65
def add_uids(doctree, condition):
# type: (nodes.Node, Any) -> Iterator[nodes.Node]
def add_uids(doctree: Node, condition: Any) -> Iterator[Node]:
"""Add a unique id to every node in the `doctree` which matches the
condition and yield the nodes.
@ -50,8 +51,7 @@ def add_uids(doctree, condition):
yield node
def merge_doctrees(old, new, condition):
# type: (nodes.Node, nodes.Node, Any) -> Iterator[nodes.Node]
def merge_doctrees(old: Node, new: Node, condition: Any) -> Iterator[Node]:
"""Merge the `old` doctree with the `new` one while looking at nodes
matching the `condition`.
@ -118,8 +118,7 @@ def merge_doctrees(old, new, condition):
yield new_node
def get_ratio(old, new):
# type: (str, str) -> float
def get_ratio(old: str, new: str) -> float:
"""Return a "similiarity ratio" (in percent) representing the similarity
between the two strings where 0 is equal and anything above less than equal.
"""
@ -132,8 +131,7 @@ def get_ratio(old, new):
return levenshtein_distance(old, new) / (len(old) / 100.0)
def levenshtein_distance(a, b):
# type: (str, str) -> int
def levenshtein_distance(a: str, b: str) -> int:
"""Return the Levenshtein edit distance between two strings *a* and *b*."""
if a == b:
return 0
@ -157,8 +155,7 @@ class UIDTransform(SphinxTransform):
"""Add UIDs to doctree for versioning."""
default_priority = 880
def apply(self, **kwargs):
# type: (Any) -> None
def apply(self, **kwargs) -> None:
env = self.env
old_doctree = None
if not env.versioning_condition:
@ -180,8 +177,7 @@ class UIDTransform(SphinxTransform):
list(merge_doctrees(old_doctree, self.document, env.versioning_condition))
def prepare(document):
# type: (nodes.document) -> None
def prepare(document: nodes.document) -> None:
"""Simple wrapper for UIDTransform."""
warnings.warn('versioning.prepare() is deprecated. Use UIDTransform instead.',
RemovedInSphinx30Warning, stacklevel=2)
@ -189,8 +185,7 @@ def prepare(document):
transform.apply()
def setup(app):
# type: (Sphinx) -> Dict[str, Any]
def setup(app: "Sphinx") -> Dict[str, Any]:
app.add_transform(UIDTransform)
return {