async attribute in python 3.7+

Bug #1878740 reported by Alexei Martchenko
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Beautiful Soup
Won't Fix
Undecided
Unassigned

Bug Description

Async is a reserved word in Python 3.7. Thus, it cannot be used as argument.

This code works perfectly in Python 2.7 (and 3.2 to 3.6 I believe) but not in 3.7.6

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<p>test</p>')
>>> new_tag = soup.new_tag('script', async=None, src="https://platform.twitter.com/widgets.js",charset="utf-8")
  File "<stdin>", line 1
    new_tag = soup.new_tag('script', async=None, src="https://platform.twitter.com/widgets.js",charset="utf-8")
                                         ^
SyntaxError: invalid syntax

Should BS4 use _async like _class?

Revision history for this message
Leonard Richardson (leonardr) wrote :

Thanks for taking the time to file this issue.

'class' gets special treatment in find() and similar methods because CSS class is the single most common way people look for a tag. It doesn't get special treatment in new_tag(), and 'async' is much less common, so my recommendation is to use the 'attrs' argument instead of Python keyword arguments:

new_tag = soup.new_tag('script', attrs={'async': None}, src="https://platform.twitter.com/widgets.js", charset="utf-8")

In revision 574 I improved the new_tag() docstring to mention this possibility.

Changed in beautifulsoup:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.