Merge pull request #5710 from tk0miya/fix_typehints_for_XRefRole

Fix typehints for XRefRole
This commit is contained in:
Takeshi KOMIYA 2018-12-03 22:06:11 +09:00 committed by GitHub
commit e888e92ac4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 12 deletions

View File

@ -238,7 +238,7 @@ class CObject(ObjectDescription):
class CXRefRole(XRefRole): class CXRefRole(XRefRole):
def process_link(self, env, refnode, has_explicit_title, title, target): def process_link(self, env, refnode, has_explicit_title, title, target):
# type: (BuildEnvironment, nodes.Node, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA # type: (BuildEnvironment, nodes.Element, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA
if not has_explicit_title: if not has_explicit_title:
target = target.lstrip('~') # only has a meaning for the title target = target.lstrip('~') # only has a meaning for the title
# if the first character is a tilde, don't display the module/class # if the first character is a tilde, don't display the module/class

View File

@ -6560,7 +6560,7 @@ class CPPNamespacePopObject(SphinxDirective):
class CPPXRefRole(XRefRole): class CPPXRefRole(XRefRole):
def process_link(self, env, refnode, has_explicit_title, title, target): def process_link(self, env, refnode, has_explicit_title, title, target):
# type: (BuildEnvironment, nodes.Node, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA # type: (BuildEnvironment, nodes.Element, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA
refnode.attributes.update(env.ref_context) refnode.attributes.update(env.ref_context)
if not has_explicit_title: if not has_explicit_title:

View File

@ -273,7 +273,7 @@ class JSModule(SphinxDirective):
class JSXRefRole(XRefRole): class JSXRefRole(XRefRole):
def process_link(self, env, refnode, has_explicit_title, title, target): def process_link(self, env, refnode, has_explicit_title, title, target):
# type: (BuildEnvironment, nodes.reference, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA # type: (BuildEnvironment, nodes.Element, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA
# basically what sphinx.domains.python.PyXRefRole does # basically what sphinx.domains.python.PyXRefRole does
refnode['js:object'] = env.ref_context.get('js:object') refnode['js:object'] = env.ref_context.get('js:object')
refnode['js:module'] = env.ref_context.get('js:module') refnode['js:module'] = env.ref_context.get('js:module')

View File

@ -33,7 +33,7 @@ logger = logging.getLogger(__name__)
class MathReferenceRole(XRefRole): class MathReferenceRole(XRefRole):
def result_nodes(self, document, env, node, is_ref): def result_nodes(self, document, env, node, is_ref):
# type: (nodes.Node, BuildEnvironment, nodes.Element, bool) -> Tuple[List[nodes.Node], List[nodes.system_message]] # NOQA # type: (nodes.document, BuildEnvironment, nodes.Element, bool) -> Tuple[List[nodes.Node], List[nodes.system_message]] # NOQA
node['refdomain'] = 'math' node['refdomain'] = 'math'
return [node], [] return [node], []

View File

@ -95,7 +95,7 @@ class EnvVarXRefRole(XRefRole):
""" """
def result_nodes(self, document, env, node, is_ref): def result_nodes(self, document, env, node, is_ref):
# type: (nodes.document, BuildEnvironment, nodes.Element, bool) -> Tuple[List[nodes.Element], List[nodes.system_message]] # NOQA # type: (nodes.document, BuildEnvironment, nodes.Element, bool) -> Tuple[List[nodes.Node], List[nodes.system_message]] # NOQA
if not is_ref: if not is_ref:
return [node], [] return [node], []
varname = node['reftarget'] varname = node['reftarget']

View File

@ -121,8 +121,7 @@ class XRefRole:
if self.fix_parens: if self.fix_parens:
text, tgt = self._fix_parens(env, False, text, "") text, tgt = self._fix_parens(env, False, text, "")
innernode = self.innernodeclass(rawtext, text, classes=classes) innernode = self.innernodeclass(rawtext, text, classes=classes)
return self.result_nodes(inliner.document, env, innernode, return self.result_nodes(inliner.document, env, innernode, is_ref=False)
is_ref=False)
# split title and target in role content # split title and target in role content
has_explicit_title, title, target = split_explicit_title(text) has_explicit_title, title, target = split_explicit_title(text)
title = utils.unescape(title) title = utils.unescape(title)
@ -138,8 +137,7 @@ class XRefRole:
refexplicit=has_explicit_title) refexplicit=has_explicit_title)
# we may need the line number for warnings # we may need the line number for warnings
set_role_source_info(inliner, lineno, refnode) # type: ignore set_role_source_info(inliner, lineno, refnode) # type: ignore
title, target = self.process_link( title, target = self.process_link(env, refnode, has_explicit_title, title, target)
env, refnode, has_explicit_title, title, target)
# now that the target and title are finally determined, set them # now that the target and title are finally determined, set them
refnode['reftarget'] = target refnode['reftarget'] = target
refnode += self.innernodeclass(rawtext, title, classes=classes) refnode += self.innernodeclass(rawtext, title, classes=classes)
@ -152,7 +150,7 @@ class XRefRole:
# methods that can be overwritten # methods that can be overwritten
def process_link(self, env, refnode, has_explicit_title, title, target): def process_link(self, env, refnode, has_explicit_title, title, target):
# type: (BuildEnvironment, nodes.reference, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA # type: (BuildEnvironment, nodes.Element, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA
"""Called after parsing title and target text, and creating the """Called after parsing title and target text, and creating the
reference node (given in *refnode*). This method can alter the reference node (given in *refnode*). This method can alter the
reference node and must return a new (or the same) ``(title, target)`` reference node and must return a new (or the same) ``(title, target)``
@ -161,7 +159,7 @@ class XRefRole:
return title, ws_re.sub(' ', target) return title, ws_re.sub(' ', target)
def result_nodes(self, document, env, node, is_ref): def result_nodes(self, document, env, node, is_ref):
# type: (nodes.document, BuildEnvironment, nodes.Element, bool) -> Tuple[List[nodes.Element], List[nodes.system_message]] # NOQA # type: (nodes.document, BuildEnvironment, nodes.Element, bool) -> Tuple[List[nodes.Node], List[nodes.system_message]] # NOQA
"""Called before returning the finished nodes. *node* is the reference """Called before returning the finished nodes. *node* is the reference
node if one was created (*is_ref* is then true), else the content node. node if one was created (*is_ref* is then true), else the content node.
This method can add other nodes and must return a ``(nodes, messages)`` This method can add other nodes and must return a ``(nodes, messages)``
@ -172,7 +170,7 @@ class XRefRole:
class AnyXRefRole(XRefRole): class AnyXRefRole(XRefRole):
def process_link(self, env, refnode, has_explicit_title, title, target): def process_link(self, env, refnode, has_explicit_title, title, target):
# type: (BuildEnvironment, nodes.reference, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA # type: (BuildEnvironment, nodes.Element, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA
result = super(AnyXRefRole, self).process_link(env, refnode, has_explicit_title, result = super(AnyXRefRole, self).process_link(env, refnode, has_explicit_title,
title, target) title, target)
# add all possible context info (i.e. std:program, py:module etc.) # add all possible context info (i.e. std:program, py:module etc.)