Skip to content

Commit

Permalink
MAINT: use future annotations where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Apr 5, 2024
1 parent 89db441 commit 7563d91
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
9 changes: 6 additions & 3 deletions src/sphinx_hep_pdgref/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Link to PDG reviews and listings in Sphinx documentation."""

from typing import Any, Dict
from __future__ import annotations

from sphinx.application import Sphinx
from typing import TYPE_CHECKING, Any

from .role import URLPattern, pdgref

if TYPE_CHECKING:
from sphinx.application import Sphinx

def setup(app: Sphinx) -> Dict[str, Any]:

def setup(app: Sphinx) -> dict[str, Any]:
app.add_role("pdg-listing", role=pdgref(pattern=URLPattern.LISTING))
app.add_role("pdg-review", role=pdgref(pattern=URLPattern.REVIEW))
return {
Expand Down
13 changes: 7 additions & 6 deletions src/sphinx_hep_pdgref/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
directly lead to the correct page number when clicking the generated link.
"""

from __future__ import annotations

import re
from typing import Optional

from attrs import field, frozen

Expand All @@ -26,8 +27,8 @@ class PDGEntry:

section: str
year: int
pages: Optional[str]
first_page: Optional[int] = field(init=False, repr=False)
pages: str | None
first_page: int | None = field(init=False, repr=False)

def __attrs_post_init__(self) -> None:
if self.pages is None:
Expand All @@ -37,7 +38,7 @@ def __attrs_post_init__(self) -> None:
object.__setattr__(self, "first_page", first_page)

@staticmethod
def from_str(text: str) -> "PDGEntry":
def from_str(text: str) -> PDGEntry:
"""Create an entry from the contents of the :code:`pdg` role.
>>> from sphinx_hep_pdgref.entry import PDGEntry
Expand Down Expand Up @@ -76,7 +77,7 @@ def get_first_page(text: str) -> int:
return int(first_page_nr)


def get_page_numbers(text: str) -> Optional[str]:
def get_page_numbers(text: str) -> str | None:
text = text.strip()
matches = re.match(r"pp?\.?\s*(\d+.*)", text)
if matches is None:
Expand All @@ -85,7 +86,7 @@ def get_page_numbers(text: str) -> Optional[str]:
return page_nr_def.replace(" ", "")


def get_year(text: str) -> Optional[int]:
def get_year(text: str) -> int | None:
text = text.strip()
matches = re.match(r"\d{4}", text)
if matches is None:
Expand Down
18 changes: 11 additions & 7 deletions src/sphinx_hep_pdgref/role.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
"""Link to PDG reviews and listings in Sphinx documentation."""

from typing import Dict, List, Optional, Tuple
from __future__ import annotations

from typing import TYPE_CHECKING

from docutils import nodes
from docutils.nodes import Node, system_message
from docutils.parsers.rst.states import Inliner
from sphinx.util.typing import RoleFunction

from .entry import PDGEntry
from .url import URLPattern, create_link_text, create_url

if TYPE_CHECKING:
from docutils.nodes import Node, system_message
from docutils.parsers.rst.states import Inliner
from sphinx.util.typing import RoleFunction


def pdgref(pattern: URLPattern) -> RoleFunction:
def role( # noqa: PLR0913
Expand All @@ -18,9 +22,9 @@ def role( # noqa: PLR0913
text: str,
lineno: int,
inliner: Inliner,
options: Optional[Dict] = None,
content: Optional[List[str]] = None,
) -> Tuple[List[Node], List[system_message]]:
options: dict | None = None,
content: list[str] | None = None,
) -> tuple[list[Node], list[system_message]]:
try:
pdg_entry = PDGEntry.from_str(text)
link_text = create_link_text(pdg_entry, pattern=pattern)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_entry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from __future__ import annotations

import pytest

Expand Down Expand Up @@ -35,8 +35,8 @@ class TestPDGEntry:
def test_from_str(
self,
text: str,
expected: Optional[PDGEntry],
first_page: Optional[int],
expected: PDGEntry | None,
first_page: int | None,
):
if expected is None:
with pytest.raises(
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_from_str(
],
)
def test_get_page_numbers(
text: str, formatted_pages: Optional[str], first_page: Optional[int]
text: str, formatted_pages: str | None, first_page: int | None
):
assert get_page_numbers(text) == formatted_pages
if first_page is None:
Expand All @@ -93,5 +93,5 @@ def test_get_page_numbers(
("p. 12-15", None),
],
)
def test_get_year(text: str, expected: Optional[int]):
def test_get_year(text: str, expected: int | None):
assert get_year(text) == expected

0 comments on commit 7563d91

Please sign in to comment.