Skip to content

Commit ef3ae9f

Browse files
Update type annotations
1 parent 353d7be commit ef3ae9f

17 files changed

Lines changed: 82 additions & 92 deletions

File tree

neoteroi/mkdocs/cards/domain.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
from dataclasses import dataclass
2-
from typing import List, Optional
32

43
from neoteroi.mkdocs.markdown.images import Image
54

65

76
@dataclass
87
class CardItem:
98
title: str
10-
url: Optional[str] = None
11-
content: Optional[str] = None
12-
icon: Optional[str] = None
13-
key: Optional[str] = None
14-
image: Optional[Image] = None
9+
url: str | None = None
10+
content: str | None = None
11+
icon: str | None = None
12+
key: str | None = None
13+
image: Image | None = None
1514

1615
def __post_init__(self):
1716
if self.image and not self.image.alt:
@@ -20,4 +19,4 @@ def __post_init__(self):
2019

2120
@dataclass
2221
class Cards:
23-
items: List[CardItem]
22+
items: list[CardItem]

neoteroi/mkdocs/contribs/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from fnmatch import fnmatch
1414
from pathlib import Path
1515
from subprocess import CalledProcessError
16-
from typing import List
1716

1817
from mkdocs.config import config_options as c
1918
from mkdocs.plugins import BasePlugin
@@ -38,7 +37,7 @@ def __init__(self) -> None:
3837
self._git_reader = GitContributionsReader()
3938
self._txt_reader = TXTContributionsReader()
4039

