Skip to content

Commit 94b81af

Browse files
authored
change _MemberSpecs to NamedTuples (enthought#484)
* change `_MemberSpec` to `NamedTuple` * `_ComMemberSpec` is no more `Generic` for Py<3.11 backward compatibility. * apply `black` style
1 parent 9724dc0 commit 94b81af

File tree

2 files changed

+31
-53
lines changed

2 files changed

+31
-53
lines changed

comtypes/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -720,17 +720,17 @@ class dispid(int):
720720
# instances with more methods or properties, and should not behave as an unpackable.
721721

722722

723-
def STDMETHOD(restype, name, argtypes=()):
723+
def STDMETHOD(restype, name, argtypes=()) -> _ComMemberSpec:
724724
"Specifies a COM method slot without idlflags"
725725
return _ComMemberSpec(restype, name, argtypes, None, (), None)
726726

727727

728-
def DISPMETHOD(idlflags, restype, name, *argspec):
728+
def DISPMETHOD(idlflags, restype, name, *argspec) -> _DispMemberSpec:
729729
"Specifies a method of a dispinterface"
730730
return _DispMemberSpec("DISPMETHOD", name, tuple(idlflags), restype, argspec)
731731

732732

733-
def DISPPROPERTY(idlflags, proptype, name):
733+
def DISPPROPERTY(idlflags, proptype, name) -> _DispMemberSpec:
734734
"Specifies a property of a dispinterface"
735735
return _DispMemberSpec("DISPPROPERTY", name, tuple(idlflags), proptype, ())
736736

@@ -744,7 +744,7 @@ def DISPPROPERTY(idlflags, proptype, name):
744744
# )
745745

746746

747-
def COMMETHOD(idlflags, restype, methodname, *argspec):
747+
def COMMETHOD(idlflags, restype, methodname, *argspec) -> _ComMemberSpec:
748748
"""Specifies a COM method slot with idlflags.
749749
750750
XXX should explain the sematics of the arguments.

comtypes/_memberspec.py

+27-49
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Dict,
66
Iterator,
77
List,
8+
NamedTuple,
89
Optional,
910
Tuple,
1011
Type,
@@ -82,56 +83,28 @@ def _resolve_argspec(
8283
return tuple(paramflags), tuple(argtypes)
8384

8485

85-
class _MemberSpec(object):
86-
"""Specifier of a slot of method or property."""
87-
88-
__slots__ = ("name", "idlflags", "restype")
86+
class _ComMemberSpec(NamedTuple):
87+
"""Specifier for a slot of COM method or property."""
8988

90-
def __init__(self, name, idlflags, restype):
91-
self.name: str = name
92-
self.idlflags: Tuple[_UnionT[str, int], ...] = idlflags
93-
self.restype: Optional[Type[_CData]] = restype
89+
restype: Optional[Type[_CData]]
90+
name: str
91+
argtypes: Tuple[Type[_CData], ...]
92+
paramflags: Optional[Tuple[_ParamFlagType, ...]]
93+
idlflags: Tuple[_UnionT[str, int], ...]
94+
doc: Optional[str]
9495

9596
def is_prop(self) -> bool:
96-
propflags = ("propget", "propput", "propputref")
97-
return any(f in propflags for f in self.idlflags)
97+
return _is_spec_prop(self)
9898

9999

100-
class _ComMemberSpec(_MemberSpec):
101-
"""Specifier for a slot of COM method or property."""
102-
103-
__slots__ = ("argtypes", "paramflags", "doc")
104-
105-
def __init__(self, restype, name, argtypes, paramflags, idlflags, doc):
106-
self.argtypes: Tuple[Type[_CData], ...] = argtypes
107-
self.paramflags: Optional[Tuple[_ParamFlagType, ...]] = paramflags
108-
self.doc: Optional[str] = doc
109-
super(_ComMemberSpec, self).__init__(name, idlflags, restype)
110-
111-
def __iter__(self):
112-
# for backward compatibility:
113-
# A function that returns this object used to return a `tuple`.
114-
# So it is implemented as unpackable as well.
115-
for item in (
116-
self.restype,
117-
self.name,
118-
self.argtypes,
119-
self.paramflags,
120-
self.idlflags,
121-
self.doc,
122-
):
123-
yield item
124-
125-
126-
class _DispMemberSpec(_MemberSpec):
100+
class _DispMemberSpec(NamedTuple):
127101
"""Specifier for a slot of dispinterface method or property."""
128102

129-
__slots__ = ("what", "argspec")
130-
131-
def __init__(self, what, name, idlflags, restype, argspec):
132-
self.what: str = what
133-
self.argspec: Tuple[_ArgSpecElmType, ...] = argspec
134-
super(_DispMemberSpec, self).__init__(name, idlflags, restype)
103+
what: str
104+
name: str
105+
idlflags: Tuple[_UnionT[str, int], ...]
106+
restype: Optional[Type[_CData]]
107+
argspec: Tuple[_ArgSpecElmType, ...]
135108

136109
@property
137110
def memid(self) -> int:
@@ -140,12 +113,17 @@ def memid(self) -> int:
140113
except IndexError:
141114
raise TypeError("no dispid found in idlflags")
142115

143-
def __iter__(self):
144-
# for backward compatibility:
145-
# A function that returns this object used to return a `tuple`.
146-
# So it is implemented as unpackable as well.
147-
for item in (self.what, self.name, self.idlflags, self.restype, self.argspec):
148-
yield item
116+
def is_prop(self) -> bool:
117+
return _is_spec_prop(self)
118+
119+
120+
# Specifier of a slot of method or property.
121+
# This should be `typing.Protocol` if supporting Py3.8+ only.
122+
_MemberSpec = _UnionT[_ComMemberSpec, _DispMemberSpec]
123+
124+
125+
def _is_spec_prop(m: _MemberSpec):
126+
return any(f in ("propget", "propput", "propputref") for f in m.idlflags)
149127

150128

151129
_PropFunc = Optional[Callable[..., Any]]

0 commit comments

Comments
 (0)