cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-95813: Improve HTMLParser from the view of inheritance

Open corona10 opened this issue 3 years ago • 4 comments

  • Issue: gh-95813

corona10 avatar Aug 11 '22 05:08 corona10

@corona10, thanks for the PR! I think we should add a test that fails before your changes and passes afterwards.

ezio-melotti avatar Aug 11 '22 05:08 ezio-melotti

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

corona10 avatar Aug 11 '22 05:08 corona10

@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?

corona10 avatar Aug 11 '22 10:08 corona10

@ezio-melotti gentle ping

corona10 avatar Aug 16 '22 15:08 corona10

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

bedevere-bot avatar Aug 17 '22 02:08 bedevere-bot

Thanks for the PR!

One more thing, what about update ParserBase as abstract class?

We could, but I don't think it's necessary.

ezio-melotti avatar Aug 18 '22 11:08 ezio-melotti