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

45 views Asked by At

Page Source example:

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:

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]

enter image description here

enter image description here

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":

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?

1

There are 1 answers

0
JialeDu On

Add the following setting to settings.json:

    "python.analysis.diagnosticSeverityOverrides": {
        "reportAttributeAccessIssue": "none",
        "reportOptionalMemberAccess": "none"
    },

This is only for those who do not want to modify the code and just block the error.