Skip to content

Internal error when identifier in except clause shadows imported module name #9615

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

Closed
ethframe opened this issue Oct 19, 2020 · 1 comment · Fixed by #10560
Closed

Internal error when identifier in except clause shadows imported module name #9615

ethframe opened this issue Oct 19, 2020 · 1 comment · Fixed by #10560

Comments

@ethframe
Copy link
Contributor

ethframe commented Oct 19, 2020

Crash Report

Mypy crashes while analysing a try statement in which one of the except clauses uses the same identifier as one of the imported modules.

Traceback

bug.py:3: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.790
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "mypy\checker.py", line 401, in accept
  File "mypy\nodes.py", line 1235, in accept
  File "mypy\checker.py", line 3350, in visit_try_stmt
  File "mypy\checker.py", line 3403, in visit_try_without_finally
  File "mypy\checker.py", line 2053, in check_assignment
  File "mypy\checker.py", line 2799, in check_lvalue
TypeError: mypy.nodes.Var object expected; got mypy.nodes.MypyFile
bug.py:3: : note: use --pdb to drop into pdb

To Reproduce

bug.py contents:

import re

try:
    pass
except Exception as re:
    pass

Command: mypy bug.py

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.6.8
  • Operating system and version: Windows 10 2004
@andrewgodwin
Copy link

It's worth noting that a very similar problem occurs when the identifier in the except clause is declared global, but that identifier is not declared in the outside scope:

# Note that e is not defined out here
def main()
    global e
    try:
        ...
    except Exception as e:
        pass

This results in TypeError: mypy.nodes.Var object expected; got None

hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue May 30, 2021
Fixes python#9615, fixes python#9682

Both issue reports hit the faulty cast on L2803. This changes the logic
so that that cast is actually always true. If not, we just end up doing
whatever the fallback else clause does; this resulted in behaviour that
matched what I expected.

After that, the python#9682 hit another crash in checker.py, where var.node
was None instead of a Var. It seemed reasonable to just branch instead.
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue May 30, 2021
Fixes python#9615, fixes python#9682

Both issue reports hit the faulty cast on L2803. This changes the logic
so that that cast is actually always true. If not, we just end up doing
whatever the fallback else clause does; this resulted in behaviour that
matched what I expected.

After that, the python#9682 hit another crash in checker.py, where var.node
was None instead of a Var. It seemed reasonable to just branch instead.
JukkaL pushed a commit that referenced this issue Jun 18, 2021
Fixes #9615, fixes #9682

Both issue reports hit the faulty cast on L2803. This changes the logic
so that that cast is actually always true. If not, we just end up doing
whatever the fallback else clause does; this resulted in behaviour that
matched what I expected.

After that, the #9682 hit another crash in checker.py, where var.node
was None instead of a Var. It seemed reasonable to just branch instead.

Co-authored-by: hauntsaninja <>
JukkaL pushed a commit that referenced this issue Jun 21, 2021
Fixes #9615, fixes #9682

Both issue reports hit the faulty cast on L2803. This changes the logic
so that that cast is actually always true. If not, we just end up doing
whatever the fallback else clause does; this resulted in behaviour that
matched what I expected.

After that, the #9682 hit another crash in checker.py, where var.node
was None instead of a Var. It seemed reasonable to just branch instead.

Co-authored-by: hauntsaninja <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants