Repository: python/mypy

Issue:
AssertionError when using double underscore in keyword-only argument
### Are you reporting a bug, or opening a feature request?
Bug report.
### Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro.  

Code for reproduction:

```python3
from typing import Tuple, Dict, Any


def exec(args: Tuple, kwargs: Dict[str, Any]):
    action_id = 0
    action_name = "exec"
    _command_fallback(*args, __action_id=action_id,
                      __action_name=action_name, **kwargs)


def _command_fallback(self, *args, __action_id: int, __action_name: str, **kwargs) -> str:
    print(args, __action_id, __action_name, kwargs)
    return "Success."
```

### What is the actual behavior/output?
```traceback
$ python3 -m mypy temp.py --show-traceback
temp.py:7: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.650
Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/usr/local/lib/python3.7/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/usr/local/lib/python3.7/site-packages/mypy/main.py", line 92, in main
    res = build.build(sources, options, None, flush_errors, fscache)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 156, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 206, in _build
    graph = dispatch(sources, manager)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 2232, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 2532, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 2655, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 1791, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 282, in check_first_pass
    self.accept(d)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/nodes.py", line 611, in accept
    return visitor.visit_func_def(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 704, in visit_func_def
    self._visit_func_def(defn)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 708, in _visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 770, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 934, in check_func_def
    self.accept(item.body)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/nodes.py", line 913, in accept
    return visitor.visit_block(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 1688, in visit_block
    self.accept(s)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/nodes.py", line 928, in accept
    return visitor.visit_expression_stmt(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 2623, in visit_expression_stmt
    self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
  File "/usr/local/lib/python3.7/site-packages/mypy/checkexpr.py", line 3063, in accept
    typ = self.visit_call_expr(node, allow_none_return=True)
  File "/usr/local/lib/python3.7/site-packages/mypy/checkexpr.py", line 229, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
  File "/usr/local/lib/python3.7/site-packages/mypy/checkexpr.py", line 296, in visit_call_expr_inner
    ret_type = self.check_call_expr_with_callee_type(callee_type, e, fullname, object_type)
  File "/usr/local/lib/python3.7/site-packages/mypy/checkexpr.py", line 633, in check_call_expr_with_callee_type
    object_type=object_type)[0]
  File "/usr/local/lib/python3.7/site-packages/mypy/checkexpr.py", line 705, in check_call
    arg_names, formal_to_actual, context, self.msg)
  File "/usr/local/lib/python3.7/site-packages/mypy/checkexpr.py", line 1094, in check_argument_count
    assert argname is not None
AssertionError:
temp.py:7: : note: use --pdb to drop into pdb
```

### What is the behavior/output you expect?
No exception raised.

### What are the versions of mypy and Python you are using?
```bash
$ python3 -m mypy version
mypy 0.701
$ python3 --version 
Python 3.7.3
```

### Do you see the same issue after installing mypy from Git master?
Same bug reproduced with `mypy-0.710+dev.dadae5f6f6f99191d6a7a407660eba5e3732c397`.

### What are the mypy flags you are using? (For example --strict-optional)
```
--show-traceback
```




Discussion:
Yeah, reproduced. I think the special treatment mypy gives to `__foo` names is to blame -- these mean "cannot use this as keyword arg" but apparently the enforcement is buggy. :-(

An even more minimal example is:
```py
def foo(*args, __pos: int, **kwds):
    pass

def bar() -> None:
    foo(__pos=0)
```
Perhaps we should just remove the `assert`, as a temporary fix? It will print `"None"` as the name of the missing keyword argument, which is not ideal, but much better than crashing IMO.
> Perhaps we should just remove the `assert`, as a temporary fix?

I don't think it is very urgent, but it is probably OK as a temporary measure.

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -1138,8 +1138,7 @@ def check_argument_count(self,
                                               not is_unexpected_arg_error):
                 # No actual for a mandatory named formal
                 if messages:
-                    argname = callee.arg_names[i]
-                    assert argname is not None
+                    argname = callee.arg_names[i] or "?"
                     messages.missing_named_argument(callee, context, argname)
                 ok = False
             elif kind in [nodes.ARG_POS, nodes.ARG_OPT,


Test patch: