Skip to content

Commit f7d9ea2

Browse files
committed
refactor: py domain: Separate parse_reftarget() from type_to_xref()
1 parent 8ddf3f0 commit f7d9ea2

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

sphinx/domains/python.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,45 +80,53 @@ class ModuleEntry(NamedTuple):
8080
deprecated: bool
8181

8282

83-
def type_to_xref(target: str, env: BuildEnvironment = None, suppress_prefix: bool = False
84-
) -> addnodes.pending_xref:
85-
"""Convert a type string to a cross reference node."""
86-
if target == 'None' or target.startswith('typing.'):
83+
def parse_reftarget(reftarget: str, suppress_prefix: bool = False
84+
) -> Tuple[str, str, str, bool]:
85+
"""Parse a type string and return (reftype, reftarget, title, refspecific flag)"""
86+
refspecific = False
87+
if reftarget.startswith('.'):
88+
reftarget = reftarget[1:]
89+
title = reftarget
90+
refspecific = True
91+
elif reftarget.startswith('~'):
92+
reftarget = reftarget[1:]
93+
title = reftarget.split('.')[-1]
94+
elif suppress_prefix:
95+
title = reftarget.split('.')[-1]
96+
elif reftarget.startswith('typing.'):
97+
title = reftarget[7:]
98+
else:
99+
title = reftarget
100+
101+
if reftarget == 'None' or reftarget.startswith('typing.'):
87102
# typing module provides non-class types. Obj reference is good to refer them.
88103
reftype = 'obj'
89104
else:
90105
reftype = 'class'
91106

107+
return reftype, reftarget, title, refspecific
108+
109+
110+
def type_to_xref(target: str, env: BuildEnvironment = None, suppress_prefix: bool = False
111+
) -> addnodes.pending_xref:
112+
"""Convert a type string to a cross reference node."""
92113
if env:
93114
kwargs = {'py:module': env.ref_context.get('py:module'),
94115
'py:class': env.ref_context.get('py:class')}
95116
else:
96117
kwargs = {}
97118

98-
refspecific = False
99-
if target.startswith('.'):
100-
target = target[1:]
101-
text = target
102-
refspecific = True
103-
elif target.startswith('~'):
104-
target = target[1:]
105-
text = target.split('.')[-1]
106-
elif suppress_prefix:
107-
text = target.split('.')[-1]
108-
elif target.startswith('typing.'):
109-
text = target[7:]
110-
else:
111-
text = target
119+
reftype, target, title, refspecific = parse_reftarget(target, suppress_prefix)
112120

113121
if env.config.python_use_unqualified_type_names:
114122
# Note: It would be better to use qualname to describe the object to support support
115123
# nested classes. But python domain can't access the real python object because this
116124
# module should work not-dynamically.
117-
shortname = text.split('.')[-1]
125+
shortname = title.split('.')[-1]
118126
contnodes: List[Node] = [pending_xref_condition('', shortname, condition='resolved'),
119-
pending_xref_condition('', text, condition='*')]
127+
pending_xref_condition('', title, condition='*')]
120128
else:
121-
contnodes = [nodes.Text(text)]
129+
contnodes = [nodes.Text(title)]
122130

123131
return pending_xref('', *contnodes,
124132
refdomain='py', reftype=reftype, reftarget=target,

0 commit comments

Comments
 (0)