tostring() ignores tag attribute
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Won't Fix
|
Undecided
|
Unassigned |
Bug Description
Python : sys.version_
lxml.etree : (4, 2, 3, 0)
libxml used : (2, 9, 8)
libxml compiled : (2, 9, 8)
libxslt used : (1, 1, 32)
libxslt compiled : (1, 1, 32)
Test script:
from lxml import etree
class Component(
pass
class Div(Component):
tag = 'div'
class Col(Div):
def __init__(self, l=None):
self.l = l
def _init(self):
if 'class' not in self.attrib:
else:
if self.l:
el = Col(l=2)
el.tag = 'div'
print('el repr', el)
print('expected: ', b'<div class="col l2"/>')
print('result: ', etree.tostring(el))
Result:
el repr <Element div at 0x7fe6923ada48>
expected: b'<div class="col l2"/>'
result: b'<Col class="col l2"/>'
As you can see, tostring enforces the class name instead of using the tag attribute, like repr does.
Element classes are not relevant for serialisation. As documented, all state must be stored in the tree, not in the element classes. If you set "self.tag" from your class, you will get the expected result.