From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 6/9] gdb: gdbarch*.py, copyright.py: add type annotations
Date: Thu, 23 Feb 2023 17:18:27 -0500 [thread overview]
Message-ID: <20230223221830.499934-7-simon.marchi@efficios.com> (raw)
In-Reply-To: <20230223221830.499934-1-simon.marchi@efficios.com>
Add type annotations to gdbarch*.py to fix all errors shown by pyright.
There is one change in copyright.py too, to fix this one:
/home/simark/src/binutils-gdb/gdb/gdbarch.py
/home/simark/src/binutils-gdb/gdb/gdbarch.py:206:13 - error: Type of "copyright" is partially unknown
Type of "copyright" is "(tool: Unknown, description: Unknown) -> str" (reportUnknownMemberType)
Change-Id: Ia109b53e267f6e2f5bd79a1288d0d5c9508c9ac4
---
gdb/gdbarch.py | 8 ++--
gdb/gdbarch_components.py | 6 +--
gdb/gdbarch_types.py | 87 ++++++++++++++++++++-------------------
gdb/gdbcopyright.py | 2 +-
4 files changed, 52 insertions(+), 51 deletions(-)
diff --git a/gdb/gdbarch.py b/gdb/gdbarch.py
index d1ac414d0eb3..93b1e8bf84e2 100755
--- a/gdb/gdbarch.py
+++ b/gdb/gdbarch.py
@@ -25,10 +25,10 @@ import textwrap
# `gdbarch_types.components`.
import gdbarch_components # noqa: F401 # type: ignore
import gdbcopyright
-from gdbarch_types import Function, Info, Value, components
+from gdbarch_types import Component, Function, Info, Value, components
-def indentation(n_columns):
+def indentation(n_columns: int):
"""Return string with tabs and spaces to indent line to N_COLUMNS."""
return "\t" * (n_columns // 8) + " " * (n_columns % 8)
@@ -38,12 +38,12 @@ copyright = gdbcopyright.copyright(
)
-def info(c):
+def info(c: Component):
"Filter function to only allow Info components."
return type(c) is Info
-def not_info(c):
+def not_info(c: Component):
"Filter function to omit Info components."
return type(c) is not Info
diff --git a/gdb/gdbarch_components.py b/gdb/gdbarch_components.py
index fe5c3b3b4bcd..c956586e82c0 100644
--- a/gdb/gdbarch_components.py
+++ b/gdb/gdbarch_components.py
@@ -165,14 +165,14 @@ Number of bits in an int or unsigned int for the target machine.
predefault="4*TARGET_CHAR_BIT",
invalid=False,
)
-
+long_bit_predefault = "4*TARGET_CHAR_BIT"
long_bit = Value(
comment="""
Number of bits in a long or unsigned long for the target machine.
""",
type="int",
name="long_bit",
- predefault="4*TARGET_CHAR_BIT",
+ predefault=long_bit_predefault,
invalid=False,
)
@@ -183,7 +183,7 @@ machine.
""",
type="int",
name="long_long_bit",
- predefault="2*" + long_bit.predefault,
+ predefault="2*" + long_bit_predefault,
invalid=False,
)
diff --git a/gdb/gdbarch_types.py b/gdb/gdbarch_types.py
index 95d9b5e65bc5..6b1483c77331 100755
--- a/gdb/gdbarch_types.py
+++ b/gdb/gdbarch_types.py
@@ -17,8 +17,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+from typing import Optional, Union
-def join_type_and_name(t, n):
+
+def join_type_and_name(t: str, n: str):
"Combine the type T and the name N into a C declaration."
if t.endswith("*") or t.endswith("&"):
return t + n
@@ -26,30 +28,29 @@ def join_type_and_name(t, n):
return t + " " + n
-def join_params(params):
+def join_params(params: list[tuple[str, str]]):
"""Given a sequence of (TYPE, NAME) pairs, generate a comma-separated
list of declarations."""
- params = [join_type_and_name(p[0], p[1]) for p in params]
- return ", ".join(params)
+ return ", ".join([join_type_and_name(p[0], p[1]) for p in params])
-class _Component:
+class Component:
"Base class for all components."
def __init__(
self,
- name,
- type,
- printer,
- comment=None,
- predicate=False,
- predefault=None,
- postdefault=None,
- invalid=None,
- params=None,
- param_checks=None,
- result_checks=None,
- implement=True,
+ name: str,
+ type: str,
+ printer: Optional[str],
+ comment: Optional[str] = None,
+ predicate: bool = False,
+ predefault: Optional[str] = None,
+ postdefault: Optional[str] = None,
+ invalid: Optional[Union[bool, str]] = None,
+ params: Optional[list[tuple[str, str]]] = None,
+ param_checks: Optional[list[str]] = None,
+ result_checks: Optional[list[str]] = None,
+ implement: bool = True,
):
self.name = name
self.type = type
@@ -59,7 +60,7 @@ class _Component:
self.predefault = predefault
self.postdefault = postdefault
self.invalid = invalid
- self.params = params
+ self.params = params or []
self.param_checks = param_checks
self.result_checks = result_checks
self.implement = implement
@@ -81,29 +82,29 @@ class _Component:
return predicate
-class Info(_Component):
+class Info(Component):
"An Info component is copied from the gdbarch_info."
- def __init__(self, *, name, type, printer=None):
+ def __init__(self, *, name: str, type: str, printer: Optional[str] = None):
super().__init__(name=name, type=type, printer=printer)
# This little hack makes the generator a bit simpler.
self.predicate = None
-class Value(_Component):
+class Value(Component):
"A Value component is just a data member."
def __init__(
self,
*,
- name,
- type,
- comment=None,
- predicate=False,
- predefault=None,
- postdefault=None,
- invalid=None,
- printer=None,
+ name: str,
+ type: str,
+ comment: Optional[str] = None,
+ predicate: bool = False,
+ predefault: Optional[str] = None,
+ postdefault: Optional[str] = None,
+ invalid: Optional[Union[bool, str]] = None,
+ printer: Optional[str] = None,
):
super().__init__(
comment=comment,
@@ -117,24 +118,24 @@ class Value(_Component):
)
-class Function(_Component):
+class Function(Component):
"A Function component is a function pointer member."
def __init__(
self,
*,
- name,
- type,
- params,
- comment=None,
- predicate=False,
- predefault=None,
- postdefault=None,
- invalid=None,
- printer=None,
- param_checks=None,
- result_checks=None,
- implement=True,
+ name: str,
+ type: str,
+ params: list[tuple[str, str]],
+ comment: Optional[str] = None,
+ predicate: bool = False,
+ predefault: Optional[str] = None,
+ postdefault: Optional[str] = None,
+ invalid: Optional[Union[bool, str]] = None,
+ printer: Optional[str] = None,
+ param_checks: Optional[list[str]] = None,
+ result_checks: Optional[list[str]] = None,
+ implement: bool = True,
):
super().__init__(
comment=comment,
@@ -185,4 +186,4 @@ class Method(Function):
# All the components created in gdbarch-components.py.
-components: list[_Component] = []
+components: list[Component] = []
diff --git a/gdb/gdbcopyright.py b/gdb/gdbcopyright.py
index 9a52bc1d847b..44c88e07cae7 100644
--- a/gdb/gdbcopyright.py
+++ b/gdb/gdbcopyright.py
@@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-def copyright(tool, description):
+def copyright(tool: str, description: str):
# Search the tool source itself for the correct copyright years.
with open(tool, "r") as f:
for line in f:
--
2.39.2
next prev parent reply other threads:[~2023-02-23 22:18 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-23 22:18 [PATCH 0/9] Add typing annotations to gdbarch*.py and make-target-delegates.py Simon Marchi
2023-02-23 22:18 ` [PATCH 1/9] gdb: remove invalid / dead code from gdbarch.py Simon Marchi
2023-02-23 22:18 ` [PATCH 2/9] gdb: reformat Python files with black 23.1.0 Simon Marchi
2023-02-23 22:18 ` [PATCH 3/9] gdb: gdbarch.py: spell out parameters of _Component.__init__ Simon Marchi
2023-02-24 19:51 ` Tom Tromey
2023-02-24 20:31 ` Simon Marchi
2023-02-24 22:07 ` Tom Tromey
2023-02-23 22:18 ` [PATCH 4/9] gdb: pyproject.toml: set pyright typeCheckingMode = "strict" Simon Marchi
2023-02-23 22:18 ` [PATCH 5/9] gdb: split gdbarch component types to gdbarch_types.py Simon Marchi
2023-02-23 22:18 ` Simon Marchi [this message]
2023-02-23 22:18 ` [PATCH 7/9] gdb: make-target-delegates.py: make one string raw Simon Marchi
2023-02-23 22:18 ` [PATCH 8/9] gdb: make-target-delegates.py: add Entry type Simon Marchi
2023-02-23 22:18 ` [PATCH 9/9] gdb: make-target-delegates.py: add type annotations Simon Marchi
2023-02-24 19:54 ` [PATCH 0/9] Add typing annotations to gdbarch*.py and make-target-delegates.py Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230223221830.499934-7-simon.marchi@efficios.com \
--to=simon.marchi@efficios.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).