votes up 1

__init__() got an unexpected keyword argument '%s'

Package:
pip
github stars 7379
Exception Class:
TypeError

Raise code

""" 
        :arg alphabetical_attributes: Reorder attributes to be in alphabetical order.

            Defaults to ``False``.

        """
        unexpected_args = frozenset(kwargs) - frozenset(self.options)
        if len(unexpected_args) > 0:
            raise TypeError("__init__() got an unexpected keyword argument '%s'" % next(iter(unexpected_args)))
        if 'quote_char' in kwargs:
            self.use_best_quote_char = False
        for attr in self.options:
            setattr(self, attr, kwargs.get(attr, getattr(self, attr)))
        self.errors = []
        self.strict = False
😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 8th August 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 1 votes down

This happens when html5lib.serializer.serialize gets unexpected argument.

Code to reproduce the exception:

from html5lib.html5parser import parse
from html5lib.serializer import serialize
token_stream = parse('<html><body><p>Hi!</p></body></html>')
serialize(token_stream, omit_optional_tag=False)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-0a6c1b2a6780> in <module>()  2 from html5lib.serializer import serialize  3 token_stream = parse('<html><body><p>Hi!</p></body></html>') ----> 4 serialize(token_stream, omit_optional_tag=False) 
/usr/local/lib/python3.7/dist-packages/html5lib/serializer.py in serialize(input, tree, encoding, **serializer_opts)  98 # XXX: Should we cache this?  99 walker = treewalkers.getTreeWalker(tree) --> 100 s = HTMLSerializer(**serializer_opts)  101 return s.render(walker(input), encoding)  102  
/usr/local/lib/python3.7/dist-packages/html5lib/serializer.py in __init__(self, **kwargs)  214 unexpected_args = frozenset(kwargs) - frozenset(self.options)  215 if len(unexpected_args) > 0: --> 216 raise TypeError("__init__() got an unexpected keyword argument '%s'" % next(iter(unexpected_args)))  217 if 'quote_char' in kwargs:  218 self.use_best_quote_char = False 
TypeError: __init__() got an unexpected keyword argument 'omit_optional_tag'

Fixed version of the code:

from html5lib.html5parser import parse
from html5lib.serializer import serialize
token_stream = parse('<html><body><p>Hi!</p></body></html>')
serialize(token_stream, omit_optional_tags=False)

<html><head></head><body><p>Hi!</p></body></html>

May 07, 2022 kellemnegasi answer
kellemnegasi 30.6k

Add a possible fix

Please authorize to post fix