MCPcopy Index your code
hub / github.com/python-openxml/python-docx / RequiredAttribute

Class RequiredAttribute

src/docx/oxml/xmlchemy.py:217–261  ·  view source on GitHub ↗

Defines a required attribute on a custom element class. A required attribute is assumed to be present for reading, so does not have a default value; its actual value is always used. If missing on read, an |InvalidXmlError| is raised. It also does not remove the attribute if |None| is

Source from the content-addressed store, hash-verified

215
216
217class RequiredAttribute(BaseAttribute):
218 """Defines a required attribute on a custom element class.
219
220 A required attribute is assumed to be present for reading, so does not have a
221 default value; its actual value is always used. If missing on read, an
222 |InvalidXmlError| is raised. It also does not remove the attribute if |None| is
223 assigned. Assigning |None| raises |TypeError| or |ValueError|, depending on the
224 simple type of the attribute.
225 """
226
227 @property
228 def _docstring(self):
229 """Return the string to use as the ``__doc__`` attribute of the property for
230 this attribute."""
231 return "%s type-converted value of ``%s`` attribute." % (
232 self._simple_type.__name__,
233 self._attr_name,
234 )
235
236 @property
237 def _getter(self) -> Callable[[BaseOxmlElement], Any]:
238 """function object suitable for "get" side of attr property descriptor."""
239
240 def get_attr_value(obj: BaseOxmlElement) -> Any | None:
241 attr_str_value = obj.get(self._clark_name)
242 if attr_str_value is None:
243 raise InvalidXmlError(
244 "required '%s' attribute not present on element %s" % (self._attr_name, obj.tag)
245 )
246 return self._simple_type.from_xml(attr_str_value)
247
248 get_attr_value.__doc__ = self._docstring
249 return get_attr_value
250
251 @property
252 def _setter(self) -> Callable[[BaseOxmlElement, Any], None]:
253 """function object suitable for "set" side of attribute property descriptor."""
254
255 def set_attr_value(obj: BaseOxmlElement, value: Any):
256 str_value = self._simple_type.to_xml(value)
257 if str_value is None:
258 raise ValueError(f"cannot assign {value} to this required attribute")
259 obj.set(self._clark_name, str_value)
260
261 return set_attr_value
262
263
264class _BaseChildElement:

Callers 15

CT_CommentClass · 0.90
CT_DecimalNumberClass · 0.90
CT_StringClass · 0.90
CT_TblWidthClass · 0.90
CT_VerticalJcClass · 0.90
CT_LsdExceptionClass · 0.90
CT_HdrFtrRefClass · 0.90
CT_Point2DClass · 0.90
CT_PositiveSize2DClass · 0.90
CT_NumClass · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…