Skip to content

Commit 75bbd6a

Browse files
schloerkecpsievert
andauthored
Apply suggestions from code review; Remove unnecessary str calls
Co-authored-by: Carson Sievert <[email protected]>
1 parent 016bc39 commit 75bbd6a

File tree

2 files changed

+2
-12
lines changed

2 files changed

+2
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
* `HTML` no longer inherits from `str`. It now inherits from `collections.UserString`. This was done to avoid confusion between `str` and `HTML` objects. (#86)
1313

14-
* `Tag` attributes no longer silently support `HTML` as values. (#86)
14+
* `Tag` attributes now error when given an `HTML()` object. To fix, use `str()` to get a string (e.g., `div(foo=str(HTML("bar")))`. (#86)
1515

1616
* `Tag` and `TagList`'s method `.get_html_string()` now both return `str` instead of `HTML`. (#86)
1717

htmltools/_core.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,6 @@ def update( # type: ignore[reportIncompatibleMethodOverride] # TODO-future: fix
438438
continue
439439
nm = self._normalize_attr_name(k)
440440

441-
# Preserve the HTML() when combining two HTML() attributes
442-
# Escape any non-HTML values before combining
443441
if nm in attrz:
444442
val = attrz[nm] + " " + val
445443

@@ -662,9 +660,6 @@ def remove_class(self: TagT, class_: str) -> TagT:
662660

663661
# Remove the class value from the ordered set of class values
664662
# Note: .split() splits on any whitespace and removes empty strings
665-
cls_is_html = isinstance(cls, HTML)
666-
if cls_is_html:
667-
cls = cls.as_string()
668663
new_classes = [cls_val for cls_val in cls.split() if cls_val != class_]
669664
if len(new_classes) > 0:
670665
# Store the new class value
@@ -690,7 +685,7 @@ def has_class(self, class_: str) -> bool:
690685
"""
691686
cls = self.attrs.get("class")
692687
if cls:
693-
return class_ in str(cls).split()
688+
return class_ in cls.split()
694689
else:
695690
return False
696691

@@ -1261,8 +1256,6 @@ class HTML(UserString):
12611256
"""
12621257

12631258
def __init__(self, html: object) -> None:
1264-
if isinstance(html, HTML):
1265-
html = html.as_string()
12661259
super().__init__(str(html))
12671260

12681261
def __str__(self) -> str:
@@ -1299,9 +1292,6 @@ def __radd__(self, other: object) -> HTML:
12991292
# Case: `str + HTML()`
13001293
return HTML(html_escape(str(other)) + self.as_string())
13011294

1302-
def __eq__(self, x: object) -> bool:
1303-
# Set `x` first so that it can dispatch to the other object's __eq__ method as we've upgraded to `str`
1304-
return x == self.as_string()
13051295

13061296
def __repr__(self) -> str:
13071297
return self.as_string()

0 commit comments

Comments
 (0)