We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
overwriting None from Tuple[AnyStr]'s only item causes PartialType-related crash
``` python
import os.path

x = None
if 0:
    _, x = os.path.split("blah")
```

results in:

```
Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 6, in <module>
    main(__file__)
  File "/usr/local/lib/python3.5/site-packages/mypy/main.py", line 54, in main
    res = type_check_only(sources, bin_dir, options)
  File "/usr/local/lib/python3.5/site-packages/mypy/main.py", line 102, in type_check_only
    python_path=options.python_path)
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 209, in build
    dispatch(sources, manager)
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 1325, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 1456, in process_graph
    process_stale_scc(graph, scc)
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 1486, in process_stale_scc
    graph[id].type_check()
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 1305, in type_check
    manager.type_checker.visit_file(self.tree, self.xpath)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 424, in visit_file
    self.accept(d)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 465, in accept
    typ = node.accept(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/nodes.py", line 862, in accept
    return visitor.visit_if_stmt(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 1661, in visit_if_stmt
    self.accept(b)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 465, in accept
    typ = node.accept(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/nodes.py", line 715, in accept
    return visitor.visit_block(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 1149, in visit_block
    self.accept(s)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 465, in accept
    typ = node.accept(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/nodes.py", line 753, in accept
    return visitor.visit_assignment_stmt(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 1158, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 1175, in check_assignment
    infer_lvalue_type)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 1244, in check_assignment_to_multiple_lvalues
    self.check_multi_assignment(lvalues, rvalue, context, infer_lvalue_type)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 1277, in check_multi_assignment
    context, undefined_rvalue, infer_lvalue_type)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 1297, in check_multi_assignment_from_tuple
    rvalue_type = cast(TupleType, self.accept(rvalue, lvalue_type))
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 465, in accept
    typ = node.accept(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/nodes.py", line 1185, in accept
    return visitor.visit_call_expr(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/checker.py", line 1997, in visit_call_expr
    return self.expr_checker.visit_call_expr(e)
  File "/usr/local/lib/python3.5/site-packages/mypy/checkexpr.py", line 141, in visit_call_expr
    return self.check_call_expr_with_callee_type(callee_type, e)
  File "/usr/local/lib/python3.5/site-packages/mypy/checkexpr.py", line 192, in check_call_expr_with_callee_type
    e.arg_names, callable_node=e.callee)[0]
  File "/usr/local/lib/python3.5/site-packages/mypy/checkexpr.py", line 230, in check_call
    callee, context)
  File "/usr/local/lib/python3.5/site-packages/mypy/checkexpr.py", line 370, in infer_function_type_arguments_using_context
    args = infer_type_arguments(callable.type_var_ids(), ret_type, erased_ctx)
  File "/usr/local/lib/python3.5/site-packages/mypy/infer.py", line 43, in infer_type_arguments
    return solve_constraints(type_var_ids, constraints)
  File "/usr/local/lib/python3.5/site-packages/mypy/solve.py", line 50, in solve_constraints
    top = meet_types(top, c.target)
  File "/usr/local/lib/python3.5/site-packages/mypy/meet.py", line 22, in meet_types
    return t.accept(TypeMeetVisitor(s))
  File "/usr/local/lib/python3.5/site-packages/mypy/types.py", line 763, in accept
    return visitor.visit_partial_type(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/meet.py", line 208, in visit_partial_type
    assert False, 'Internal error'
AssertionError: Internal error

*** INTERNAL ERROR ***

test.py:5: error: Internal error -- please report a bug at https://github.com/python/mypy/issues

NOTE: you can use "mypy --pdb ..." to drop into the debugger when this happens.
```

