# Buggy code
def lookup_target(
    manager: BuildManager, target: str
) -> Tuple[List[DeferredNode], Optional[TypeInfo]]:
    """Look up a target by fully-qualified name.

    The first item in the return tuple is a list of deferred nodes that
    needs to be reprocessed. If the target represents a TypeInfo corresponding
    to a protocol, return it as a second item in the return tuple, otherwise None.
    """

    def not_found() -> None:
        manager.log_fine_grained(
            "Can't find matching target for %s (stale dependency?)" % target
        )

    modules = manager.modules
    items = split_target(modules, target)
    if items is None:
        not_found()  # Stale dependency
        return [], None
    module, rest = items
    if rest:
        components = rest.split(".")
    else:
        components = []
    node = modules[module]  # type: Optional[SymbolNode]
    file = None  # type: Optional[MypyFile]
    active_class = None
    active_class_name = None
    for c in components:
        if isinstance(node, TypeInfo):
            active_class = node
            active_class_name = node.name()
        if isinstance(node, MypyFile):
            file = node
        if not isinstance(node, (MypyFile, TypeInfo)) or c not in node.names:
            not_found()  # Stale dependency
            return [], None
        node = node.names[c].node
    if isinstance(node, TypeInfo):
        # A ClassDef target covers the body of the class and everything defined
        # within it.  To get the body we include the entire surrounding target,
        # typically a module top-level, since we don't support processing class
        # bodies as separate entitites for simplicity.
        assert file is not None
        if node.fullname() != target:
            # This is a reference to a different TypeInfo, likely due to a stale dependency.
            # Processing them would spell trouble -- for example, we could be refreshing
            # a deserialized TypeInfo with missing attributes.
            not_found()
            return [], None
        result = [DeferredNode(file, None, None)]
        stale_info = None  # type: Optional[TypeInfo]
        if node.is_protocol:
            stale_info = node
        for name, symnode in node.names.items():
            node = symnode.node
            if isinstance(node, FuncDef):
                method, _ = lookup_target(manager, target + "." + name)
                result.extend(method)
        return result, stale_info
    if isinstance(node, Decorator):
        # Decorator targets actually refer to the function definition only.
        node = node.func
    if not isinstance(node, (FuncDef, MypyFile, OverloadedFuncDef)):
        # The target can't be refreshed. It's possible that the target was
        # changed to another type and we have a stale dependency pointing to it.
        not_found()
        return [], None
    if node.fullname() != target:
        # Stale reference points to something unexpected. We shouldn't process since the
        # context will be wrong and it could be a partially initialized deserialized node.
        not_found()
        return [], None
    return [DeferredNode(node, active_class_name, active_class)], None


# Traceback (AssertionError)
pymc3/model.py:672: error: Decorated property not supported
pymc3/variational/opvi.py:1556: error: Decorated property not supported
pymc3/variational/flows.py:193: error: Decorated property not supported
pymc3/__init__.py:25: error: Name 'utils' already defined (by an import)
pymc3/data.py:226: error: Need type annotation for 'RNG'
Traceback (most recent call last):
File "/Users/rpg/Library/Python/3.7/bin/mypy", line 11, in <module>
sys.exit(console_entry())
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/__main__.py", line 7, in console_entry
main(None)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/main.py", line 91, in main
res = type_check_only(sources, bin_dir, options, flush_errors, fscache)  # noqa
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/main.py", line 148, in type_check_only
fscache=fscache)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/build.py", line 177, in build
flush_errors, fscache)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/build.py", line 350, in _build
graph = dispatch(sources, manager)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/build.py", line 2560, in dispatch
process_graph(graph, manager)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/build.py", line 2853, in process_graph
process_stale_scc(graph, scc, manager)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/build.py", line 2976, in process_stale_scc
graph[id].type_check_first_pass()
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/build.py", line 2151, in type_check_first_pass
self.type_checker().check_first_pass()
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 253, in check_first_pass
self.accept(d)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 352, in accept
stmt.accept(self)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/nodes.py", line 808, in accept
return visitor.visit_class_def(self)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 1368, in visit_class_def
self.accept(defn.defs)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 352, in accept
stmt.accept(self)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/nodes.py", line 873, in accept
return visitor.visit_block(self)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 1514, in visit_block
self.accept(s)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 352, in accept
stmt.accept(self)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/nodes.py", line 443, in accept
return visitor.visit_overloaded_func_def(self)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 385, in visit_overloaded_func_def
self._visit_overloaded_func_def(defn)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 409, in _visit_overloaded_func_def
self.check_method_override(defn)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 1198, in check_method_override
self.check_method_or_accessor_override_for_base(defn, base)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 1208, in check_method_or_accessor_override_for_base
self.check_method_override_for_base_with_name(defn, name, base)
File "/Users/rpg/Library/Python/3.7/lib/python/site-packages/mypy/checker.py", line 1252, in check_method_override_for_base_with_name
assert False, str(base_attr.node)
AssertionError: Var(sampler_vars)

# Fixed code
def lookup_target(
    manager: BuildManager, target: str
) -> Tuple[List[FineGrainedDeferredNode], Optional[TypeInfo]]:
    """Look up a target by fully-qualified name.

    The first item in the return tuple is a list of deferred nodes that
    needs to be reprocessed. If the target represents a TypeInfo corresponding
    to a protocol, return it as a second item in the return tuple, otherwise None.
    """

    def not_found() -> None:
        manager.log_fine_grained(
            "Can't find matching target for %s (stale dependency?)" % target
        )

    modules = manager.modules
    items = split_target(modules, target)
    if items is None:
        not_found()  # Stale dependency
        return [], None
    module, rest = items
    if rest:
        components = rest.split(".")
    else:
        components = []
    node = modules[module]  # type: Optional[SymbolNode]
    file = None  # type: Optional[MypyFile]
    active_class = None
    active_class_name = None
    for c in components:
        if isinstance(node, TypeInfo):
            active_class = node
            active_class_name = node.name()
        if isinstance(node, MypyFile):
            file = node
        if not isinstance(node, (MypyFile, TypeInfo)) or c not in node.names:
            not_found()  # Stale dependency
            return [], None
        node = node.names[c].node
    if isinstance(node, TypeInfo):
        # A ClassDef target covers the body of the class and everything defined
        # within it.  To get the body we include the entire surrounding target,
        # typically a module top-level, since we don't support processing class
        # bodies as separate entitites for simplicity.
        assert file is not None
        if node.fullname() != target:
            # This is a reference to a different TypeInfo, likely due to a stale dependency.
            # Processing them would spell trouble -- for example, we could be refreshing
            # a deserialized TypeInfo with missing attributes.
            not_found()
            return [], None
        result = [FineGrainedDeferredNode(file, None, None)]
        stale_info = None  # type: Optional[TypeInfo]
        if node.is_protocol:
            stale_info = node
        for name, symnode in node.names.items():
            node = symnode.node
            if isinstance(node, FuncDef):
                method, _ = lookup_target(manager, target + "." + name)
                result.extend(method)
        return result, stale_info
    if isinstance(node, Decorator):
        # Decorator targets actually refer to the function definition only.
        node = node.func
    if not isinstance(node, (FuncDef, MypyFile, OverloadedFuncDef)):
        # The target can't be refreshed. It's possible that the target was
        # changed to another type and we have a stale dependency pointing to it.
        not_found()
        return [], None
    if node.fullname() != target:
        # Stale reference points to something unexpected. We shouldn't process since the
        # context will be wrong and it could be a partially initialized deserialized node.
        not_found()
        return [], None
    return [FineGrainedDeferredNode(node, active_class_name, active_class)], None