gdb: gdbarch*.py, copyright.py: add type annotations

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
Reviewed-By: Tom Tromey <tom@tromey.com>
Reviewed-By: Andrew Burgess <aburgess@redhat.com>
This commit is contained in:
Simon Marchi 2023-02-26 20:14:00 -05:00 committed by Simon Marchi
parent 05e4e89373
commit 116e3492f2
4 changed files with 51 additions and 49 deletions

View file

@ -25,10 +25,10 @@ import textwrap
# `gdbarch_types.components`. # `gdbarch_types.components`.
import gdbarch_components # noqa: F401 # type: ignore import gdbarch_components # noqa: F401 # type: ignore
import gdbcopyright 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 string with tabs and spaces to indent line to N_COLUMNS."""
return "\t" * (n_columns // 8) + " " * (n_columns % 8) 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." "Filter function to only allow Info components."
return type(c) is Info return type(c) is Info
def not_info(c): def not_info(c: Component):
"Filter function to omit Info components." "Filter function to omit Info components."
return type(c) is not Info return type(c) is not Info

View file

@ -166,13 +166,14 @@ Number of bits in an int or unsigned int for the target machine.
invalid=False, invalid=False,
) )
long_bit_predefault = "4*TARGET_CHAR_BIT"
long_bit = Value( long_bit = Value(
comment=""" comment="""
Number of bits in a long or unsigned long for the target machine. Number of bits in a long or unsigned long for the target machine.
""", """,
type="int", type="int",
name="long_bit", name="long_bit",
predefault="4*TARGET_CHAR_BIT", predefault=long_bit_predefault,
invalid=False, invalid=False,
) )
@ -183,7 +184,7 @@ machine.
""", """,
type="int", type="int",
name="long_long_bit", name="long_long_bit",
predefault="2*" + long_bit.predefault, predefault="2*" + long_bit_predefault,
invalid=False, invalid=False,
) )

View file

@ -17,8 +17,10 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import List, Optional, Tuple, 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." "Combine the type T and the name N into a C declaration."
if t.endswith("*") or t.endswith("&"): if t.endswith("*") or t.endswith("&"):
return t + n return t + n
@ -26,30 +28,29 @@ def join_type_and_name(t, n):
return 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 """Given a sequence of (TYPE, NAME) pairs, generate a comma-separated
list of declarations.""" list of declarations."""
params = [join_type_and_name(p[0], p[1]) for p in params] return ", ".join([join_type_and_name(p[0], p[1]) for p in params])
return ", ".join(params)
class _Component: class Component:
"Base class for all components." "Base class for all components."
def __init__( def __init__(
self, self,
name, name: str,
type, type: str,
printer=None, printer: Optional[str] = None,
comment=None, comment: Optional[str] = None,
predicate=False, predicate: bool = False,
predefault=None, predefault: Optional[str] = None,
postdefault=None, postdefault: Optional[str] = None,
invalid=None, invalid: Optional[Union[bool, str]] = None,
params=None, params: Optional[List[Tuple[str, str]]] = None,
param_checks=None, param_checks: Optional[List[str]] = None,
result_checks=None, result_checks: Optional[List[str]] = None,
implement=True, implement: bool = True,
): ):
self.name = name self.name = name
self.type = type self.type = type
@ -59,7 +60,7 @@ class _Component:
self.predefault = predefault self.predefault = predefault
self.postdefault = postdefault self.postdefault = postdefault
self.invalid = invalid self.invalid = invalid
self.params = params self.params = params or []
self.param_checks = param_checks self.param_checks = param_checks
self.result_checks = result_checks self.result_checks = result_checks
self.implement = implement self.implement = implement
@ -81,24 +82,24 @@ class _Component:
return predicate return predicate
class Info(_Component): class Info(Component):
"An Info component is copied from the gdbarch_info." "An Info component is copied from the gdbarch_info."
class Value(_Component): class Value(Component):
"A Value component is just a data member." "A Value component is just a data member."
def __init__( def __init__(
self, self,
*, *,
name, name: str,
type, type: str,
comment=None, comment: Optional[str] = None,
predicate=False, predicate: bool = False,
predefault=None, predefault: Optional[str] = None,
postdefault=None, postdefault: Optional[str] = None,
invalid=None, invalid: Optional[Union[bool, str]] = None,
printer=None, printer: Optional[str] = None,
): ):
super().__init__( super().__init__(
comment=comment, comment=comment,
@ -112,24 +113,24 @@ class Value(_Component):
) )
class Function(_Component): class Function(Component):
"A Function component is a function pointer member." "A Function component is a function pointer member."
def __init__( def __init__(
self, self,
*, *,
name, name: str,
type, type: str,
params, params: List[Tuple[str, str]],
comment=None, comment: Optional[str] = None,
predicate=False, predicate: bool = False,
predefault=None, predefault: Optional[str] = None,
postdefault=None, postdefault: Optional[str] = None,
invalid=None, invalid: Optional[Union[bool, str]] = None,
printer=None, printer: Optional[str] = None,
param_checks=None, param_checks: Optional[List[str]] = None,
result_checks=None, result_checks: Optional[List[str]] = None,
implement=True, implement: bool = True,
): ):
super().__init__( super().__init__(
comment=comment, comment=comment,
@ -180,4 +181,4 @@ class Method(Function):
# All the components created in gdbarch-components.py. # All the components created in gdbarch-components.py.
components = [] components: List[Component] = []

View file

@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # 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. # Search the tool source itself for the correct copyright years.
with open(tool, "r") as f: with open(tool, "r") as f:
for line in f: for line in f: