From b244bde82aee806f1f39cbcdcc6c00bfe4083c5c Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 19 Apr 2025 13:31:44 -0400 Subject: [PATCH 1/2] Simplify various repr calls --- com/win32com/client/__init__.py | 9 +++------ com/win32com/client/build.py | 10 ++++------ com/win32com/client/dynamic.py | 2 +- com/win32comext/axscript/client/framework.py | 9 ++------- win32/Lib/win32timezone.py | 7 ++----- 5 files changed, 12 insertions(+), 25 deletions(-) diff --git a/com/win32com/client/__init__.py b/com/win32com/client/__init__.py index 84e59a2fa2..a261c516ed 100644 --- a/com/win32com/client/__init__.py +++ b/com/win32com/client/__init__.py @@ -557,11 +557,8 @@ def __dir__(self): def __repr__(self): # Need to get the docstring for the module for this class. try: - mod_doc = sys.modules[self.__class__.__module__].__doc__ - if mod_doc: - mod_name = "win32com.gen_py." + mod_doc - else: - mod_name = sys.modules[self.__class__.__module__].__name__ + module = sys.modules[self.__class__.__module__] + mod_name = "win32com.gen_py." + (module.__doc__ or module.__name__) except KeyError: mod_name = "win32com.gen_py.unknown" return f"<{mod_name}.{self.__class__.__name__} instance at 0x{id(self)}>" @@ -630,7 +627,7 @@ def __init__(self, oobj=None): self.__dict__["_dispobj_"] = self.default_interface(oobj) def __repr__(self): - return f"" + return f"<{self.__class__.__module__}.{self.__class__.__name__}>" def __getattr__(self, attr): d = self.__dict__["_dispobj_"] diff --git a/com/win32com/client/build.py b/com/win32com/client/build.py index 4b7628438e..fa36af096f 100644 --- a/com/win32com/client/build.py +++ b/com/win32com/client/build.py @@ -70,7 +70,7 @@ class NotSupportedException(Exception): class MapEntry: - "Simple holder for named attibutes - items in a map." + """Simple holder for named attributes - items in a map.""" def __init__( self, @@ -98,11 +98,9 @@ def __init__( self.hidden = hidden def __repr__(self): - return ( - "MapEntry(dispid={s.dispid}, desc={s.desc}, names={s.names}, doc={s.doc!r}, " - "resultCLSID={s.resultCLSID}, resultDocumentation={s.resultDocumentation}, " - "wasProperty={s.wasProperty}, hidden={s.hidden}" - ).format(s=self) + return "MapEntry(" + ", ".join( + [f"{key}={value!r}" for key, value in self.__dict__.items()] + ) def GetResultCLSID(self): rc = self.resultCLSID diff --git a/com/win32com/client/dynamic.py b/com/win32com/client/dynamic.py index 3738da9031..9b6b8deb56 100644 --- a/com/win32com/client/dynamic.py +++ b/com/win32com/client/dynamic.py @@ -221,7 +221,7 @@ def __bool__(self): # desirable??? def __repr__(self): - return "" % (self._username_) + return f"" def __str__(self): # __str__ is used when the user does "print(object)", so we gracefully diff --git a/com/win32comext/axscript/client/framework.py b/com/win32comext/axscript/client/framework.py index c4a04329f8..6f39152435 100644 --- a/com/win32comext/axscript/client/framework.py +++ b/com/win32comext/axscript/client/framework.py @@ -166,7 +166,7 @@ def __init__(self): self.name = "" def __repr__(self): - return "<%s at %d: %s>" % (self.__class__.__name__, id(self), self.name) + return f"<{self.__class__.__name__} at {id(self)}: {self.name}>" def Reset(self): pass @@ -318,12 +318,7 @@ def __repr__(self): flagsDesc = "" if self.flags is not None and self.flags & axscript.SCRIPTITEM_GLOBALMEMBERS: flagsDesc = "/Global" - return "<%s at %d: %s%s>" % ( - self.__class__.__name__, - id(self), - self.name, - flagsDesc, - ) + return f"<{self.__class__.__name__} at {id(self)}: {self.name}{flagsDesc}>" def _dump_(self, level): flagDescs = [] diff --git a/win32/Lib/win32timezone.py b/win32/Lib/win32timezone.py index d9f69700ab..411713949b 100644 --- a/win32/Lib/win32timezone.py +++ b/win32/Lib/win32timezone.py @@ -663,11 +663,8 @@ def _LoadDynamicInfoFromKey(self, key) -> None: ) def __repr__(self) -> str: - result = f"{self.__class__.__name__}({self.timeZoneName!r}" - if self.fixedStandardTime: - result += ", True" - result += ")" - return result + fix_standard_time = ", True" if self.fixedStandardTime else "" + return f"{self.__class__.__name__}({self.timeZoneName!r}{fix_standard_time})" def __str__(self) -> str: return self.displayName From ab07a27ac496023d352dc6d324749c9e9c3477c1 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 19 Apr 2025 14:03:05 -0400 Subject: [PATCH 2/2] Discard changes to com/win32com/client/build.py --- com/win32com/client/build.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/com/win32com/client/build.py b/com/win32com/client/build.py index fa36af096f..4b7628438e 100644 --- a/com/win32com/client/build.py +++ b/com/win32com/client/build.py @@ -70,7 +70,7 @@ class NotSupportedException(Exception): class MapEntry: - """Simple holder for named attributes - items in a map.""" + "Simple holder for named attibutes - items in a map." def __init__( self, @@ -98,9 +98,11 @@ def __init__( self.hidden = hidden def __repr__(self): - return "MapEntry(" + ", ".join( - [f"{key}={value!r}" for key, value in self.__dict__.items()] - ) + return ( + "MapEntry(dispid={s.dispid}, desc={s.desc}, names={s.names}, doc={s.doc!r}, " + "resultCLSID={s.resultCLSID}, resultDocumentation={s.resultDocumentation}, " + "wasProperty={s.wasProperty}, hidden={s.hidden}" + ).format(s=self) def GetResultCLSID(self): rc = self.resultCLSID