How working correctly with Beautifulsoup to not generate Type Checking alerts in VSCode?

36 views
Skip to first unread message

Frattos Dj (Dj Frattos)

unread,
Mar 12, 2024, 11:21:49 AMMar 12
to beautifulsoup
Page Source example:  
```lang-python
from bs4 import BeautifulSoup, Tag, ResultSet
from re import compile

page_source = """
<html>
<body>
    <div class="block_general_statistics">
    <table>
        <tbody>
        <tr>
            <th>Header 1</th>
            <td class="total">Data 1</td>
        </tr>
        </tbody>
    </table>
    </div>
</body>
</html>
"""
```
Original use to reduce the number of lines and characters but which generate Type Checking alerts and also note that `find | text | strip` inside the list comprehension all of these the font color is white due to lack of necessary combination:  
```lang-pyton
soup = BeautifulSoup(page_source, 'html.parser')
table_stats = soup.find('div', class_=compile('block_general_statistics')).find('table')
table_stats_body = table_stats.find('tbody').find_all('tr')
thead = [th.find('th').text.strip() for th in table_stats_body]
tbody = [th.find('td', class_='total').text.strip() for th in table_stats_body]
```

cKpgY.png

7TqfR.png


The only way that with my basic knowledge I was able to resolve all the alerts and also fix all fonts colored correctly without any being white due to "lack of function":  
```lang-python
soup = BeautifulSoup(page_source, 'html.parser')
table_stats = soup.find('div', class_=compile('block_general_statistics'))
if type(table_stats) == Tag:
    table_stats = table_stats.find('table')
    if type(table_stats) == Tag:
        table_stats_body = table_stats.find('tbody')
        if type(table_stats_body) == Tag:
            table_stats_body = table_stats_body.find_all('tr')
            if type(table_stats_body) == ResultSet:
                thead = []
                for th in table_stats_body:
                    if type(th) == Tag:
                        th = th.find('th')
                        if type(th) == Tag:
                            thead.append(th.text.strip())
                tbody = []
                for th in table_stats_body:
                    if type(th) == Tag:
                        th = th.find('td', class_='total')
                        if type(th) == Tag:
                            tbody.append(th.text.strip())
```
Is there any smarter way that can resolve the alerts but that doesn't make a simple, short code become something so large, detailed and even difficult to make changes in the future?

StackOverflow:
https://stackoverflow.com/questions/78147561/how-working-correctly-with-beautifulsoup-to-not-generate-type-checking-alerts-in

Chris Papademetrious

unread,
Mar 17, 2024, 2:40:07 PMMar 17
to beautifulsoup
Try adding a type annotation to let VSCode know that table_stats_body contains Tag objects:

table_stats_body: list[Tag] = table_stats.find('tbody').find_all('tr')

 - Chris
Reply all
Reply to author
Forward
0 new messages