File tree Expand file tree Collapse file tree 2 files changed +29
-9
lines changed Expand file tree Collapse file tree 2 files changed +29
-9
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,17 @@ def __call__(
3939
4040 return self .__class__ (* new_children , ** new_attributes )
4141
42+ def __iter__ (self ) -> None :
43+ """
44+ Override the __iter__ method to give a slightly nicer error message
45+ when using PyHTML with flask
46+ """
47+ raise TypeError (
48+ f"'{ self ._get_tag_name ()} ' object is not iterable.\n "
49+ f"**HINT:** if you're using Flask, try converting your PyHTML "
50+ f"into a string using the `str` function before returning it.\n "
51+ )
52+
4253 def _get_tag_name (self ) -> str :
4354 """
4455 Returns the name of the tag
Original file line number Diff line number Diff line change 1313@app .get ("/" )
1414def simple_route ():
1515 return str (p .html (
16- p .body ("Hello, world!" )
16+ p .body (
17+ p .p ("This app is to test Flask's integration with PyHTML." ),
18+ p .a (href = "/no_str" )("Click here to get a 500 error." ),
19+ )
1720 ))
1821
1922
@@ -49,11 +52,16 @@ def test_simple_stringify(client: FlaskClient):
4952 assert response .status_code == 200
5053
5154 assert response .text == '\n ' .join ([
52- "<html>" ,
53- " <body>" ,
54- " Hello, world!" ,
55- " </body>" ,
56- "</html>" ,
55+ '<html>' ,
56+ ' <body>' ,
57+ ' <p>' ,
58+ ' This app is to test Flask's integration with PyHTML.' ,
59+ ' </p>' ,
60+ ' <a href="/no_str">' ,
61+ ' Click here to get a 500 error.' ,
62+ ' </a>' ,
63+ ' </body>' ,
64+ '</html>' ,
5765 ])
5866
5967
@@ -65,7 +73,8 @@ def test_failed_to_stringify(client: FlaskClient):
6573 with pytest .raises (TypeError ) as exception_info :
6674 client .get ("/no_str" )
6775
68- expected_error = " HINT: try converting your PyHTML into a string using " \
69- "the `str` function"
76+ assert "** HINT:** if you're using Flask" in str ( exception_info . value )
77+
7078
71- assert expected_error in str (exception_info .value )
79+ if __name__ == '__main__' :
80+ app .run (debug = True )
You can’t perform that action at this time.
0 commit comments