gh-95813: Improve HTMLParser from the view of inheritance
- Issue: gh-95813
@corona10, thanks for the PR! I think we should add a test that fails before your changes and passes afterwards.
I think we should add a test that fails before your changes and passes afterwards.
Updated!
Without patch:
0:00:00 load avg: 4.23 Run tests sequentially
0:00:00 load avg: 4.23 [1/1] test_htmlparser
test test_htmlparser failed -- Traceback (most recent call last):
File "/Users/user/oss/cpython/Lib/unittest/mock.py", line 1359, in patched
return func(*newargs, **newkeywargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/oss/cpython/Lib/test/test_htmlparser.py", line 800, in test_init
self.assertTrue(super_init_method.called)
AssertionError: False is not true
test_htmlparser failed (1 failure)
== Tests result: FAILURE ==
1 test failed:
test_htmlparser
@ezio-melotti
diff --git a/Lib/_markupbase.py b/Lib/_markupbase.py
index 3ad7e27996..f8b2b901b9 100644
--- a/Lib/_markupbase.py
+++ b/Lib/_markupbase.py
@@ -7,6 +7,7 @@
import re
+from abc import ABCMeta, abstractmethod
_declname_match = re.compile(r'[a-zA-Z][-_.a-zA-Z0-9]*\s*').match
_declstringlit_match = re.compile(r'(\'[^\']*\'|"[^"]*")\s*').match
_commentclose = re.compile(r'--\s*>')
@@ -20,7 +21,7 @@
del re
-class ParserBase:
+class ParserBase(metaclass=ABCMeta):
"""Parser base class which provides some common support methods used
by the SGML/HTML and XHTML parsers."""
@@ -391,6 +392,6 @@ def _scan_name(self, i, declstartpos):
"expected name token at %r" % rawdata[declstartpos:declstartpos+20]
)
- # To be overridden -- handlers for unknown objects
+ @abstractmethod
def unknown_decl(self, data):
pass
One more thing, what about update ParserBase as abstract class?
@ezio-melotti gentle ping
When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.
Thanks for the PR!
One more thing, what about update ParserBase as abstract class?
We could, but I don't think it's necessary.