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

Sphinx: WARNING: Cannot resolve forward reference in type annotations of ... #155

Closed
markheik opened this issue Jun 20, 2022 · 3 comments
Closed
Labels
bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request

Comments

@markheik
Copy link
Collaborator

markheik commented Jun 20, 2022

Description

Sphinx build complains about the missing references.

Example WARNING when building docs:
WARNING: Cannot resolve forward reference in type annotations of "zhinst.toolkit.driver.devices.shfqa.QAChannel": name 'Session' is not defined

This is probably due to type hinting.

if t.TYPE_HINTING:
    from zhinst.toolkit.session import Session

and using it in as type hint in, for example BaseInstrument:

def __init__(
      self,
      serial: str,
      device_type: str,
      session: "Session", <----
):

This could be probably be solved importing the whole zhinst.toolkit.session module

https://peps.python.org/pep-0484/

https://github.com/tox-dev/sphinx-autodoc-typehints#dealing-with-circular-imports

So it would result to:

from __future__ import annotations
import zhinst.toolkit.session

and using it in as type hint in, for example BaseInstrument:

def __init__(
      self,
      serial: str,
      device_type: str,
      session: zhinst.toolkit.session.Session
):

How to reproduce

Environment info

  • Python version:
  • zhinst-toolkit version:
  • LabOne version:
  • zhinst version:

Device/system information (if applicable)

Additional information

@markheik markheik added bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request labels Jun 20, 2022
@markheik markheik changed the title Sphinx: WARNING: Cannot resolve forward reference in type annotations of "zhinst.toolkit.driver.devices.shfqa.QAChannel": name 'Session' is not defined Sphinx: WARNING: Cannot resolve forward reference in type annotations of ... Jun 20, 2022
@jenshnielsen
Copy link
Collaborator

We gave up on using Sphinx-autodoc-typehints in QCoDeS for this exact reason

There is a long standing bug about this here
tox-dev/sphinx-autodoc-typehints#186

@markheik
Copy link
Collaborator Author

We gave up on using Sphinx-autodoc-typehints in QCoDeS for this exact reason

There is a long standing bug about this here tox-dev/sphinx-autodoc-typehints#186

@jenshnielsen Thanks for info, I did read the thread. I agree with you and will evaluate the changes required for this.

@markheik
Copy link
Collaborator Author

@jenshnielsen I did end to remove sphinx-autodoc-typehints and replace it with

autodoc_typehints = "both"
autodoc_typehints_format = "short"

I like autodoc_typehints="both" as it shows the type both in signature and description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants