Skip to content

all BeautifulSoup type hints are wrong #8356

Closed
@simon-liebehenschel

Description

@simon-liebehenschel

Summary

It is a total chaos. Where I have BeautifulSoup, I get Iterable[PageElement]. Where I have Tag, I get PageElement, etc. See the example below.

I have errors from Mypy in every line:
Item "NavigableString" of "Union[Tag, NavigableString]" has no attribute "find_all"
"Iterable[PageElement]" has no attribute "__next__" [attr-defined]
"enumerate" has incompatible type "BeautifulSoup"; expected "Iterable[<nothing>]"
Value of type "Union[Tag, NavigableString, None]" is not indexable
etc.

The only workaround is either #type: ignore dozens of lines, or uninstall "types-beautifulsoup4".

Reproducible code sample

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'lxml')
reveal_type(soup)
for el in soup:
    reveal_type(el)

Output

_local.py:16:13: note: Revealed type is "bs4.BeautifulSoup"
_local.py:17:11: error: "Iterable[PageElement]" has no attribute "__next__"  [attr-defined]
    for el in soup:
              ^
_local.py:18:17: note: Revealed type is "Any"
Found 1 error in 1 file (checked 1 source file)

Expected output

No errors.

Versions

beautifulsoup4 = "4.11.1"
types-beautifulsoup4 = "4.11.1"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions