Skip to content

Commit 7e2221a

Browse files
committed
Respect subclass names in repr
1 parent 16c4981 commit 7e2221a

File tree

10 files changed

+22
-13
lines changed

10 files changed

+22
-13
lines changed

CHANGES.txt

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ Coming in build 311, as yet unreleased
1616
* Fixed a regression that broke special __dunder__ methods with CoClass. (#1870, #2493, @Avasam, @geppi)
1717
* Fixed a memory leak when SafeArrays are used as out parameters (@the-snork)
1818
* Fixed dispatch handling for properties (@the-snork)
19+
* The following classes will now use the correct subclass name in `repr`: (#2570, @Avasam)
20+
* `pywin.tools.browser.HLIPythonObject`
21+
* `win32com.client.VARIANT`
22+
* `win32com.client.build.MapEntry`
23+
* `win32com.server.exception.COMException`
24+
* `win32comext.axdebug.debugger.ModuleTreeNode`
25+
* `win32comext.axscript.client.pyscript.NamedScriptAttribute`
26+
* `win32comext.axscript.client.error.AXScriptException`
27+
* `win32pdhquery.QueryError`
28+
* `win32rcparser.StringDef`
1929

2030
Build 310, released 2025/03/16
2131
------------------------------

Pythonwin/pywin/tools/browser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __repr__(self):
4949
type = self.GetHLIType()
5050
except:
5151
type = "Generic"
52-
return f"HLIPythonObject({type}) - name: {self.name} object: {self.myobject!r}"
52+
return f"{self.__class__.__name__}({type}) - name: {self.name} object: {self.myobject!r}"
5353

5454
def GetText(self):
5555
try:

com/win32com/client/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def __bool__(self):
678678

679679
# A very simple VARIANT class. Only to be used with poorly-implemented COM
680680
# objects. If an object accepts an arg which is a simple "VARIANT", but still
681-
# is very pickly about the actual variant type (eg, isn't happy with a VT_I4,
681+
# is very picky about the actual variant type (eg, isn't happy with a VT_I4,
682682
# which it would get from a Python integer), you can use this to force a
683683
# particular VT.
684684
class VARIANT:
@@ -700,4 +700,4 @@ def _del_value(self):
700700
value = property(_get_value, _set_value, _del_value)
701701

702702
def __repr__(self):
703-
return f"win32com.client.VARIANT({self.varianttype!r}, {self._value!r})"
703+
return f"{self.__class__.__module__}.{self.__class__.__name__}({self.varianttype!r}, {self._value!r})"

com/win32com/client/build.py

+2-2
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,
@@ -99,7 +99,7 @@ def __init__(
9999

100100
def __repr__(self):
101101
return (
102-
"MapEntry(dispid={s.dispid}, desc={s.desc}, names={s.names}, doc={s.doc!r}, "
102+
"{s.__class__.__name__}(dispid={s.dispid}, desc={s.desc}, names={s.names}, doc={s.doc!r}, "
103103
"resultCLSID={s.resultCLSID}, resultDocumentation={s.resultDocumentation}, "
104104
"wasProperty={s.wasProperty}, hidden={s.hidden}"
105105
).format(s=self)

com/win32com/server/exception.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def __init__(
7878
pythoncom.com_error.__init__(self, scode, self.description, None, -1)
7979

8080
def __repr__(self):
81-
return f"<COM Exception - scode={self.scode}, desc={self.description}>"
81+
return (
82+
f"<{self.__class__.__name__} - scode={self.scode}, desc={self.description}>"
83+
)
8284

8385

8486
def IsCOMException(t=None):

com/win32comext/axdebug/debugger.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, module):
1919
self.cont = codecontainer.SourceModuleContainer(module)
2020

2121
def __repr__(self):
22-
return f"<ModuleTreeNode wrapping {self.module}>"
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/pyscript.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def __init__(self, scriptItem):
109109
self.__dict__["_scriptItem_"] = scriptItem
110110

111111
def __repr__(self):
112-
return f"<NamedItemAttribute{self._scriptItem_!r}>"
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,6 @@ def __init__(self, query: BaseQuery):
565565
self.query = query
566566

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

570570
__str__ = __repr__

win32/Lib/win32rcparser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def __init__(self, id, idNum, value):
169169
self.value = value
170170

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

174174

175175
class RCParser:

0 commit comments

Comments
 (0)