# Buggy code
def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
    result = None  # type: Optional[Type]
    node = e.node

    if isinstance(e, NameExpr) and e.is_special_form:
        # A special form definition, nothing to check here.
        return AnyType(TypeOfAny.special_form)

    if isinstance(node, Var):
        # Variable reference.
        result = self.analyze_var_ref(node, e)
        if isinstance(result, PartialType):
            result = self.chk.handle_partial_var_type(result, lvalue, node, e)
    elif isinstance(node, FuncDef):
        # Reference to a global function.
        result = function_type(node, self.named_type("builtins.function"))
    elif isinstance(node, OverloadedFuncDef) and node.type is not None:
        # node.type is None when there are multiple definitions of a function
        # and it's decorated by something that is not typing.overload
        result = node.type
    elif isinstance(node, TypeInfo):
        # Reference to a type object.
        result = type_object_type(node, self.named_type)
        if isinstance(result, CallableType) and isinstance(result.ret_type, Instance):
            # We need to set correct line and column
            # TODO: always do this in type_object_type by passing the original context
            result.ret_type.line = e.line
            result.ret_type.column = e.column
        if isinstance(self.type_context[-1], TypeType):
            # This is the type in a Type[] expression, so substitute type
            # variables with Any.
            result = erasetype.erase_typevars(result)
    elif isinstance(node, MypyFile):
        # Reference to a module object.
        try:
            result = self.named_type("types.ModuleType")
        except KeyError:
            # In test cases might 'types' may not be available.
            # Fall back to a dummy 'object' type instead to
            # avoid a crash.
            result = self.named_type("builtins.object")
    elif isinstance(node, Decorator):
        result = self.analyze_var_ref(node.var, e)
    elif isinstance(node, TypeAlias):
        # Something that refers to a type alias appears in runtime context.
        # Note that we suppress bogus errors for alias redefinitions,
        # they are already reported in semanal.py.
        result = self.alias_type_in_runtime_context(
            node.target,
            node.alias_tvars,
            node.no_args,
            e,
            alias_definition=e.is_alias_rvalue or lvalue,
        )
    else:
        if isinstance(node, PlaceholderNode):
            assert False, "PlaceholderNode %r leaked to checker" % node.fullname()
        # Unknown reference; use any type implicitly to avoid
        # generating extra type errors.
        result = AnyType(TypeOfAny.from_error)
    assert result is not None
    return result


