Types used in PEP-604 union syntax can be linked with intersphinx

Closes #366
pull/394/head
Ashley Whetter 12 months ago
parent 7fa3998438
commit 38a615ff74

@ -432,6 +432,10 @@ def _resolve_annotation(annotation):
resolved = (
"[" + ", ".join(_resolve_annotation(elt) for elt in annotation.elts) + "]"
)
elif isinstance(annotation, astroid.BinOp) and annotation.op == "|":
left = _resolve_annotation(annotation.left)
right = _resolve_annotation(annotation.right)
resolved = f"{left} | {right}"
else:
resolved = annotation.as_string()

@ -0,0 +1 @@
Types used in PEP-604 union syntax can be linked with intersphinx

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
project = "pyexample"
copyright = "2015, readthedocs"
author = "readthedocs"
version = "0.1"
release = "0.1"
language = "en"
exclude_patterns = ["_build"]
pygments_style = "sphinx"
todo_include_todos = False
html_theme = "alabaster"
htmlhelp_basename = "pyexampledoc"
extensions = ["sphinx.ext.intersphinx", "sphinx.ext.autodoc", "autoapi.extension"]
intersphinx_mapping = {"python": ("https://docs.python.org/3.10", None)}
autoapi_type = "python"
autoapi_dirs = ["example"]
autoapi_file_pattern = "*.py"

@ -0,0 +1,18 @@
from pathlib import Path
from typing import Optional, Union
def simple(p: Path):
"""This is OK"""
def optional(p: Optional[Path]):
"""This is OK"""
def union(p: Union[Path, None]):
"""This is OK"""
def pipe(p: Path | None):
"""This is OK"""

@ -0,0 +1,26 @@
.. pyexample documentation master file, created by
sphinx-quickstart on Fri May 29 13:34:37 2015.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to pyexample's documentation!
=====================================
.. toctree::
autoapi/index
Contents:
.. toctree::
:maxdepth: 2
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

@ -586,6 +586,49 @@ class TestPositionalOnlyArgumentsModule:
assert "f_no_cd(a: int, b: int, /, *, e: float, f: float)" in f_no_cd.text
@pytest.mark.skipif(
sys.version_info < (3, 10), reason="Union pipe syntax requires Python >=3.10"
)
class TestPipeUnionModule:
@pytest.fixture(autouse=True, scope="class")
def built(self, builder):
builder("py310unionpipe", warningiserror=True)
def test_integration(self, parse):
example_file = parse("_build/html/autoapi/example/index.html")
simple = example_file.find(id="example.simple")
args = simple.find_all(class_="sig-param")
assert len(args) == 1
links = args[0].select("span > a")
assert len(links) == 1
assert links[0].text == "pathlib.Path"
optional = example_file.find(id="example.optional")
args = optional.find_all(class_="sig-param")
assert len(args) == 1
links = args[0].select("span > a")
assert len(links) == 2
assert links[0].text == "pathlib.Path"
assert links[1].text == "None"
union = example_file.find(id="example.union")
args = union.find_all(class_="sig-param")
assert len(args) == 1
links = args[0].select("span > a")
assert len(links) == 2
assert links[0].text == "pathlib.Path"
assert links[1].text == "None"
pipe = example_file.find(id="example.pipe")
args = pipe.find_all(class_="sig-param")
assert len(args) == 1
links = args[0].select("span > a")
assert len(links) == 2
assert links[0].text == "pathlib.Path"
assert links[1].text == "None"
def test_napoleon_integration_loaded(builder, parse):
confoverrides = {
"exclude_patterns": ["manualapi.rst"],

Loading…
Cancel
Save