From patchwork Sat Feb 25 03:30:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 65641 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8364D385086D for ; Sat, 25 Feb 2023 03:37:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8364D385086D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677296253; bh=KdxIHehERc+mqLrybDvLvAZOugCPbE5fl/mwbVxcQsc=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=ot2+z30jjLC+nHSQX62b6vwJ7k9uq3J5Ovg6l6x72kcXgDWCrop9cogVeFsdMlk/C W1XinxdMCAHPMiGOHtaYlBtFH+oBoD/yAEaTgukKHrbrBFKxC+rwT4ZbzJKv5EfQzt du3o/ZJ4q0NXy2pfzZ9nIWl5NbMhVICbfGgPWAfg= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 4F025385B53C for ; Sat, 25 Feb 2023 03:37:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4F025385B53C Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 31P3b0R8030131 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 24 Feb 2023 22:37:05 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 31P3b0R8030131 Received: from simark.localdomain (unknown [217.28.27.60]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 2DD841E9EF; Fri, 24 Feb 2023 22:30:51 -0500 (EST) To: gdb-patches@sourceware.org Cc: Simon Marchi , Tom Tromey Subject: [PATCH v2 07/10] gdb: gdbarch*.py, copyright.py: add type annotations Date: Fri, 24 Feb 2023 22:30:44 -0500 Message-Id: <20230225033047.373899-8-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230225033047.373899-1-simon.marchi@polymtl.ca> References: <20230225033047.373899-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Sat, 25 Feb 2023 03:37:00 +0000 X-Spam-Status: No, score=-3189.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Simon Marchi via Gdb-patches From: Simon Marchi Reply-To: Simon Marchi Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" 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 --- gdb/gdbarch.py | 8 ++-- gdb/gdbarch_components.py | 5 ++- gdb/gdbarch_types.py | 85 ++++++++++++++++++++------------------- gdb/gdbcopyright.py | 2 +- 4 files changed, 51 insertions(+), 49 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..caa65c334eca 100644 --- a/gdb/gdbarch_components.py +++ b/gdb/gdbarch_components.py @@ -166,13 +166,14 @@ Number of bits in an int or unsigned int for the target machine. 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 +184,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 aca23850b911..9eb38c987a2c 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 . +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=None, - 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] = None, + 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,24 +82,24 @@ class _Component: return predicate -class Info(_Component): +class Info(Component): "An Info component is copied from the gdbarch_info." -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, @@ -112,24 +113,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, @@ -180,4 +181,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 . -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: