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 as
| 229 | |
| 230 | |
| 231 | class RequiredAttribute(BaseAttribute): |
| 232 | """Defines a required attribute on a custom element class. |
| 233 | |
| 234 | A required attribute is assumed to be present for reading, so does not have a default value; |
| 235 | its actual value is always used. If missing on read, an |InvalidXmlError| is raised. It also |
| 236 | does not remove the attribute if |None| is assigned. Assigning |None| raises |TypeError| or |
| 237 | |ValueError|, depending on the simple type of the attribute. |
| 238 | """ |
| 239 | |
| 240 | @property |
| 241 | def _getter(self) -> Callable[[BaseOxmlElement], Any]: |
| 242 | """Callable suitable for the "get" side of the attribute property descriptor.""" |
| 243 | |
| 244 | def get_attr_value(obj: BaseOxmlElement) -> Any: |
| 245 | attr_str_value = obj.get(self._clark_name) |
| 246 | if attr_str_value is None: |
| 247 | raise InvalidXmlError( |
| 248 | "required '%s' attribute not present on element %s" % (self._attr_name, obj.tag) |
| 249 | ) |
| 250 | return self._simple_type.from_xml(attr_str_value) |
| 251 | |
| 252 | get_attr_value.__doc__ = self._docstring |
| 253 | return get_attr_value |
| 254 | |
| 255 | @property |
| 256 | def _docstring(self): |
| 257 | """ |
| 258 | Return the string to use as the ``__doc__`` attribute of the property |
| 259 | for this attribute. |
| 260 | """ |
| 261 | return "%s type-converted value of ``%s`` attribute." % ( |
| 262 | self._simple_type.__name__, |
| 263 | self._attr_name, |
| 264 | ) |
| 265 | |
| 266 | @property |
| 267 | def _setter(self) -> Callable[[BaseOxmlElement, Any], None]: |
| 268 | """Callable suitable for the "set" side of the attribute property descriptor.""" |
| 269 | |
| 270 | def set_attr_value(obj: BaseOxmlElement, value: Any) -> None: |
| 271 | str_value = self._simple_type.to_xml(value) |
| 272 | obj.set(self._clark_name, str_value) |
| 273 | |
| 274 | return set_attr_value |
| 275 | |
| 276 | |
| 277 | class _BaseChildElement: |
no outgoing calls
no test coverage detected
searching dependent graphs…