From 5b0a76bede0b49fd8926607e648fcc539b98cde0 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Fri, 19 May 2023 12:53:48 +0100 Subject: [PATCH 1/2] gh-104050: Argument clinic: Add annotations to the `LandMine` class --- Tools/clinic/clinic.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 3d4961e6e7d7dd..e05fc42e617f2c 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -29,7 +29,7 @@ from collections.abc import Callable from types import FunctionType, NoneType -from typing import Any, Final, NamedTuple, NoReturn, Literal, overload +from typing import TYPE_CHECKING, Any, Final, NamedTuple, NoReturn, Literal, overload # TODO: # @@ -2554,17 +2554,21 @@ def get_displayname(self, i): class LandMine: # try to access any - def __init__(self, message): + def __init__(self, message: str) -> None: self.__message__ = message - def __repr__(self): + def __repr__(self) -> str: return '" - def __getattribute__(self, name): - if name in ('__repr__', '__message__'): - return super().__getattribute__(name) - # raise RuntimeError(repr(name)) - fail("Stepped on a land mine, trying to access attribute " + repr(name) + ":\n" + self.__message__) + if not TYPE_CHECKING: + def __getattribute__(self, name): + if name in ('__repr__', '__message__'): + return super().__getattribute__(name) + # raise RuntimeError(repr(name)) + fail( + f"Stepped on a land mine, trying to access attribute " + f"{name!r}:\n{self.__message__}" + ) def add_c_converter(f, name=None): From 75abb8790e9b9c8a4dac19e9450abdf2c078c042 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 19 May 2023 15:06:08 +0100 Subject: [PATCH 2/2] comment --- Tools/clinic/clinic.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index e05fc42e617f2c..9b5c6333bfb1e4 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2560,6 +2560,9 @@ def __init__(self, message: str) -> None: def __repr__(self) -> str: return '" + # Type checkers wouldn't check attribute access on instances of this class properly + # if they saw it had a __getattribute__ method, + # so pretend to type checkers that the __getattribute__ method doesn't exist if not TYPE_CHECKING: def __getattribute__(self, name): if name in ('__repr__', '__message__'):