Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash with BeautifulSoup #14548

Closed
cap-jmk opened this issue Jan 28, 2023 · 3 comments · Fixed by #14552
Closed

Crash with BeautifulSoup #14548

cap-jmk opened this issue Jan 28, 2023 · 3 comments · Fixed by #14552
Labels

Comments

@cap-jmk
Copy link

cap-jmk commented Jan 28, 2023

The bug produces as internal error when accessing the dict returned from BeautifulSoup.find_all() method:
on: 0.991


I would rather expect an mypy error instead on an internal error. 
**To Reproduce**

```python
repos = []
    for url in links:
        soup = BeautifulSoup(urlopen(url), "html.parser")
        for link in soup.find_all("a", href=True):
            repos.append(repos.append(link["href"])

Expected Behavior

I would expect some sort of mypy error.

Actual Behavior

error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues

Yet, when using the following

mypy --strict package_name/ --show-traceback

The result is

error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.991
Traceback (most recent call last):
  File "mypy/checkexpr.py", line 4657, in accept
  File "mypy/checkexpr.py", line 410, in visit_call_expr
  File "mypy/checkexpr.py", line 473, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 885, in try_infer_partial_type
KeyError: <mypy.nodes.Var object at 0x7f3843b28670>

Thus, I am reporting the bug.

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: --strict, --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): no special config
  • Python version used: 3.10
@cap-jmk cap-jmk added the bug mypy got something wrong label Jan 28, 2023
@AlexWaygood AlexWaygood added crash and removed bug mypy got something wrong labels Jan 28, 2023
@JelleZijlstra JelleZijlstra changed the title Internal error causes crash Crash with BeautifulSoup Jan 28, 2023
@JelleZijlstra
Copy link
Member

JelleZijlstra commented Jan 28, 2023

Crashes like this are definitely bugs that we should fix. Could you provide a complete, self-contained code sample that reproduces the error?

@cap-jmk
Copy link
Author

cap-jmk commented Jan 29, 2023

Yes, of course. Sorry I thought the first would suffice. My bad.

from typing import  List
from bs4 import BeautifulSoup
from urllib.request import urlopen

def get_repos(
    git_links:List[str]=[
        "www.google.com",
        "www.bing.com",
    ],
):

    repos = []
    print(git_links)
    for url in git_links:
        soup = BeautifulSoup(urlopen(url), "html.parser")
        for link in soup.find_all("a", href=True):
            repos.append(repos.append(link["href"]))


if name == "__main__": 
    get_repos()

Then if you run

mypy --strict file_name.py

It gives the error on Python3.10

@uriyyo
Copy link
Member

uriyyo commented Jan 29, 2023

@cap-jmk Main issue here in this line:

repos.append(repos.append(link["href"]))

You are trying to append the result of append call to list.

You just need to change your code to:

from typing import  List
from bs4 import BeautifulSoup
from urllib.request import urlopen

def get_repos(
    git_links:List[str]=[
        "www.google.com",
        "www.bing.com",
    ],
):

    repos = []
    print(git_links)
    for url in git_links:
        soup = BeautifulSoup(urlopen(url), "html.parser")
        for link in soup.find_all("a", href=True):
            repos.append(link["href"])


if __name__ == "__main__":
    get_repos()

JelleZijlstra pushed a commit that referenced this issue Jan 29, 2023
Fixes: #14548

Fixed case when untyped list item type resolving can lead to an internal
crash.

Code to reproduce this issue:
```py
arr = []
arr.append(arr.append(1))
```

Basically, the issue is that after the first resolving of `arr.append`
method, `var` is deleted from `partial_types`, and as war as
`arr.append` is a nested call we try to delete the same `var` that was
already deleted.
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue Jan 29, 2023
Fixes: python#14548

Fixed case when untyped list item type resolving can lead to an internal
crash.

Code to reproduce this issue:
```py
arr = []
arr.append(arr.append(1))
```

Basically, the issue is that after the first resolving of `arr.append`
method, `var` is deleted from `partial_types`, and as war as
`arr.append` is a nested call we try to delete the same `var` that was
already deleted.
hauntsaninja added a commit that referenced this issue Jan 29, 2023
#14552) (#14553)

Fixes: #14548

Fixed case when untyped list item type resolving can lead to an internal
crash.

Code to reproduce this issue:
```py
arr = []
arr.append(arr.append(1))
```

Basically, the issue is that after the first resolving of `arr.append`
method, `var` is deleted from `partial_types`, and as war as
`arr.append` is a nested call we try to delete the same `var` that was
already deleted.

Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants