Repository: python/mypy

Issue:
Internal error
I ran the latest mypy from master (commit 13b7573) and I got an internal error. I didn't get this error with mypy version 0.511.

File which caused the traceback: https://github.com/zulip/zulip/blob/master/api/integrations/perforce/git_p4.py
This was the only file I ran mypy on.

I didn't use any extra flags with mypy (except `--tb` to generate the traceback once I got the internal error).

Here's the traceback:
```
api/integrations/perforce/git_p4.py: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.520-dev-13b75737bb41d62ae4e7714704dcda3b446fa9f4
Traceback (most recent call last):
  File "/mnt/code/venvs/mypy_venv/bin/mypy", line 6, in <module>
    main(__file__)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/main.py", line 46, in main
    res = type_check_only(sources, bin_dir, options)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/main.py", line 93, in type_check_only
    options=options)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/build.py", line 188, in build
    graph = dispatch(sources, manager)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/build.py", line 1587, in dispatch
    graph = load_graph(sources, manager)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/build.py", line 1670, in load_graph
    root_source=True)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/build.py", line 1259, in __init__
    self.parse_file()
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/build.py", line 1442, in parse_file
    first.visit_file(self.tree, self.xpath, self.id, self.options)
  File "/usr/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/build.py", line 1347, in wrap_context
    yield
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/build.py", line 1442, in parse_file
    first.visit_file(self.tree, self.xpath, self.id, self.options)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/semanal.py", line 3423, in visit_file
    d.accept(self)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/nodes.py", line 1007, in accept
    return visitor.visit_try_stmt(self)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/semanal.py", line 3614, in visit_try_stmt
    self.sem.analyze_try_stmt(s, self, add_global=self.sem.is_module_scope())
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/semanal.py", line 2632, in analyze_try_stmt
    handler.accept(visitor)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/nodes.py", line 811, in accept
    return visitor.visit_block(self)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/semanal.py", line 3458, in visit_block
    node.accept(self)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/nodes.py", line 750, in accept
    return visitor.visit_class_def(self)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/semanal.py", line 3526, in visit_class_def
    self.sem.check_no_global(cdef.name, cdef)
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/semanal.py", line 3318, in check_no_global
    self.name_already_defined(n, ctx, self.globals[n])
  File "/mnt/code/venvs/mypy_venv/lib/python3.5/site-packages/mypy/semanal.py", line 3330, in name_already_defined
    extra_msg = ' on line {}'.format(original_ctx.node.get_line())
AttributeError: 'NoneType' object has no attribute 'get_line'
api/integrations/perforce/git_p4.py: note: use --pdb to drop into pdb
```


Discussion:
I found a smaller reproducer:
```python
try:
    from subprocess import CalledProcessError
except ImportError:
    class CalledProcessError(object):
        pass
```
Could you bisect it?
I think https://github.com/python/mypy/pull/3396 is to blame. @pkch could you please take a look?
Yeah, it's my mistake. Please check the PR.

Resolving patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -3315,7 +3315,7 @@ def check_no_global(self, n: str, ctx: Context,
             elif prev_is_overloaded:
                 self.fail("Definition of '{}' missing 'overload'".format(n), ctx)
             else:
-                self.name_already_defined(n, ctx, self.globals[n])
+                self.name_already_defined(n, ctx)
 
     def name_not_defined(self, name: str, ctx: Context) -> None:
         message = "Name '{}' is not defined".format(name)
@@ -3324,13 +3324,8 @@ def name_not_defined(self, name: str, ctx: Context) -> None:
             message += ' {}'.format(extra)
         self.fail(message, ctx)
 
-    def name_already_defined(self, name: str, ctx: Context,
-                             original_ctx: Optional[SymbolTableNode] = None) -> None:
-        if original_ctx:
-            extra_msg = ' on line {}'.format(original_ctx.node.get_line())
-        else:
-            extra_msg = ''
-        self.fail("Name '{}' already defined{}".format(name, extra_msg), ctx)
+    def name_already_defined(self, name: str, ctx: Context) -> None:
+        self.fail("Name '{}' already defined".format(name), ctx)
 
     def fail(self, msg: str, ctx: Context, serious: bool = False, *,
              blocker: bool = False) -> None:


Test patch:
diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test
--- a/test-data/unit/check-classes.test
+++ b/test-data/unit/check-classes.test
@@ -714,7 +714,7 @@ A()
 class A: pass
 class A: pass
 [out]
-main:4: error: Name 'A' already defined on line 3
+main:4: error: Name 'A' already defined
 
 [case testDocstringInClass]
 import typing
diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test
--- a/test-data/unit/check-functions.test
+++ b/test-data/unit/check-functions.test
@@ -1212,7 +1212,7 @@ from typing import Any
 x = None # type: Any
 if x:
     def f(): pass
-def f(): pass # E: Name 'f' already defined on line 4
+def f(): pass # E: Name 'f' already defined
 
 [case testIncompatibleConditionalFunctionDefinition]
 from typing import Any
@@ -1835,7 +1835,7 @@ f = g # E: Incompatible types in assignment (expression has type Callable[[Any,
 
 [case testRedefineFunction2]
 def f() -> None: pass
-def f() -> None: pass # E: Name 'f' already defined on line 1
+def f() -> None: pass # E: Name 'f' already defined
 
 
 -- Special cases
diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test
--- a/test-data/unit/check-overloading.test
+++ b/test-data/unit/check-overloading.test
@@ -1106,7 +1106,7 @@ def f(a: int) -> None: pass
 def f(a: str) -> None: pass
 [out]
 tmp/foo.pyi:2: error: Single overload definition, multiple required
-tmp/foo.pyi:5: error: Name 'f' already defined on line 2
+tmp/foo.pyi:5: error: Name 'f' already defined
 
 [case testOverloadTuple]
 from foo import *
diff --git a/test-data/unit/semanal-errors.test b/test-data/unit/semanal-errors.test
--- a/test-data/unit/semanal-errors.test
+++ b/test-data/unit/semanal-errors.test
@@ -165,7 +165,7 @@ import typing
 class A: pass
 class A: pass
 [out]
-main:3: error: Name 'A' already defined on line 2
+main:3: error: Name 'A' already defined
 
 [case testMultipleMixedDefinitions]
 import typing
@@ -173,8 +173,8 @@ x = 1
 def x(): pass
 class x: pass
 [out]
-main:3: error: Name 'x' already defined on line 2
-main:4: error: Name 'x' already defined on line 2
+main:3: error: Name 'x' already defined
+main:4: error: Name 'x' already defined
 
 [case testNameNotImported]
 import typing
@@ -1037,13 +1037,13 @@ t = 1 # E: Invalid assignment target
 [case testRedefineTypevar2]
 from typing import TypeVar
 t = TypeVar('t')
-def t(): pass # E: Name 't' already defined on line 2
+def t(): pass # E: Name 't' already defined
 [out]
 
 [case testRedefineTypevar3]
 from typing import TypeVar
 t = TypeVar('t')
-class t: pass # E: Name 't' already defined on line 2
+class t: pass # E: Name 't' already defined
 [out]
 
 [case testRedefineTypevar4]