aboutsummaryrefslogtreecommitdiffstats
path: root/tools/snippets_translate/main.py
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2024-06-06 10:59:31 +0200
committerAdrian Herrmann <adrian.herrmann@qt.io>2024-06-20 16:10:46 +0000
commitba2582125f6c9d470d3a5f4e1e61666de9101e0e (patch)
treecb04e0b4a90e02e0d6026309447fbcf08a8e00a0 /tools/snippets_translate/main.py
parent7bb9c0e2f81ec474bf98690b4f90f195a6ea27c8 (diff)
Use modern typing syntax
We can already use the modern typing syntax introduced with Python 3.10 in 3.9 via future statement definitions, even before we raise the minimum Python version to 3.10. Note that direct expressions with "|" don't work yet. Task-number: PYSIDE-2786 Change-Id: Ie36c140fc960328322502ea29cf6868805a7c558 Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'tools/snippets_translate/main.py')
-rw-r--r--tools/snippets_translate/main.py21
1 files changed, 10 insertions, 11 deletions
diff --git a/tools/snippets_translate/main.py b/tools/snippets_translate/main.py
index 488a1b1b2..faa07a0b0 100644
--- a/tools/snippets_translate/main.py
+++ b/tools/snippets_translate/main.py
@@ -10,7 +10,6 @@ from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
from enum import Enum
from pathlib import Path
from textwrap import dedent
-from typing import Dict, List
from override import python_example_snippet_mapping
from converter import snippet_translate
@@ -185,7 +184,7 @@ def is_valid_file(x):
return True
-def get_snippet_ids(line: str, pattern: re.Pattern) -> List[str]:
+def get_snippet_ids(line: str, pattern: re.Pattern) -> list[str]:
# Extract the snippet ids for a line '//! [1] //! [2]'
result = []
for m in pattern.finditer(line):
@@ -193,7 +192,7 @@ def get_snippet_ids(line: str, pattern: re.Pattern) -> List[str]:
return result
-def overriden_snippet_lines(lines: List[str], start_id: str) -> List[str]:
+def overriden_snippet_lines(lines: list[str], start_id: str) -> list[str]:
"""Wrap an overridden snippet with marker and id lines."""
id_string = f"//! [{start_id}]"
result = [OVERRIDDEN_SNIPPET, id_string]
@@ -202,7 +201,7 @@ def overriden_snippet_lines(lines: List[str], start_id: str) -> List[str]:
return result
-def get_snippet_override(start_id: str, rel_path: str) -> List[str]:
+def get_snippet_override(start_id: str, rel_path: str) -> list[str]:
"""Check if the snippet is overridden by a local file under
sources/pyside6/doc/snippets."""
file_start_id = start_id.replace(' ', '_')
@@ -214,14 +213,14 @@ def get_snippet_override(start_id: str, rel_path: str) -> List[str]:
return overriden_snippet_lines(lines, start_id)
-def _get_snippets(lines: List[str],
+def _get_snippets(lines: list[str],
comment: str,
- pattern: re.Pattern) -> Dict[str, List[str]]:
+ pattern: re.Pattern) -> dict[str, list[str]]:
"""Helper to extract (potentially overlapping) snippets from a C++ file
indicated by pattern ("//! [1]") and return them as a dict by <id>."""
- snippets: Dict[str, List[str]] = {}
- snippet: List[str]
- done_snippets : List[str] = []
+ snippets: dict[str, list[str]] = {}
+ snippet: list[str]
+ done_snippets: list[str] = []
i = 0
while i < len(lines):
@@ -260,7 +259,7 @@ def _get_snippets(lines: List[str],
return snippets
-def get_python_example_snippet_override(start_id: str, rel_path: str) -> List[str]:
+def get_python_example_snippet_override(start_id: str, rel_path: str) -> list[str]:
"""Check if the snippet is overridden by a python example snippet."""
key = (os.fspath(rel_path), start_id)
value = python_example_snippet_mapping().get(key)
@@ -276,7 +275,7 @@ def get_python_example_snippet_override(start_id: str, rel_path: str) -> List[st
return overriden_snippet_lines(lines, start_id)
-def get_snippets(lines: List[str], rel_path: str) -> List[List[str]]:
+def get_snippets(lines: list[str], rel_path: str) -> list[list[str]]:
"""Extract (potentially overlapping) snippets from a C++ file indicated
by '//! [1]'."""
result = _get_snippets(lines, '//', CPP_SNIPPET_PATTERN)