We might want to fix this before releasing.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mypy/applytype.py`
Content:
```
1 from typing import List, Dict
2 
3 import mypy.subtypes
4 from mypy.sametypes import is_same_type
5 from mypy.expandtype import expand_type
6 from mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType
7 from mypy.messages import MessageBuilder
8 from mypy.nodes import Context
9 
10 
11 def apply_generic_arguments(callable: CallableType, types: List[Type],
12                             msg: MessageBuilder, context: Context) -> CallableType:
13     """Apply generic type arguments to a callable type.
14 
15     For example, applying [int] to 'def [T] (T) -> T' results in
16     'def (int) -> int'.
17 
18     Note that each type can be None; in this case, it will not be applied.
19     """
20     tvars = callable.variables
21     assert len(tvars) == len(types)
22     # Check that inferred type variable values are compatible with allowed
23     # values and bounds.  Also, promote subtype values to allowed values.
24     types = types[:]
25     for i, type in enumerate(types):
26         values = callable.variables[i].values
27         if values and type:
28             if isinstance(type, AnyType):
29                 continue
30             if isinstance(type, TypeVarType) and type.values:
31                 # Allow substituting T1 for T if every allowed value of T1
32                 # is also a legal value of T.
33                 if all(any(is_same_type(v, v1) for v in values)
34                        for v1 in type.values):
35                     continue
36             for value in values:
37                 if mypy.subtypes.is_subtype(type, value):
38                     types[i] = value
39                     break
40             else:
41                 msg.incompatible_typevar_value(callable, i + 1, type, context)
42 
43         upper_bound = callable.variables[i].upper_bound
44         if type and not mypy.subtypes.satisfies_upper_bound(type, upper_bound):
45             msg.incompatible_typevar_value(callable, i + 1, type, context)
46 
47     # Create a map from type variable id to target type.
48     id_to_type = {}  # type: Dict[TypeVarId, Type]
49     for i, tv in enumerate(tvars):
50         if types[i]:
51             id_to_type[tv.id] = types[i]
52 
53     # Apply arguments to argument types.
54     arg_types = [expand_type(at, id_to_type) for at in callable.arg_types]
55 
56     # The callable may retain some type vars if only some were applied.
57     remaining_tvars = [tv for tv in tvars if tv.id not in id_to_type]
58 
59     return callable.copy_modified(
60         arg_types=arg_types,
61         ret_type=expand_type(callable.ret_type, id_to_type),
62         variables=remaining_tvars,
63     )
64
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/mypy/applytype.py b/mypy/applytype.py
--- a/mypy/applytype.py
+++ b/mypy/applytype.py
@@ -3,7 +3,7 @@
 import mypy.subtypes
 from mypy.sametypes import is_same_type
 from mypy.expandtype import expand_type
-from mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType
+from mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType, PartialType
 from mypy.messages import MessageBuilder
 from mypy.nodes import Context
 
@@ -34,14 +34,15 @@
                        for v1 in type.values):
                     continue
             for value in values:
-                if mypy.subtypes.is_subtype(type, value):
+                if isinstance(type, PartialType) or mypy.subtypes.is_subtype(type, value):
                     types[i] = value
                     break
             else:
                 msg.incompatible_typevar_value(callable, i + 1, type, context)
 
         upper_bound = callable.variables[i].upper_bound
-        if type and not mypy.subtypes.satisfies_upper_bound(type, upper_bound):
+        if (type and not isinstance(type, PartialType) and
+                not mypy.subtypes.satisfies_upper_bound(type, upper_bound)):
             msg.incompatible_typevar_value(callable, i + 1, type, context)
 
     # Create a map from type variable id to target type.

