Skip to content

beautifulsoup: iterating over bs4.BeautifulSoup returns Tag, but Mypy thinks that bs4.element.PageElement type is returned #8369

Open
@simon-liebehenschel

Description

@simon-liebehenschel

The code is correct, but fails with Mypy:

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, SoupStrainer

soup = BeautifulSoup(html_doc, 'lxml', parse_only=SoupStrainer("a"))
reveal_type(soup)
for el in soup:
    print(type(el))
    reveal_type(el)
    print(el.get("href"))

Mypy output

_local.py:17:13: note: Revealed type is "bs4.BeautifulSoup"
_local.py:19:17: note: Revealed type is "bs4.element.PageElement"
_local.py:20:11: error: "PageElement" has no attribute "get"  [attr-defined]
        print(el.get("href"))
              ^
Found 1 error in 1 file (checked 1 source file)

Python output if you run the code

<class 'bs4.element.Tag'>
http://example.com/elsie
<class 'bs4.element.Tag'>
http://example.com/lacie
<class 'bs4.element.Tag'>
http://example.com/tillie

Expected Mypy output

soup = BeautifulSoup(...
for item in soup:
  reveal_type(item)  #  <-- type must be `Tag`

Versions

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions