-
-
Notifications
You must be signed in to change notification settings - Fork 104
Description
The following is pointed in python/typeshed#12982 (comment).
I did a local comparison using
mypy --strictand there were only two things that meaningfully changed, both additional findings:> comtypes/_memberspec.py:191: error: "type[_CData]" has no attribute "from_param" [attr-defined] > comtypes/test/find_memleak.py:26: error: Too many values to unpack (2 expected, 3 provided) [misc]
The second is from a line like this:
for n, _ in self._fields_:This is also a correct addition.
_fields_is typically a list of 2-tuples, and on the class in question within comtypes that's true, but in the general case it's valid for_fields_to include 3-tuples (c.f. https://docs.python.org/3/library/ctypes.html#ctypes.Structure._fields_). It's safe because your class doesn't use any 3-tuple fields, but I don't think there's anything that could be done in the stubs that would allow mypy to realize that.
Therefore, the following would be changed as shown.
comtypes/comtypes/test/test_urlhistory.py
Lines 20 to 26 in 2b411e5
| from comtypes.util import cast_field | |
| result = type(self)() | |
| for n, _ in self._fields_: | |
| setattr(result, n, getattr(self, n)) | |
| url, title = self.pwcsUrl, self.pwcsTitle | |
| windll.ole32.CoTaskMemFree(cast_field(self, "pwcsUrl", c_void_p)) |
from comtypes.util import cast_field
result = type(self)()
- for n, _ in self._fields_:
+ for n, *_ in self._fields_:
setattr(result, n, getattr(self, n))
url, title = self.pwcsUrl, self.pwcsTitle
windll.ole32.CoTaskMemFree(cast_field(self, "pwcsUrl", c_void_p))Additionally, similar code exists elsewhere, and pyright in my environment was issuing warnings about it. This would be changed as well.
comtypes/comtypes/test/find_memleak.py
Lines 23 to 27 in 2b411e5
| self.cb = sizeof(self) | |
| def dump(self): | |
| for n, _ in self._fields_[2:]: | |
| print(n, getattr(self, n) / 1e6) |
self.cb = sizeof(self)
def dump(self):
- for n, _ in self._fields_[2:]:
+ for n, *_ in self._fields_[2:]:
print(n, getattr(self, n) / 1e6)