Add an opt-in value view for WinRT Object-valued maps in Python - #195
Open
leileizhang (lei9444) wants to merge 3 commits into
Open
leileizhang (lei9444) wants to merge 3 commits into
leileizhang (lei9444) wants to merge 3 commits into
Conversation
dynwinrt.values.object_value_view(mapping, *, preserve_type=False) wraps a generated IMap or IMapView wrapper whose values are Object, such as PropertySet, ValueSet or DeviceInformation.properties. It returns a live MutableObjectValueView or read-only ObjectValueView that holds no WinRT reference of its own: reads unbox with unbox_object(), writes box with to_winrt_object()'s default rules, and view.raw is the generated map. QueryInterface for IMap/IMapView<String or Guid, Object> confirms the value type, so other maps such as StringMap raise TypeError, and the overloads reject them statically. A box without a Python form (an unsupported PropertyType or a DateTime outside datetime's range) reads back raw so that dict(view) does not fail on one odd entry. Generated code, unbox_object() and to_winrt_object() are unchanged. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
update() writes through __setitem__ and to_winrt_object(), so its overloads now accept any value, as item assignment already does. They mirror MutableMapping.update(), which allows keyword arguments only for str keys. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
setdefault() now accepts any default that to_winrt_object() accepts, like __setitem__ and update(), and returns the read type: after storing a missing key it reads the value back, so (1, 2) returns [1, 2] and a runtime object its DynWinRTValue. MutableMapping.setdefault() would return the default itself. A targeted type: ignore[override] covers typeshed's self-typed overload, which infers "-> None" for a value type that includes None. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Code that reads and writes many values in
PropertySet,ValueSet,DeviceInformation.propertiesor the map fromretrieve_properties_async()has to wrap every access inunbox_object()orto_winrt_object()(#194).Key changes
dynwinrt.values.object_value_view(mapping, *, preserve_type=False)returns a live view:MutableObjectValueView(MutableMapping) for a generatedIMapwrapper;ObjectValueView(Mapping) for anIMapViewwrapper.The view holds no WinRT reference of its own.
Reads unbox with
unbox_object(). WinRT null reads asNone. A runtime object comes back as itsDynWinRTValue. So does a box with no Python form: an unsupportedPropertyType, or aDateTimeoutsidedatetime's range.Writes use
to_winrt_object()'s default rules, and its errors propagate unchanged.view.rawis the generated map. Its values stay native and keep their COM identity.A QueryInterface check confirms
IMap/IMapView<String or Guid, Object>. Any other map, such asStringMap, raisesTypeError, and the typing overloads reject it too.Tests:
--strictconsumer check.Notes
unbox_object()andto_winrt_object()are unchanged, and there are no codegen or native changes.IPropertySetwrappers, such asApplicationDataContainer.values, are not Python mappings. Passvalues.as_interface(IMap_String_Object)instead.