MCPcopy
hub / github.com/danielgtaylor/python-betterproto / from_pydict

Method from_pydict

src/betterproto/__init__.py:1774–1823  ·  view source on GitHub ↗

Parse the key/value pairs into the current message instance. This returns the instance itself and is therefore assignable and chainable. Parameters ----------- value: Dict[:class:`str`, Any] The dictionary to parse from. Returns

(self: T, value: Mapping[str, Any])

Source from the content-addressed store, hash-verified

1772 return output
1773
1774 def from_pydict(self: T, value: Mapping[str, Any]) -> T:
1775 """
1776 Parse the key/value pairs into the current message instance. This returns the
1777 instance itself and is therefore assignable and chainable.
1778
1779 Parameters
1780 -----------
1781 value: Dict[:class:`str`, Any]
1782 The dictionary to parse from.
1783
1784 Returns
1785 --------
1786 :class:`Message`
1787 The initialized message.
1788 """
1789 self._serialized_on_wire = True
1790 for key in value:
1791 field_name = safe_snake_case(key)
1792 meta = self._betterproto.meta_by_field_name.get(field_name)
1793 if not meta:
1794 continue
1795
1796 if value[key] is not None:
1797 if meta.proto_type == TYPE_MESSAGE:
1798 v = getattr(self, field_name)
1799 if isinstance(v, list):
1800 cls = self._betterproto.cls_by_field[field_name]
1801 for item in value[key]:
1802 v.append(cls().from_pydict(item))
1803 elif isinstance(v, datetime):
1804 v = value[key]
1805 elif isinstance(v, timedelta):
1806 v = value[key]
1807 elif meta.wraps:
1808 v = value[key]
1809 else:
1810 # NOTE: `from_pydict` mutates the underlying message, so no
1811 # assignment here is necessary.
1812 v.from_pydict(value[key])
1813 elif meta.map_types and meta.map_types[1] == TYPE_MESSAGE:
1814 v = getattr(self, field_name)
1815 cls = self._betterproto.cls_by_field[f"{field_name}.value"]
1816 for k in value[key]:
1817 v[k] = cls().from_pydict(value[key][k])
1818 else:
1819 v = value[key]
1820
1821 if v is not None:
1822 setattr(self, field_name, v)
1823 return self
1824
1825 def is_set(self, name: str) -> bool:
1826 """

Callers 3

test_enum_as_int_jsonFunction · 0.80

Calls 2

safe_snake_caseFunction · 0.85
getMethod · 0.80

Tested by 3

test_enum_as_int_jsonFunction · 0.64