Skip to content

Commit 433abb6

Browse files
committed
Improve and fix repr methods
1 parent 84a050c commit 433abb6

File tree

16 files changed

+57
-55
lines changed

16 files changed

+57
-55
lines changed

AutoDuck/Dump2HHC.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __str__(self):
4848
{"context": self.context, "name": self.name, "contains": self.contains}
4949
)
5050

51-
def __repr__(self):
51+
def __repr__(self) -> str:
5252
if len(self.contains) > 0:
5353
return repr(
5454
{"context": self.context, "name": self.name, "contains": self.contains}

CHANGES.txt

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ https://mhammond.github.io/pywin32_installers.html .
1414
Coming in build 311, as yet unreleased
1515
--------------------------------------
1616
* Fixed a regression that broke special __dunder__ methods with CoClass. (#1870, #2493, @Avasam, @geppi)
17+
* Adding missing quotes and closing parenthesis to `win32com.client.build.MapEntry`'s `repr`
18+
* The following classes will now use the correct subclass name in `repr`:
19+
* `win32rcparser.StringDef`
20+
* `win32pdhquery.QueryError`
21+
* `pywin.tools.browser.HLIPythonObject`
22+
* `win32comext.axscript.client.pyscript.NamedScriptAttribute`
23+
* `win32comext.axscript.client.error.AXScriptException`
24+
* `win32comext.axdebug.debugger.ModuleTreeNode`
25+
* `win32com.server.exception.COMException`
26+
* `win32com.client.VARIANT`
27+
* The following classes now produce a valid `eval` string representation when calling `repr`:
28+
* `pywin.tools.browser.HLIPythonObject`
29+
* `win32comext.axscript.client.pyscript.NamedScriptAttribute`
30+
* `win32comext.axscript.client.error.AXScriptException`
31+
* `win32com.server.exception.COMException`
1732

1833
Build 310, released 2025/03/16
1934
------------------------------

Pythonwin/pywin/tools/browser.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ def __lt__(self, other):
4444
def __eq__(self, other):
4545
return self.name == other.name
4646

47-
def __repr__(self):
48-
try:
49-
type = self.GetHLIType()
50-
except:
51-
type = "Generic"
52-
return f"HLIPythonObject({type}) - name: {self.name} object: {self.myobject!r}"
47+
def __repr__(self) -> str:
48+
return (
49+
f"{self.__class__.__name__}(name={self.name!r}, object={self.myobject!r})"
50+
)
5351

5452
def GetText(self):
5553
try:

Pythonwin/pywin/tools/regedit.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,9 @@ def __eq__(self, other):
331331
and self.userName == other.userName
332332
)
333333

334-
def __repr__(self):
335-
return "<{} with root={}, key={}>".format(
336-
self.__class__.__name__,
337-
self.keyRoot,
338-
self.keyName,
334+
def __repr__(self) -> str:
335+
return (
336+
f"<{self.__class__.__name__} with root={self.keyRoot}, key={self.keyName}>"
339337
)
340338

341339
def GetText(self):

com/win32com/client/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def __dir__(self):
555555
return list(set(attributes))
556556

557557
# Provide a prettier name than the CLSID
558-
def __repr__(self):
558+
def __repr__(self) -> str:
559559
# Need to get the docstring for the module for this class.
560560
try:
561561
mod_doc = sys.modules[self.__class__.__module__].__doc__
@@ -630,7 +630,7 @@ def __init__(self, oobj=None):
630630
oobj = pythoncom.new(self.CLSID)
631631
self.__dict__["_dispobj_"] = self.default_interface(oobj)
632632

633-
def __repr__(self):
633+
def __repr__(self) -> str:
634634
return f"<win32com.gen_py.{__doc__}.{self.__class__.__name__}>"
635635

636636
def __getattr__(self, attr):
@@ -700,5 +700,5 @@ def _del_value(self):
700700

701701
value = property(_get_value, _set_value, _del_value)
702702

703-
def __repr__(self):
704-
return f"win32com.client.VARIANT({self.varianttype!r}, {self._value!r})"
703+
def __repr__(self) -> str:
704+
return f"{self.__class__.__qualname__}({self.varianttype!r}, {self._value!r})"

com/win32com/client/build.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class NotSupportedException(Exception):
7070

7171

7272
class MapEntry:
73-
"Simple holder for named attibutes - items in a map."
73+
"""Simple holder for named attributes - items in a map."""
7474

7575
def __init__(
7676
self,
@@ -92,17 +92,16 @@ def __init__(
9292
self.doc = doc
9393
self.resultCLSID = resultCLSID
9494
self.resultDocumentation = resultDoc
95-
self.wasProperty = (
96-
0 # Have I been transformed into a function so I can pass args?
97-
)
95+
self.wasProperty = 0
96+
"""Has this MapEntry been transformed into a function so it can pass args?"""
9897
self.hidden = hidden
9998

100-
def __repr__(self):
99+
def __repr__(self) -> str:
101100
return (
102-
"MapEntry(dispid={s.dispid}, desc={s.desc}, names={s.names}, doc={s.doc!r}, "
103-
"resultCLSID={s.resultCLSID}, resultDocumentation={s.resultDocumentation}, "
104-
"wasProperty={s.wasProperty}, hidden={s.hidden}"
105-
).format(s=self)
101+
"MapEntry("
102+
+ ", ".join([f"{key}{value!r}" for key, value in self.__dict__.items()])
103+
+ ")"
104+
)
106105

107106
def GetResultCLSID(self):
108107
rc = self.resultCLSID

com/win32com/client/dynamic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ def __bool__(self):
220220
# _Possibly_ want to defer to __len__ if available, but I'm not sure this is
221221
# desirable???
222222

223-
def __repr__(self):
224-
return "<COMObject %s>" % (self._username_)
223+
def __repr__(self) -> str:
224+
return f"<COMObject {self._username_}>"
225225

226226
def __str__(self):
227227
# __str__ is used when the user does "print(object)", so we gracefully

com/win32com/client/genpy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def __lt__(self, other):
111111
return self.doc < other.doc
112112
return self.order < other.order
113113

114-
def __repr__(self):
114+
def __repr__(self) -> str:
115115
return f"OleItem: doc={self.doc!r}, order={self.order}"
116116

117117

com/win32com/server/exception.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def __init__(
7777
# todo - fill in the exception value
7878
pythoncom.com_error.__init__(self, scode, self.description, None, -1)
7979

80-
def __repr__(self):
81-
return f"<COM Exception - scode={self.scode}, desc={self.description}>"
80+
def __repr__(self) -> str:
81+
return f"{self.__class__.__name__}(scode={self.scode!r}, desc={self.description!r})"
8282

8383

8484
def IsCOMException(t=None):

com/win32comext/axdebug/debugger.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def __init__(self, module):
1818
self.realNode = None
1919
self.cont = codecontainer.SourceModuleContainer(module)
2020

21-
def __repr__(self):
22-
return f"<ModuleTreeNode wrapping {self.module}>"
21+
def __repr__(self) -> str:
22+
return f"<{self.__class__.__name__} wrapping {self.module}>"
2323

2424
def Attach(self, parentRealNode):
2525
self.realNode.Attach(parentRealNode)

com/win32comext/axscript/client/error.py

-3
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,6 @@ def ExtractTracebackInfo(self, tb: TracebackType, site: COMScript):
218218
line = None
219219
return filename, lineno, name, line
220220

221-
def __repr__(self):
222-
return "AXScriptException Object with description:" + self.description
223-
224221

225222
def ProcessAXScriptException(
226223
scriptingSite: AXSite,

com/win32comext/axscript/client/framework.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ class Event:
165165
def __init__(self):
166166
self.name = "<None>"
167167

168-
def __repr__(self):
169-
return "<%s at %d: %s>" % (self.__class__.__name__, id(self), self.name)
168+
def __repr__(self) -> str:
169+
return f"<{self.__class__.__name__} at {id(self)}: {self.name}>"
170170

171171
def Reset(self):
172172
pass
@@ -314,16 +314,11 @@ def __init__(self, parentItem, name, dispatch, flags):
314314

315315
# trace("Creating ScriptItem", name, "of parent", parentItem,"with dispatch", dispatch)
316316

317-
def __repr__(self):
317+
def __repr__(self) -> str:
318318
flagsDesc = ""
319319
if self.flags is not None and self.flags & axscript.SCRIPTITEM_GLOBALMEMBERS:
320320
flagsDesc = "/Global"
321-
return "<%s at %d: %s%s>" % (
322-
self.__class__.__name__,
323-
id(self),
324-
self.name,
325-
flagsDesc,
326-
)
321+
return f"<{self.__class__.__name__} at {id(self)}: {self.name}{flagsDesc}>"
327322

328323
def _dump_(self, level):
329324
flagDescs = []

com/win32comext/axscript/client/pyscript.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ class NamedScriptAttribute:
108108
def __init__(self, scriptItem):
109109
self.__dict__["_scriptItem_"] = scriptItem
110110

111-
def __repr__(self):
112-
return f"<NamedItemAttribute{self._scriptItem_!r}>"
111+
def __repr__(self) -> str:
112+
return f"{self.__class__.__name__}({self._scriptItem_!r})"
113113

114114
def __getattr__(self, attr):
115115
# If a known subitem, return it.

win32/Lib/win32pdhquery.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ class QueryError(Exception):
564564
def __init__(self, query: BaseQuery):
565565
self.query = query
566566

567-
def __repr__(self):
568-
return f"<Query Error in {self.query!r}>"
567+
def __repr__(self) -> str:
568+
return f"<{self.__class__.__name__} in {self.query!r}>"
569569

570570
__str__ = __repr__

win32/Lib/win32rcparser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ def __init__(self, id, idNum, value):
168168
self.idNum = idNum
169169
self.value = value
170170

171-
def __repr__(self):
172-
return f"StringDef({self.id!r}, {self.idNum!r}, {self.value!r})"
171+
def __repr__(self) -> str:
172+
return f"{self.__class__.__name__}({self.id!r}, {self.idNum!r}, {self.value!r})"
173173

174174

175175
class RCParser:

win32/Lib/win32timezone.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,11 @@ def _LoadDynamicInfoFromKey(self, key) -> None:
663663
)
664664

665665
def __repr__(self) -> str:
666-
result = f"{self.__class__.__name__}({self.timeZoneName!r}"
667-
if self.fixedStandardTime:
668-
result += ", True"
669-
result += ")"
670-
return result
666+
return (
667+
f"{self.__class__.__name__}({self.timeZoneName!r}"
668+
+ (", True" if self.fixedStandardTime else "")
669+
+ ")"
670+
)
671671

672672
def __str__(self) -> str:
673673
return self.displayName

0 commit comments

Comments
 (0)