@@ -271,18 +271,19 @@ class TagList(UserList[TagNode]):
271271 <div id="foo" class="bar"></div>
272272 """
273273
274+ def _should_not_expand (self , x : object ) -> TypeIs [str ]:
275+ """
276+ Check if an object should not be expanded into a list of children.
277+ """
278+ return isinstance (x , str )
279+
274280 def __init__ (self , * args : TagChild ) -> None :
275281 super ().__init__ (_tagchilds_to_tagnodes (args ))
276282
277283 def extend (self , other : Iterable [TagChild ]) -> None :
278284 """
279285 Extend the children by appending an iterable of children.
280286 """
281-
282- # If other is a string (an iterable), convert it to a list of s.
283- if isinstance (other , str ):
284- other = [other ]
285-
286287 super ().extend (_tagchilds_to_tagnodes (other ))
287288
288289 def append (self , item : TagChild , * args : TagChild ) -> None :
@@ -304,8 +305,7 @@ def __add__(self, item: Iterable[TagChild]) -> TagList:
304305 Return a new TagList with the item added at the end.
305306 """
306307
307- should_not_expand = isinstance (item , str )
308- if should_not_expand :
308+ if self ._should_not_expand (item ):
309309 return TagList (self , item )
310310
311311 return TagList (self , * item )
@@ -315,8 +315,7 @@ def __radd__(self, item: Iterable[TagChild]) -> TagList:
315315 Return a new TagList with the item added to the beginning.
316316 """
317317
318- should_not_expand = isinstance (item , str )
319- if should_not_expand :
318+ if self ._should_not_expand (item ):
320319 return TagList (item , self )
321320
322321 return TagList (* item , self )
@@ -1926,6 +1925,9 @@ def consolidate_attrs(
19261925# Convert a list of TagChild objects to a list of TagNode objects. Does not alter input
19271926# object.
19281927def _tagchilds_to_tagnodes (x : Iterable [TagChild ]) -> list [TagNode ]:
1928+ if isinstance (x , str ):
1929+ return [x ]
1930+
19291931 result = flatten (x )
19301932 for i , item in enumerate (result ):
19311933 if isinstance (item , (int , float )):
0 commit comments