41-
def get_contributors(self, file_path: Path) -> List[Contributor]:
40+
def get_contributors(self, file_path: Path) -> list[Contributor]:
4241
git_history_contributors = self._git_reader.get_contributors(file_path)
4342
configured_contributors = self._txt_reader.get_contributors(file_path)
4443
return list(
@@ -71,7 +70,7 @@ def __init__(self) -> None:
7170

7271
def _merge_contributor_by_email(
7372
self,
74-
contributors: List[Contributor],
73+
contributors: list[Contributor],
7574
contributor: Contributor,
7675
contributor_info: dict,
7776
) -> bool:
@@ -97,7 +96,7 @@ def _merge_contributor_by_email(
9796

9897
return False
9998

100-
def _get_contributors(self, page_file: File) -> List[Contributor]:
99+
def _get_contributors(self, page_file: File) -> list[Contributor]:
101100
results = []
102101
contributors = self._contribs_reader.get_contributors(
103102
Path("docs") / page_file.src_path

neoteroi/mkdocs/contribs/domain.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
from dataclasses import dataclass
33
from datetime import datetime
44
from pathlib import Path
5-
from typing import List, Optional
65

76

87
@dataclass
98
class Contributor:
109
name: str
1110
email: str
1211
count: int = -1
13-
image: Optional[str] = None
14-
key: Optional[str] = None
12+
image: str | None = None
13+
key: str | None = None
1514

1615

1716
class ContributionsReader(ABC):
1817
@abstractmethod
19-
def get_contributors(self, file_path: Path) -> List[Contributor]:
18+
def get_contributors(self, file_path: Path) -> list[Contributor]:
2019
"""Obtains the list of contributors for a file with the given path."""
2120

2221
@abstractmethod

neoteroi/mkdocs/contribs/git.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import subprocess
1010
from datetime import datetime
1111
from pathlib import Path
12-
from typing import Iterable, List, Tuple
12+
from typing import Iterable
1313

1414
from dateutil.parser import ParserError
1515
from dateutil.parser import parse as parse_date
@@ -26,7 +26,7 @@ def _decode(self, value: bytes) -> str:
2626
except UnicodeDecodeError:
2727
return value.decode("ISO-8859-1")
2828

29-
def _parse_name_and_email(self, name_and_email) -> Tuple[str, str]:
29+
def _parse_name_and_email(self, name_and_email) -> tuple[str, str]:
3030
match = self._name_email_rx.search(name_and_email)
3131
if match:
3232
name = match.groupdict()["name"].strip()
@@ -41,7 +41,7 @@ def parse_committers(self, output: str) -> Iterable[Contributor]:
4141
name, email = self._parse_name_and_email(name_and_email)
4242
yield Contributor(name, email, int(count))
4343

44-
def get_contributors(self, file_path: Path) -> List[Contributor]:
44+
def get_contributors(self, file_path: Path) -> list[Contributor]:
4545
"""
4646
Obtains the list of contributors for a file with the given path,
4747
using the Git CLI.

neoteroi/mkdocs/contribs/html.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import xml.etree.ElementTree as etree
66
from dataclasses import dataclass
77
from datetime import datetime
8-
from typing import List
98
from xml.etree.ElementTree import tostring as xml_to_str
109

1110
from neoteroi.mkdocs.contribs.domain import Contributor
@@ -25,7 +24,7 @@ class ContribsViewOptions:
2524

2625

2726
def contribution_stats_to_element(
28-
contributors: List[Contributor],
27+
contributors: list[Contributor],
2928
last_commit_date: datetime,
3029
options: ContribsViewOptions,
3130
) -> etree.Element:
@@ -85,7 +84,7 @@ def contribution_stats_to_element(
8584

8685

8786
def render_contribution_stats(
88-
contributors: List[Contributor],
87+
contributors: list[Contributor],
8988
last_commit_date: datetime,
9089
options: ContribsViewOptions,
9190
) -> str:

neoteroi/mkdocs/contribs/txt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import re
33
from datetime import datetime
44
from pathlib import Path
5-
from typing import Iterable, List, Tuple
5+
from typing import Iterable
66

77
from dateutil.parser import parse
88

@@ -30,7 +30,7 @@ class TXTContributionsReader(ContributionsReader):
3030
r"^\s*Last\smodified\stime:\s(?P<value>.+)$", re.IGNORECASE | re.MULTILINE
3131
)
3232

33-
def _parse_value(self, value: str) -> Tuple[str, str, int]:
33+
def _parse_value(self, value: str) -> tuple[str, str, int]:
3434
match = self._contrib_rx.search(value)
3535
if match:
3636
values = match.groupdict()
@@ -51,7 +51,7 @@ def _get_contributors_from_txt_file(self, file_path: Path) -> Iterable[Contribut
5151
if name and email:
5252
yield Contributor(name, email, count)
5353

54-
def get_contributors(self, file_path: Path) -> List[Contributor]:
54+
def get_contributors(self, file_path: Path) -> list[Contributor]:
5555
"""
5656
Obtains the list of contributors from a txt file with the given path.
5757
The file contents should look like:

neoteroi/mkdocs/markdown/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
import re
7-
from typing import Dict, Tuple
87

98
_PROPS_RE = re.compile(
109
r"""\s?((?P<name>[^\s\=]+)=(?P<quot>"|')(?P<value>[^\"\']+)(?P=quot))""",
@@ -14,7 +13,7 @@
1413

1514
def parse_props(
1615
line: str, prefix: str = "", bool_attrs: bool = False
17-
) -> Dict[str, str]:
16+
) -> dict[str, str]:
1817
"""
1918
Parses a line describing properties, in this form:
2019
@@ -49,7 +48,7 @@ def parse_props(
4948
return props
5049

5150

52-
def extract_props(line: str, prefix: str = "") -> Tuple[str, Dict[str, str]]:
51+
def extract_props(line: str, prefix: str = "") -> tuple[str, dict[str, str]]:
5352
"""
5453
Extracts properties like the `parse_props` function, but
5554
also returns the original line with properties removed.

neoteroi/mkdocs/markdown/images.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import xml.etree.ElementTree as etree
22
from dataclasses import dataclass
3-
from typing import Optional
43

54
from .utils import create_instance
65

76

87
@dataclass
98
class Image:
109
url: str
11-
height: Optional[int] = None
12-
width: Optional[int] = None
13-
alt: Optional[str] = None
10+
height: int | None = None
11+
width: int | None = None
12+
alt: str | None = None
1413

1514
@classmethod
1615
def from_obj(cls, obj):

neoteroi/mkdocs/markdown/processors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import textwrap
1717
import xml.etree.ElementTree as etree
1818
from abc import ABC, abstractmethod
19-
from typing import Iterable, List, Optional
19+
from typing import Iterable
2020

2121
from markdown.blockprocessors import BlockProcessor
2222

@@ -34,7 +34,7 @@
3434
logger = logging.getLogger("MARKDOWN")
3535

3636

37-
def find_closing_fragment_index(pattern: re.Pattern, blocks: List[str]) -> int:
37+
def find_closing_fragment_index(pattern: re.Pattern, blocks: list[str]) -> int:
3838
for index, block in enumerate(blocks):
3939
if pattern.search(block):
4040
return index
@@ -128,7 +128,7 @@ def render(self, parent, data, props):
128128
f"Could not render a {self.name} block. Please correct the input.",
129129
)
130130

131-
def get_match(self, pattern, blocks) -> Optional[re.Match]:
131+
def get_match(self, pattern, blocks) -> re.Match | None:
132132
first_block = blocks.pop(0)
133133
new_lines = []
134134
match = None
@@ -159,7 +159,7 @@ class SourceBlockProcessor(BlockProcessor, BaseProcessor):
159159
[timeline(https://.../example.json)]
160160
"""
161161

162-
_pattern: Optional[re.Pattern] = None
162+
_pattern: re.Pattern | None = None
163163

164164
@property
165165
def pattern(self) -> re.Pattern:
@@ -233,8 +233,8 @@ class EmbeddedBlockProcessor(BlockProcessor, BaseProcessor):
233233
The source of data in this case is always coming as a string.
234234
"""
235235

236-
_start_pattern: Optional[re.Pattern] = None
237-
_end_pattern: Optional[re.Pattern] = None
236+
_start_pattern: re.Pattern | None = None
237+
_end_pattern: re.Pattern | None = None
238238

239239
@property
240240
def start_pattern(self) -> re.Pattern:

neoteroi/mkdocs/markdown/tables/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, TypeVar
2+
from typing import Any, Iterable, Type, TypeVar
33

44
_TABLE_LINE_PATTERN = re.compile(r"\s?\|([^\|]+)")
55
_TABLE_SEPARATOR_LINE_PATTERN = re.compile(r"^[-:\s\|]*$")
@@ -20,7 +20,7 @@ def __init__(
2020
)
2121

2222
@property
23-
def rows(self) -> Tuple[List[Any], ...]:
23+
def rows(self) -> tuple[list[Any], ...]:
2424
return self._rows
2525

2626
def __str__(self) -> str:
@@ -71,14 +71,14 @@ def __repr__(self) -> str:
7171
)
7272

7373
@property
74-
def headers(self) -> Tuple[str, ...]:
74+
def headers(self) -> tuple[str, ...]:
7575
return self._headers
7676

7777
@property
78-
def records(self) -> Tuple[Tuple[str, ...], ...]:
78+
def records(self) -> tuple[tuple[str, ...], ...]:
7979
return self._records
8080

81-
def items(self) -> Iterable[Dict[str, str]]:
81+
def items(self) -> Iterable[dict[str, str]]:
8282
properties = {index: header for index, header in enumerate(self.headers)}
8383

8484
for record in self.records:
@@ -110,9 +110,9 @@ def __getitem__(self, key):
110110
T = TypeVar("T", bound=Table)
111111

112112

113-
def read_table(markdown: str, cls: Type[T] = Table) -> Optional[T]:
114-
headers: List[str] = []
115-
records: List[List[str]] = []
113+
def read_table(markdown: str, cls: Type[T] = Table) -> T | None:
114+
headers: list[str] = []
115+
records: list[list[str]] = []
116116

117117
for line in markdown.splitlines():
118118
if _TABLE_SEPARATOR_LINE_PATTERN.match(line):

0 commit comments

Comments
 (0)