Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix keyerror on refexplicit and refdoc #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions azure-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ jobs:
- job: ${{ format(parameters.name) }}
pool:
${{ if eq(parameters.os, 'macosx') }}:
vmImage: macOS 10.13
vmImage: 'macOS-latest'
${{ if eq(parameters.os, 'linux') }}:
vmImage: Ubuntu 16.04
vmImage: 'ubuntu-latest'
${{ if eq(parameters.os, 'windows') }}:
vmImage: vs2017-win2016
vmImage: 'vs2017-win2016'

steps:

Expand Down Expand Up @@ -40,7 +40,7 @@ jobs:
versionSpec: '3.7'
architecture: 'x64'

- script: pip install tox coverage
- script: pip install tox "coverage<5.0"
displayName: Installing tox and coverage

- script: tox
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test =
pytest-cov
cython
codecov
coverage
coverage < 5.0

[options.package_data]
sphinx_automodapi = templates/*/*.rst
Expand Down
2 changes: 1 addition & 1 deletion sphinx_automodapi/automodapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class are included in the generated documentation. Defaults to ``False``.
def automodapi_replace(sourcestr, app, dotoctree=True, docname=None,
warnings=True):
"""
Replaces `sourcestr`'s entries of ".. automdapi::" with the
Replaces `sourcestr`'s entries of ".. automodapi::" with the
automodapi template form based on provided options.

This is used with the sphinx event 'source-read' to replace
Expand Down
50 changes: 41 additions & 9 deletions sphinx_automodapi/smart_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,53 @@ def merge_mapping(app, env, docnames, env_other):


def missing_reference_handler(app, env, node, contnode):
"""
Handler to be connect to the sphinx 'missing-reference' event. The handler a
resolves reference (node) and returns a new node when sphinx could not
originally resolve the reference.

see `missing-reference in sphinx documentation
<https://www.sphinx-doc.org/en/master/extdev/appapi.html#event-missing-reference>`_

:param app: The Sphinx application object
:param env: The build environment (``app.builder.env`)
:param node: The ``pending_xref`` node to be resolved. Its attributes reftype,
reftarget, modname and classname attributes determine the type and
target of the reference.
:param contnode: The node that carries the text and formatting inside the
future reference and should be a child of the returned
reference node.
"""
# a good example of how a missing reference handle works look to
# https://github.com/sphinx-doc/sphinx/issues/1572#issuecomment-68590981
#
# Important attributes of the "node":
#
# example role: :ref:`title <target>`
#
# 'reftype' - role name (in the example above 'ref' is the reftype)
# 'reftarget' - target of the role, as given in the role content
# (in the example 'target' is the reftarget
# 'refexplicit' - the explicit title of the role
# (in the example 'title' is the refexplicit)
# 'refdoc' - document in which the role appeared
# 'refdomain' - domain of the role, in our case emtpy

if not hasattr(env, 'class_name_mapping'):
env.class_name_mapping = {}
mapping = env.class_name_mapping

reftype = node['reftype']
reftarget = node['reftarget']
refexplicit = node.get('refexplicit') # default: None
refdoc = node.get('refdoc', env.docname)
if reftype in ('obj', 'class', 'exc', 'meth'):
reftarget = node['reftarget']
suffix = ''
if reftarget not in mapping:
if '.' in reftarget:
front, suffix = reftarget.rsplit('.', 1)
else:
front = None
suffix = reftarget

if suffix.startswith('_') and not suffix.startswith('__'):
Expand All @@ -56,7 +89,7 @@ def missing_reference_handler(app, env, node, contnode):
# nitpick warning.
return node[0].deepcopy()

if reftype in ('obj', 'meth') and '.' in reftarget:
if reftype in ('obj', 'meth') and front is not None:
if front in mapping:
reftarget = front
suffix = '.' + suffix
Expand All @@ -73,10 +106,10 @@ def missing_reference_handler(app, env, node, contnode):
if (reftarget not in mapping and
prefix in inventory):

if 'py:class' in inventory[prefix] and reftarget in inventory[prefix]['py:class']:
if 'py:class' in inventory[prefix] and \
reftarget in inventory[prefix]['py:class']:
newtarget = inventory[prefix]['py:class'][reftarget][2]
if not node['refexplicit'] and \
'~' not in node.rawsource:
if not refexplicit and '~' not in node.rawsource:
contnode = literal(text=reftarget)
newnode = reference('', '', internal=True)
newnode['reftitle'] = reftarget
Expand All @@ -87,11 +120,10 @@ def missing_reference_handler(app, env, node, contnode):

if reftarget in mapping:
newtarget = mapping[reftarget] + suffix
if not node['refexplicit'] and '~' not in node.rawsource:
if not refexplicit and '~' not in node.rawsource:
contnode = literal(text=newtarget)
newnode = env.domains['py'].resolve_xref(
env, node['refdoc'], app.builder, 'class', newtarget,
node, contnode)
newnode = env.domains['py'].resolve_xref(env, refdoc, app.builder, 'class',
newtarget, node, contnode)
if newnode is not None:
newnode['reftitle'] = reftarget
return newnode
Expand Down