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
| 215 | |
| 216 | |
| 217 | class 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 | |
| 264 | class _BaseChildElement: |
no outgoing calls
no test coverage detected
searching dependent graphs…