# Traceback (AssertionError)
(tildes) vagrant@ubuntu-xenial:/opt/tildes$ mypy --new-semantic-analyzer --show-traceback test_mypy.py
test_mypy.py:16: error: Cannot assign to a method
test_mypy.py:16: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.720+dev.48916e63403645730a584d6898fbe925d513a841
Traceback (most recent call last):
File "/opt/venvs/tildes/bin/mypy", line 10, in <module>
sys.exit(console_entry())
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/__main__.py", line 8, in console_entry
main(None, sys.stdout, sys.stderr)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/main.py", line 83, in main
res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/build.py", line 164, in build
result = _build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/build.py", line 224, in _build
graph = dispatch(sources, manager, stdout)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/build.py", line 2567, in dispatch
process_graph(graph, manager)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/build.py", line 2880, in process_graph
process_stale_scc(graph, scc, manager)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/build.py", line 2987, in process_stale_scc
graph[id].type_check_first_pass()
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/build.py", line 2096, in type_check_first_pass
self.type_checker().check_first_pass()
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 281, in check_first_pass
self.accept(d)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 392, in accept
stmt.accept(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/nodes.py", line 913, in accept
return visitor.visit_class_def(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 1596, in visit_class_def
self.accept(defn.defs)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 392, in accept
stmt.accept(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/nodes.py", line 978, in accept
return visitor.visit_block(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 1785, in visit_block
self.accept(s)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 392, in accept
stmt.accept(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/nodes.py", line 655, in accept
return visitor.visit_func_def(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 703, in visit_func_def
self._visit_func_def(defn)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 707, in _visit_func_def
self.check_func_item(defn, name=defn.name())
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 769, in check_func_item
self.check_func_def(defn, typ, name)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 935, in check_func_def
self.accept(item.body)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 392, in accept
stmt.accept(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/nodes.py", line 978, in accept
return visitor.visit_block(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 1785, in visit_block
self.accept(s)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 392, in accept
stmt.accept(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/nodes.py", line 1036, in accept
return visitor.visit_assignment_stmt(self)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 1793, in visit_assignment_stmt
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 1834, in check_assignment
lvalue_type, index_lvalue, inferred = self.check_lvalue(lvalue)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checker.py", line 2479, in check_lvalue
True)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checkexpr.py", line 1766, in analyze_ordinary_member_access
in_literal_context=self.is_literal_context())
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checkmember.py", line 103, in analyze_member_access
result = _analyze_member_access(name, typ, mx, override_info)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checkmember.py", line 117, in _analyze_member_access
return analyze_instance_member_access(name, typ, mx, override_info)
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/checkmember.py", line 179, in analyze_instance_member_access
signature = function_type(method, mx.builtin_type('builtins.function'))
File "/opt/venvs/tildes/lib/python3.7/site-packages/mypy/types.py", line 2188, in function_type
assert isinstance(func, mypy.nodes.FuncItem), str(func)
AssertionError: OverloadedFuncDef:7(
Decorator:11(
Var(name)
MemberExpr:11(
NameExpr(name [test_mypy.FirstNameOnly.name])
setter)
FuncDef:12(
name
Args(
Var(self)
Var(value))
def (self: test_mypy.FirstNameOnly, value: builtins.str)
Block:12(
AssignmentStmt:13(
MemberExpr:13(
NameExpr(self [l])
first_name)
NameExpr(value [l]))))))

# Fixed code
def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
    result = None  # type: Optional[Type]
    node = e.node

    if isinstance(e, NameExpr) and e.is_special_form:
        # A special form definition, nothing to check here.
        return AnyType(TypeOfAny.special_form)

    if isinstance(node, Var):
        # Variable reference.
        result = self.analyze_var_ref(node, e)
        if isinstance(result, PartialType):
            result = self.chk.handle_partial_var_type(result, lvalue, node, e)
    elif isinstance(node, FuncDef):
        # Reference to a global function.
        result = function_type(node, self.named_type("builtins.function"))
    elif isinstance(node, OverloadedFuncDef) and node.type is not None:
        # node.type is None when there are multiple definitions of a function
        # and it's decorated by something that is not typing.overload
        # TODO: use a dummy Overloaded instead of AnyType in this case
        # like we do in mypy.types.function_type()?
        result = node.type
    elif isinstance(node, TypeInfo):
        # Reference to a type object.
        result = type_object_type(node, self.named_type)
        if isinstance(result, CallableType) and isinstance(result.ret_type, Instance):
            # We need to set correct line and column
            # TODO: always do this in type_object_type by passing the original context
            result.ret_type.line = e.line
            result.ret_type.column = e.column
        if isinstance(self.type_context[-1], TypeType):
            # This is the type in a Type[] expression, so substitute type
            # variables with Any.
            result = erasetype.erase_typevars(result)
    elif isinstance(node, MypyFile):
        # Reference to a module object.
        try:
            result = self.named_type("types.ModuleType")
        except KeyError:
            # In test cases might 'types' may not be available.
            # Fall back to a dummy 'object' type instead to
            # avoid a crash.
            result = self.named_type("builtins.object")
    elif isinstance(node, Decorator):
        result = self.analyze_var_ref(node.var, e)
    elif isinstance(node, TypeAlias):
        # Something that refers to a type alias appears in runtime context.
        # Note that we suppress bogus errors for alias redefinitions,
        # they are already reported in semanal.py.
        result = self.alias_type_in_runtime_context(
            node.target,
            node.alias_tvars,
            node.no_args,
            e,
            alias_definition=e.is_alias_rvalue or lvalue,
        )
    else:
        if isinstance(node, PlaceholderNode):
            assert False, "PlaceholderNode %r leaked to checker" % node.fullname()
        # Unknown reference; use any type implicitly to avoid
        # generating extra type errors.
        result = AnyType(TypeOfAny.from_error)
    assert result is not None
    return result