diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 919e1bb..7ee6a1d 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -7,6 +7,7 @@ Also, it models the Bindings and Scopes.
 import __future__
 import builtins
 import ast
+import collections
 import contextlib
 import doctest
 import functools
@@ -735,7 +736,6 @@ class Checker:
     nodeDepth = 0
     offset = None
     _in_annotation = AnnotationState.NONE
-    _in_deferred = False
 
     builtIns = set(builtin_vars).union(_MAGIC_GLOBALS)
     _customBuiltIns = os.environ.get('PYFLAKES_BUILTINS')
@@ -746,7 +746,7 @@ class Checker:
     def __init__(self, tree, filename='(none)', builtins=None,
                  withDoctest='PYFLAKES_DOCTEST' in os.environ, file_tokens=()):
         self._nodeHandlers = {}
-        self._deferredFunctions = []
+        self._deferred = collections.deque()
         self.deadScopes = []
         self.messages = []
         self.filename = filename
@@ -762,12 +762,8 @@ class Checker:
         for builtin in self.builtIns:
             self.addBinding(None, Builtin(builtin))
         self.handleChildren(tree)
-        self._in_deferred = True
-        self.runDeferred(self._deferredFunctions)
-        # Set _deferredFunctions to None so that deferFunction will fail
-        # noisily if called after we've run through the deferred functions.
-        self._deferredFunctions = None
-        del self.scopeStack[1:]
+
+        self._run_deferred()
 
         self.popScope()
         self.checkDeadScopes()
@@ -787,17 +783,18 @@ class Checker:
         `callable` is called, the scope at the time this is called will be
         restored, however it will contain any new bindings added to it.
         """
-        self._deferredFunctions.append((callable, self.scopeStack[:], self.offset))
+        self._deferred.append((callable, self.scopeStack[:], self.offset))
 
-    def runDeferred(self, deferred):
-        """
-        Run the callables in C{deferred} using their associated scope stack.
-        """
-        for handler, scope, offset in deferred:
-            self.scopeStack = scope
-            self.offset = offset
+    def _run_deferred(self):
+        orig = (self.scopeStack, self.offset)
+
+        while self._deferred:
+            handler, scope, offset = self._deferred.popleft()
+            self.scopeStack, self.offset = scope, offset
             handler()
 
+        self.scopeStack, self.offset = orig
+
     def _in_doctest(self):
         return (len(self.scopeStack) >= 2 and
                 isinstance(self.scopeStack[1], DoctestScope))
@@ -1696,10 +1693,7 @@ class Checker:
                 node.col_offset,
                 messages.ForwardAnnotationSyntaxError,
             )
-            if self._in_deferred:
-                fn()
-            else:
-                self.deferFunction(fn)
+            self.deferFunction(fn)
 
     def CONSTANT(self, node):
         if isinstance(node.value, str):

diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py
index 2f27b06..f0fd3b9 100644
--- a/pyflakes/test/test_type_annotations.py
+++ b/pyflakes/test/test_type_annotations.py
@@ -594,6 +594,20 @@ class TestTypeAnnotations(TestCase):
                 return None
         """)
 
+    def test_forward_annotations_for_classes_in_scope(self):
+        # see #749
+        self.flakes("""
+        from typing import Optional
+
+        def f():
+            class C:
+                a: "D"
+                b: Optional["D"]
+                c: "Optional[D]"
+
+            class D: pass
+        """)
+
     def test_idomiatic_typing_guards(self):
         # typing.TYPE_CHECKING: python3.5.3+
         self.flakes("""

Error when referencing a class yet not declared inside function
I am facing a bug.

I created this example to test all behaviours I can foresee and spotted a bug:

```python
from typing import Optional

class InnerModel1:
        model2: "Model2"
        mode2_no_err1: Optional["Model2"] # NO ERROR
        mode2_no_err2: "Optional[Model2]" # NO ERROR

class Model2:
    pass

def f():
    class InnerModel1:
        model2: "InnerModel2"
        mode2_err: Optional["InnerModel2"] # ERROR
        mode2_no_err: "Optional[InnerModel2]"

    class InnerModel2:
        pass
```
if I run `pyflakes` against this code I get:

`pyflakes_bug.py:14:29: undefined name 'InnerModel2'`

I would expect that annotating a with `Optional["InnerModel2"]` or with `"Optional[InnerModel2]"` should have the same behaviour as demonstrated with `Optional["Model2"]` and `"Optional[Model2]"`.

Why am I defining a class inside a method?  For now this is supporting lots of unit testing on my side.



pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_forward_annotations_for_classes_in_scope
pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_TypeAlias_annotations pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_aliased_import pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_annotated_async_def pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_annotated_type_typing_missing_forward_type pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_annotated_type_typing_missing_forward_type_multiple_args pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_annotated_type_typing_with_string_args pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_annotated_type_typing_with_string_args_in_union pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_annotating_an_import pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_deferred_twice_annotation pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_idomiatic_typing_guards pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_literal_type_some_other_module pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_literal_type_typing pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_literal_type_typing_extensions pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_literal_union_type_typing pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_namedtypes_classes pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_nested_partially_quoted_type_assignment pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_not_a_typing_overload pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_overload_in_class pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_overload_with_multiple_decorators pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_partial_string_annotations_with_future_annotations pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_partially_quoted_type_annotation pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_partially_quoted_type_assignment pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_positional_only_argument_annotations pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_postponed_annotations pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_quoted_TypeVar_bound pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_quoted_TypeVar_constraints pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_quoted_type_cast pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_quoted_type_cast_renamed_import pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_return_annotation_is_class_scope_variable pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_return_annotation_is_function_body_variable pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_type_annotation_clobbers_all pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_type_cast_literal_str_to_str pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_typednames_correct_forward_ref pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_typingExtensionsOverload pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_typingOverload pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_typingOverloadAsync pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_typing_guard_for_protocol pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_unassigned_annotation_is_undefined pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_unused_annotation pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_variable_annotation_references_self_name_undefined pyflakes/test/test_type_annotations.py::TestTypeAnnotations::test_variable_annotations