Skip to content
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

More than one target found for cross-reference with quotes around type annotation #10400

Open
martinburchell opened this issue Apr 29, 2022 · 3 comments

Comments

@martinburchell
Copy link

Describe the bug

If you have two classes with the same name and have quotes around a type annotation when referencing one of them, you get the warning more than one target found for cross-reference

If you import the class normally, it works fine. In the real world case I need to use quotes to avoid circular dependencies.

#!/usr/bin/env python

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from crate_anon.anonymise.patient import Patient


class AlterMethod:
    """
    AlterMethod class
    """

    def alter(
        self,
        patient: "Patient" = None,
    ) -> None:
        """
        alter method
        """

How to Reproduce

$ git clone https://github.com/martinburchell/sphinx-test.git
$ cd sphinx-test
$ python3 -m venv ~/.virtualenvs/sphinx-test
$ source ~/.virtualenvs/sphinx-test/bin/activate
$ pip install -e .
$ cd docs
$ make html SPHINXOPTS="-W"

You get the error:

sphinx-test/crate_anon/anonymise/altermethod.py:docstring of crate_anon.anonymise.altermethod.AlterMethod.alter::more than one target found for cross-reference 'Patient': crate_anon.anonymise.make_demo_database.Patient, crate_anon.anonymise.patient.Patient

Expected behavior

Sphinx should be able to resolve the cross reference and not produce a warning.

Your project

https://github.com/martinburchell/sphinx-test (minimal test case based on https://github.com/RudolfCardinal/crate)

Screenshots

No response

OS

Linux

Python version

3.8.10

Sphinx version

4.4.0, 4.5.0, latest 5.x from GitHub

Sphinx extensions

sphinx.ext.autodoc

Extra tools

No response

Additional context

Works in 4.2.0, 4.3.0, 4.3.1, 4.3.2

Broken since:

Fix #9899: py domain: Allows cross-reference specifier to :type: option
commit acaf705

@AA-Turner AA-Turner added this to the some future version milestone Sep 29, 2022
graingert added a commit to graingert/distributed that referenced this issue Mar 22, 2023
…e' and 'WorkerState'

distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.ActiveMemoryManagerExtension.pending:: WARNING: more than one target found for cross-reference 'TaskState': distributed.worker_state_machine.TaskState, distributed.scheduler.TaskState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.ActiveMemoryManagerExtension.pending:: WARNING: more than one target found for cross-reference 'WorkerState': distributed.worker_state_machine.WorkerState, distributed.scheduler.WorkerState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.ActiveMemoryManagerExtension.pending:: WARNING: more than one target found for cross-reference 'WorkerState': distributed.worker_state_machine.WorkerState, distributed.scheduler.WorkerState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.ActiveMemoryManagerExtension.workers_memory:: WARNING: more than one target found for cross-reference 'WorkerState': distributed.worker_state_machine.WorkerState, distributed.scheduler.WorkerState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.Suggestion.candidates:: WARNING: more than one target found for cross-reference 'WorkerState': distributed.worker_state_machine.WorkerState, distributed.scheduler.WorkerState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.Suggestion.ts:: WARNING: more than one target found for cross-reference 'TaskState': distributed.worker_state_machine.TaskState, distributed.scheduler.TaskState

workaround for sphinx-doc/sphinx#10400
graingert added a commit to graingert/distributed that referenced this issue Mar 22, 2023
…e' and 'WorkerState'

distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.ActiveMemoryManagerExtension.pending:: WARNING: more than one target found for cross-reference 'TaskState': distributed.worker_state_machine.TaskState, distributed.scheduler.TaskState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.ActiveMemoryManagerExtension.pending:: WARNING: more than one target found for cross-reference 'WorkerState': distributed.worker_state_machine.WorkerState, distributed.scheduler.WorkerState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.ActiveMemoryManagerExtension.pending:: WARNING: more than one target found for cross-reference 'WorkerState': distributed.worker_state_machine.WorkerState, distributed.scheduler.WorkerState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.ActiveMemoryManagerExtension.workers_memory:: WARNING: more than one target found for cross-reference 'WorkerState': distributed.worker_state_machine.WorkerState, distributed.scheduler.WorkerState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.Suggestion.candidates:: WARNING: more than one target found for cross-reference 'WorkerState': distributed.worker_state_machine.WorkerState, distributed.scheduler.WorkerState
distributed/active_memory_manager.py:docstring of distributed.active_memory_manager.Suggestion.ts:: WARNING: more than one target found for cross-reference 'TaskState': distributed.worker_state_machine.TaskState, distributed.scheduler.TaskState

workaround for sphinx-doc/sphinx#10400
@graingert
Copy link
Contributor

I don't think this is fixable very easily because sphinx imports the concrete type and uses typing.get_type_hints to resolve the annotation, anything in "if TYPE_CHECKING:` will become a ForwardRef

@graingert
Copy link
Contributor

graingert commented Mar 22, 2023

I think the only workaround here is as documented in https://github.com/agronholm/sphinx-autodoc-typehints#dealing-with-circular-imports

#!/usr/bin/env python
from __future__ import annotations

from crate_anon.anonymise import patient
class AlterMethod:
    """
    AlterMethod class
    """

    def alter(
        self,
        patient: patient.Patient = None,
    ) -> None:
        """
        alter method
        """

@martinburchell
Copy link
Author

martinburchell commented Feb 22, 2024

@graingert Apologies for the delay in replying. I can confirm that your workaround works. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants