Skip to content

Commit baa91ac

Browse files
authored
Add tests for DictPropertyBag and type hints for IPropertyBag. (#860)
* Add `Test_DictPropertyBag` for `IPropertyBag` interface. * Add type hints for `IPropertyBag` methods. - Introduce `TYPE_CHECKING` block for `Read` and `Write` methods in `IPropertyBag`.
1 parent 8dcee01 commit baa91ac

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

comtypes/persist.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from ctypes import POINTER, Structure, c_int, c_ulong, c_ushort, c_wchar_p
1414
from ctypes.wintypes import BOOL, DWORD, WORD
15-
from typing import TYPE_CHECKING
15+
from typing import TYPE_CHECKING, Any, Optional
1616

1717
from comtypes import COMMETHOD, GUID, HRESULT, IPersist, IUnknown
1818
from comtypes.automation import VARIANT, tagEXCEPINFO
@@ -65,6 +65,13 @@ class IPropertyBag(IUnknown):
6565
),
6666
]
6767

68+
if TYPE_CHECKING:
69+
70+
def Read(
71+
self, pszPropName: str, pVar: VARIANT, pErrorLog: Optional[IErrorLog]
72+
) -> Any: ...
73+
def Write(self, pszPropName: str, pVar: Any) -> hints.Hresult: ...
74+
6875

6976
class IPersistPropertyBag(IPersist):
7077
_iid_ = GUID("{37D84F60-42CB-11CE-8135-00AA004BB851}")

comtypes/test/test_persist.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import tempfile
22
import unittest as ut
3+
from _ctypes import COMError
34
from pathlib import Path
45

5-
from comtypes import GUID, CoCreateInstance, IPersist, persist
6+
from comtypes import GUID, CoCreateInstance, IPersist, hresult, persist
7+
from comtypes.automation import VARIANT
68

79
CLSID_ShellLink = GUID("{00021401-0000-0000-C000-000000000046}")
810

@@ -36,3 +38,34 @@ def test_save(self):
3638
pf.Save(str(tgt_file), True)
3739
self.assertEqual(pf.GetCurFile(), str(tgt_file))
3840
self.assertTrue(tgt_file.exists())
41+
42+
43+
class Test_DictPropertyBag(ut.TestCase):
44+
def create_itf_ptr(self) -> persist.IPropertyBag:
45+
# Create a DictPropertyBag instance with some initial values
46+
impl = persist.DictPropertyBag(Key1="value1", Key2=123, Key3=True)
47+
# Get the IPropertyBag interface pointer
48+
itf = impl.QueryInterface(persist.IPropertyBag)
49+
return itf
50+
51+
def test_read_existing_properties(self):
52+
itf = self.create_itf_ptr()
53+
self.assertEqual(itf.Read("Key1", VARIANT(), None), "value1")
54+
self.assertEqual(itf.Read("Key2", VARIANT(), None), 123)
55+
self.assertEqual(itf.Read("Key3", VARIANT(), None), True)
56+
57+
def test_write_new_property(self):
58+
itf = self.create_itf_ptr()
59+
itf.Write("Key4", "new_value")
60+
self.assertEqual(itf.Read("Key4", VARIANT(), None), "new_value")
61+
62+
def test_update_existing_property(self):
63+
itf = self.create_itf_ptr()
64+
itf.Write("Key1", "updated_value")
65+
self.assertEqual(itf.Read("Key1", VARIANT(), None), "updated_value")
66+
67+
def test_read_non_existent_property(self):
68+
itf = self.create_itf_ptr()
69+
with self.assertRaises(COMError) as cm:
70+
itf.Read("NonExistentProp", VARIANT(), None)
71+
self.assertEqual(cm.exception.hresult, hresult.E_INVALIDARG)

0 commit comments

Comments
 (0)