{"golden_diff": "diff --git a/mypy/applytype.py b/mypy/applytype.py\n--- a/mypy/applytype.py\n+++ b/mypy/applytype.py\n@@ -3,7 +3,7 @@\n import mypy.subtypes\n from mypy.sametypes import is_same_type\n from mypy.expandtype import expand_type\n-from mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType\n+from mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType, PartialType\n from mypy.messages import MessageBuilder\n from mypy.nodes import Context\n \n@@ -34,14 +34,15 @@\n                        for v1 in type.values):\n                     continue\n             for value in values:\n-                if mypy.subtypes.is_subtype(type, value):\n+                if isinstance(type, PartialType) or mypy.subtypes.is_subtype(type, value):\n                     types[i] = value\n                     break\n             else:\n                 msg.incompatible_typevar_value(callable, i + 1, type, context)\n \n         upper_bound = callable.variables[i].upper_bound\n-        if type and not mypy.subtypes.satisfies_upper_bound(type, upper_bound):\n+        if (type and not isinstance(type, PartialType) and\n+                not mypy.subtypes.satisfies_upper_bound(type, upper_bound)):\n             msg.incompatible_typevar_value(callable, i + 1, type, context)\n \n     # Create a map from type variable id to target type.\n", "issue": "overwriting None from Tuple[AnyStr]'s only item causes PartialType-related crash\n``` python\nimport os.path\n\nx = None\nif 0:\n    _, x = os.path.split(\"blah\")\n```\n\nresults in:\n\n```\nTraceback (most recent call last):\n  File \"/usr/local/bin/mypy\", line 6, in <module>\n    main(__file__)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/main.py\", line 54, in main\n    res = type_check_only(sources, bin_dir, options)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/main.py\", line 102, in type_check_only\n    python_path=options.python_path)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/build.py\", line 209, in build\n    dispatch(sources, manager)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/build.py\", line 1325, in dispatch\n    process_graph(graph, manager)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/build.py\", line 1456, in process_graph\n    process_stale_scc(graph, scc)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/build.py\", line 1486, in process_stale_scc\n    graph[id].type_check()\n  File \"/usr/local/lib/python3.5/site-packages/mypy/build.py\", line 1305, in type_check\n    manager.type_checker.visit_file(self.tree, self.xpath)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 424, in visit_file\n    self.accept(d)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 465, in accept\n    typ = node.accept(self)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/nodes.py\", line 862, in accept\n    return visitor.visit_if_stmt(self)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 1661, in visit_if_stmt\n    self.accept(b)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 465, in accept\n    typ = node.accept(self)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/nodes.py\", line 715, in accept\n    return visitor.visit_block(self)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 1149, in visit_block\n    self.accept(s)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 465, in accept\n    typ = node.accept(self)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/nodes.py\", line 753, in accept\n    return visitor.visit_assignment_stmt(self)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 1158, in visit_assignment_stmt\n    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 1175, in check_assignment\n    infer_lvalue_type)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 1244, in check_assignment_to_multiple_lvalues\n    self.check_multi_assignment(lvalues, rvalue, context, infer_lvalue_type)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 1277, in check_multi_assignment\n    context, undefined_rvalue, infer_lvalue_type)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 1297, in check_multi_assignment_from_tuple\n    rvalue_type = cast(TupleType, self.accept(rvalue, lvalue_type))\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 465, in accept\n    typ = node.accept(self)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/nodes.py\", line 1185, in accept\n    return visitor.visit_call_expr(self)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checker.py\", line 1997, in visit_call_expr\n    return self.expr_checker.visit_call_expr(e)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checkexpr.py\", line 141, in visit_call_expr\n    return self.check_call_expr_with_callee_type(callee_type, e)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checkexpr.py\", line 192, in check_call_expr_with_callee_type\n    e.arg_names, callable_node=e.callee)[0]\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checkexpr.py\", line 230, in check_call\n    callee, context)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/checkexpr.py\", line 370, in infer_function_type_arguments_using_context\n    args = infer_type_arguments(callable.type_var_ids(), ret_type, erased_ctx)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/infer.py\", line 43, in infer_type_arguments\n    return solve_constraints(type_var_ids, constraints)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/solve.py\", line 50, in solve_constraints\n    top = meet_types(top, c.target)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/meet.py\", line 22, in meet_types\n    return t.accept(TypeMeetVisitor(s))\n  File \"/usr/local/lib/python3.5/site-packages/mypy/types.py\", line 763, in accept\n    return visitor.visit_partial_type(self)\n  File \"/usr/local/lib/python3.5/site-packages/mypy/meet.py\", line 208, in visit_partial_type\n    assert False, 'Internal error'\nAssertionError: Internal error\n\n*** INTERNAL ERROR ***\n\ntest.py:5: error: Internal error -- please report a bug at https://github.com/python/mypy/issues\n\nNOTE: you can use \"mypy --pdb ...\" to drop into the debugger when this happens.\n```\n\nWe might want to fix this before releasing.\n\n", "before_files": [{"content": "from typing import List, Dict\n\nimport mypy.subtypes\nfrom mypy.sametypes import is_same_type\nfrom mypy.expandtype import expand_type\nfrom mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType\nfrom mypy.messages import MessageBuilder\nfrom mypy.nodes import Context\n\n\ndef apply_generic_arguments(callable: CallableType, types: List[Type],\n                            msg: MessageBuilder, context: Context) -> CallableType:\n    \"\"\"Apply generic type arguments to a callable type.\n\n    For example, applying [int] to 'def [T] (T) -> T' results in\n    'def (int) -> int'.\n\n    Note that each type can be None; in this case, it will not be applied.\n    \"\"\"\n    tvars = callable.variables\n    assert len(tvars) == len(types)\n    # Check that inferred type variable values are compatible with allowed\n    # values and bounds.  Also, promote subtype values to allowed values.\n    types = types[:]\n    for i, type in enumerate(types):\n        values = callable.variables[i].values\n        if values and type:\n            if isinstance(type, AnyType):\n                continue\n            if isinstance(type, TypeVarType) and type.values:\n                # Allow substituting T1 for T if every allowed value of T1\n                # is also a legal value of T.\n                if all(any(is_same_type(v, v1) for v in values)\n                       for v1 in type.values):\n                    continue\n            for value in values:\n                if mypy.subtypes.is_subtype(type, value):\n                    types[i] = value\n                    break\n            else:\n                msg.incompatible_typevar_value(callable, i + 1, type, context)\n\n        upper_bound = callable.variables[i].upper_bound\n        if type and not mypy.subtypes.satisfies_upper_bound(type, upper_bound):\n            msg.incompatible_typevar_value(callable, i + 1, type, context)\n\n    # Create a map from type variable id to target type.\n    id_to_type = {}  # type: Dict[TypeVarId, Type]\n    for i, tv in enumerate(tvars):\n        if types[i]:\n            id_to_type[tv.id] = types[i]\n\n    # Apply arguments to argument types.\n    arg_types = [expand_type(at, id_to_type) for at in callable.arg_types]\n\n    # The callable may retain some type vars if only some were applied.\n    remaining_tvars = [tv for tv in tvars if tv.id not in id_to_type]\n\n    return callable.copy_modified(\n        arg_types=arg_types,\n        ret_type=expand_type(callable.ret_type, id_to_type),\n        variables=remaining_tvars,\n    )\n", "path": "mypy/applytype.py"}], "after_files": [{"content": "from typing import List, Dict\n\nimport mypy.subtypes\nfrom mypy.sametypes import is_same_type\nfrom mypy.expandtype import expand_type\nfrom mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType, PartialType\nfrom mypy.messages import MessageBuilder\nfrom mypy.nodes import Context\n\n\ndef apply_generic_arguments(callable: CallableType, types: List[Type],\n                            msg: MessageBuilder, context: Context) -> CallableType:\n    \"\"\"Apply generic type arguments to a callable type.\n\n    For example, applying [int] to 'def [T] (T) -> T' results in\n    'def (int) -> int'.\n\n    Note that each type can be None; in this case, it will not be applied.\n    \"\"\"\n    tvars = callable.variables\n    assert len(tvars) == len(types)\n    # Check that inferred type variable values are compatible with allowed\n    # values and bounds.  Also, promote subtype values to allowed values.\n    types = types[:]\n    for i, type in enumerate(types):\n        values = callable.variables[i].values\n        if values and type:\n            if isinstance(type, AnyType):\n                continue\n            if isinstance(type, TypeVarType) and type.values:\n                # Allow substituting T1 for T if every allowed value of T1\n                # is also a legal value of T.\n                if all(any(is_same_type(v, v1) for v in values)\n                       for v1 in type.values):\n                    continue\n            for value in values:\n                if isinstance(type, PartialType) or mypy.subtypes.is_subtype(type, value):\n                    types[i] = value\n                    break\n            else:\n                msg.incompatible_typevar_value(callable, i + 1, type, context)\n\n        upper_bound = callable.variables[i].upper_bound\n        if (type and not isinstance(type, PartialType) and\n                not mypy.subtypes.satisfies_upper_bound(type, upper_bound)):\n            msg.incompatible_typevar_value(callable, i + 1, type, context)\n\n    # Create a map from type variable id to target type.\n    id_to_type = {}  # type: Dict[TypeVarId, Type]\n    for i, tv in enumerate(tvars):\n        if types[i]:\n            id_to_type[tv.id] = types[i]\n\n    # Apply arguments to argument types.\n    arg_types = [expand_type(at, id_to_type) for at in callable.arg_types]\n\n    # The callable may retain some type vars if only some were applied.\n    remaining_tvars = [tv for tv in tvars if tv.id not in id_to_type]\n\n    return callable.copy_modified(\n        arg_types=arg_types,\n        ret_type=expand_type(callable.ret_type, id_to_type),\n        variables=remaining_tvars,\n    )\n", "path": "mypy/applytype.py"}]}