Sphinx: PythonDomain.resolve_xref incorrectly matches ivars to class methods

Created on 16 May 2016  路  13Comments  路  Source: sphinx-doc/sphinx

This issue was originally described in a stackoverflow post and has gone unanswered for a while. If an :ivar: has the same name as a method in a different class, a cross reference is incorrectly created. Furthermore, if two classes have methods of the same name as the :ivar:, a warning is raised saying:
WARNING: more than one target found for cross-reference ...

bug py

Most helpful comment

This randomly fixed the issue on my project. Not sure why.

:ivar ~.signature: The signature of this signature tree
:vartype ~.signature: str

All 13 comments

I'm having the exact same problem, is there a known workaround for that? I'm trying to build pytest's documentation with -W, and this is the only warning that I couldn't get rid of...

I still haven't found a workaround.

I had to change from ivars in the dosctring to inline comments as can be seen here, in case others find this useful.

Bump, also having this issue.

I'm getting this same error with :param:

me too -- seeing the issue also with :param list foo:

Still no good workaround for this 馃槙

Also seeing this with :type foo: list

It is perhaps not a "good" workaround, but this monkey patch works: https://stackoverflow.com/a/41184353.

I'm seeing this with :attr:, where using

:attr:`~.current`

within docstrings finds current attributes/properties of other unrelated classes, too, and warns about them. Sometimes even the wrong one is preferred and thus linked in the output.

:ivar: has the same name as a method in a different class

__slots__ seems to play into this as well:

foo.py:

class Foo:
    """
    :ivar id: thing
    """

    __slots__ = ['id']

bar.py:

class Bar:
    """
    :ivar id: thing
    """

    __slots__ = ['id']

log:

bar.py:docstring of bar.Bar:: WARNING: more than one target found for cross-reference 'id': bar.Bar.id, foo.Foo.id

Without __slots__ this doesn't happen. With __slots__, an id field appears at the bottom of the class documentation that wasn't there before, and if the classes are in different files, the ivar representation links to this field. However, if the classes are in the same file, then the second class's ivar representation will link to the first class's id field.

This randomly fixed the issue on my project. Not sure why.

:ivar ~.signature: The signature of this signature tree
:vartype ~.signature: str

This randomly fixed the issue on my project. Not sure why.

:ivar ~.signature: The signature of this signature tree
:vartype ~.signature: str

With Sphinx 3.3, this fixed most of the duplicate crossref warnings for me, but even this wasn't enough in some cases. I had to also add the class name prefix:


# pkg.nn.tf_impl.py

class MyRNN(tf.keras.AbstractRNNCell):
    """
    :ivar tf.keras.layers.Dense ~MyRNN.layer1: :math:`l_1` layer.
    :ivar tf.keras.layers.Dense ~MyRNN.layer2: :math:`l_2` layer.
    """

# pkg.nn.torch_impl.py

class MyRNN(nn.Module):
    """
    :ivar torch.nn.Linear ~MyRNN.layer1: :math:`l_1` layer.
    :ivar torch.nn.Linear ~MyRNN.layer2: :math:`l_2` layer.
    :ivar torch.nn.Linear layer3: :math:`l_3` layer.
    """

I suppose this is because the classes have the same name, but are located in different modules.

Was this page helpful?
0 / 5 - 0 ratings