You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This function is similar to the function I proposed here #484. These two functions can be integrated into single one,“How to convert between python objects and CLR objects dynamically?”
Based on my code before, I just wrote a new PyConverter here. I can make a PR if this design is accepted.
@yagweb when you mention conversion happens "dynamically", do you mean the conversion happens at runtime or even later when the corresponding value/field is accessed?
@yagweb i have not tested your converter API yet, but this looks promising. regarding preparing a PR, there is too much activity currently. i would wait until we stabilize .net core support in few weeks.
When working with a typing.Dict[str, str] and Dictionary<string, string> I still get
TypeError: 'dict' value cannot be converted to System.Collections.Generic.Dictionary`2[System.String,System.String]
on Python.NET 3.0.3
Looking at the source code it seems there is a PyDict wrapper, but it is not used in any of the Converter logic. Are there any plans to add built-in conversion support for dictionaries?
If it is supposed to work, an example show-casing the correct way to assign a python dictionary to an managed property accepting Dictionary<TKey, TValue> or IDictionary<TKey, TValue> or IReadOnlyDictionary<TKey, TValue> would be highly appreciated.
If only Python.NET was more "user-friendly"... don't get me wrong, it is doing an awesome job with most things, there are just some rough edges and unfortunately I already spent countless hours figuring out these things without much success...
Thanks!
Reacted by Frederic Linn, heartacker, Wojciech Wentland, Trevor Herr, Guillaume DEMICHELI, Trygve Wastvedt, Nate Woolls, Jared Blank, Felix Uhl and knbr
@iFreilicht it is fixed one way where you can pass Dictionary<K,V> where Python expects dict. I am unsure, but it might be fixed for the scenario where you pass Python dict to IDictionary<K,V>. But it won't be fixed for dict -> Dictionary<K, V> because it might have surprising behavior: in both C# and Python you can modify a dictionary passed that way, but the above mentioned conversion would not modify the original dict.
Activity
yagweb commentedon Feb 11, 2018
This function is similar to the function I proposed here #484. These two functions can be integrated into single one,“How to convert between python objects and CLR objects dynamically?”
Based on my code before, I just wrote a new PyConverter here. I can make a PR if this design is accepted.
It can be used in these ways,
For this issue from stackoverflow, we can use the PyConverter in this way,
First define a PyConverter according to needs,
Then, we can use this converter to convert a Clr Dictionary into a Python Dictionary, and vice versa.
den-run-ai commentedon Feb 12, 2018
@yagweb when you mention conversion happens "dynamically", do you mean the conversion happens at runtime or even later when the corresponding value/field is accessed?
yagweb commentedon Feb 12, 2018
@denfromufa "dynamically" here means the conversion happens at runtime.
It‘s opposite to "static": we know the type of the value/field in coding phase, so we directly use the PyInt etc. in the code.
There are three drawbacks of this hard coding method,
The work steps of PyConverter are,
den-run-ai commentedon Feb 13, 2018
@yagweb i have not tested your converter API yet, but this looks promising. regarding preparing a PR, there is too much activity currently. i would wait until we stabilize .net core support in few weeks.
kostjaaa commentedon Nov 6, 2018
Have you tested this yet? It looks quite promising and I would like to use it.
Would this be licensed under any terms?
den-run-ai commentedon Dec 19, 2018
@kostjaaa feel free to submit a PR, but make a summary of what you would like to achieve first.
sschukat commentedon Dec 21, 2018
I had a similar problem but solved it using Python code, which works with the current release
See the netdict example.
lostmsu commentedon Sep 23, 2021
This should now be fixed in 3.0
siegfriedpammer commentedon Feb 16, 2024
When working with a
typing.Dict[str, str]andDictionary<string, string>I still geton Python.NET 3.0.3
Looking at the source code it seems there is a PyDict wrapper, but it is not used in any of the Converter logic. Are there any plans to add built-in conversion support for dictionaries?
If it is supposed to work, an example show-casing the correct way to assign a python dictionary to an managed property accepting Dictionary<TKey, TValue> or IDictionary<TKey, TValue> or IReadOnlyDictionary<TKey, TValue> would be highly appreciated.
If only Python.NET was more "user-friendly"... don't get me wrong, it is doing an awesome job with most things, there are just some rough edges and unfortunately I already spent countless hours figuring out these things without much success...
Thanks!
theGEBIRGE commentedon Feb 26, 2024
I would also be interested in an example.
nwoolls commentedon Oct 21, 2024
Any details on how this was fixed in 3.0? How are we meant to take a dictionary from python and convert it into a C#
Dictionary<T,T>?iFreilicht commentedon May 28, 2025
@lostmsu please reopen this issue, it is not fixed in Python.Net 3.0.5, the error message by @siegfriedpammer is still accurate.
lostmsu commentedon May 28, 2025
@iFreilicht it is fixed one way where you can pass
Dictionary<K,V>where Python expectsdict. I am unsure, but it might be fixed for the scenario where you pass PythondicttoIDictionary<K,V>. But it won't be fixed fordict->Dictionary<K, V>because it might have surprising behavior: in both C# and Python you can modify a dictionary passed that way, but the above mentioned conversion would not modify the originaldict.