leforestier / yattag

Python library to generate HTML or XML in a readable, concise and pythonic way.
333 stars 31 forks source link

Allow `SimpleDoc.text()` to accept objects of any type #83

Closed abingham closed 3 months ago

abingham commented 3 months ago

SimpleDoc.text() currently only accepts objects of type str, int, and float. So if I have objects of other types that have __str__ representations I want to use, I have to stringify them before passing them into text(). This is a bit error prone (i.e. I forget to do it and get an exception), it clutters my code a bit, and I don't really see what's gained by not internally stringifying the arguments to text().

So could we update SimpleDoc.text() to accept objects of any sort, having it call str() on them before calling html_escape()? Or is there an important reason that we limit the API to str, float, and int?

leforestier commented 3 months ago

This has already been discussed here https://github.com/leforestier/yattag/pull/21 and I decided to limit the automatic stringification to numerical values. To sum it up, this is to catch programming mistakes early, and to help people avoid leaking potentially sensitive data on the internet by mistake (by accidentally printing a full dictionary instead of one of its entries for example).

abingham commented 3 months ago

Ok, that's fair enough, and I can probably subclass Doc to get what I'm talking about if I really want.