Repository: tensorflow
Issue: AutoGraph did convert this function: NameError: name 'Tuple' is not defined
Labels: stat:awaiting tensorflower,type:bug,comp:tf.function,TF 2.11

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

No

### Source

source

### Tensorflow Version

2.11.0

### Custom Code

Yes

### OS Platform and Distribution

Mac, Linux, Google Colab

### Mobile device

_No response_

### Python version

_No response_

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

</details>

### Current Behaviour?

`tf.function` fails when type annotations are used which are locally imported. I get:

```
Cause: name 'Tuple' is not defined
```

Note:

* It seems important that `Tuple` is locally imported. If it is imported globally in the module, there does not seem to be a problem.
* When I use `from __future__ import annotations`, there is also no error. But I assume because this will just not evaluate it directly, but it still lacks the `Tuple` reference, although it's maybe really not relevant then.


### Standalone code to reproduce the issue

```python
import tensorflow as tf

print("TensorFlow:", tf.__version__)

tf.compat.v1.disable_eager_execution()

session = tf.compat.v1.Session()


# https://www.tensorflow.org/guide/function


def f(x: tf.Tensor):
  from typing import Tuple

  @tf.function
  def local_func(x: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]:
    while tf.reduce_sum(x) > 1:
      tf.print(x)
      x = tf.tanh(x)
    return x, x

  return local_func(x)


session.run(f(tf.random.uniform([5])))
```


Colab link: https://colab.research.google.com/drive/1K69XH_RsU-Ux-RBfUd0B4eR8iJFTXoFM?usp=sharing

### Relevant log output

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/impl/api.py", line 427, in converted_call
    converted_f = _convert_actual(target_entity, program_ctx)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/impl/api.py", line 269, in _convert_actual
    transformed, module, source_map = _TRANSPILER.transform(entity, program_ctx)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/pyct/transpiler.py", line 282, in transform
    return self.transform_function(obj, user_context)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/pyct/transpiler.py", line 490, in transform_function
    transformed_fn = factory.instantiate(
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/pyct/transpiler.py", line 213, in instantiate
    new_fn = bound_factory(**self._extra_locals)  # pylint:disable=not-callable
  File "/tmp/__autograph_generated_filem5lq6_hq.py", line 6, in inner_factory
    def tf__local_func(x: tf.Tensor) -> Tuple[(tf.Tensor, tf.Tensor)]:
NameError: name 'Tuple' is not defined
WARNING:tensorflow:AutoGraph could not transform <function f.<locals>.local_func at 0x7f15f5ff3af0> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: name 'Tuple' is not defined
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
TensorFlow: 2.11.0
Converted call: <function f.<locals>.local_func at 0x7f15f5ff3af0>
    args: (<tf.Tensor 'x:0' shape=(5,) dtype=float32>,)
    kwargs: {}

<function f.<locals>.local_func at 0x7f15f5ff3af0> is not cached for subkey ConversionOptions[{}]
Source code of <function f.<locals>.local_func at 0x7f15f5ff3af0>:

@tf.function
def local_func(x: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]:
  while tf.reduce_sum(x) > 1:
    tf.print(x)
    x = tf.tanh(x)
  return x, x


Transformed <function f.<locals>.local_func at 0x7f15f5ff3af0>:

# coding=utf-8
def tf__local_func(x: tf.Tensor) -> Tuple[(tf.Tensor, tf.Tensor)]:
    with ag__.FunctionScope('local_func', 'fscope', ag__.ConversionOptions(recursive=True, user_requested=True, optional_features=(), internal_convert_user_code=True)) as fscope:
        do_return = False
        retval_ = ag__.UndefinedReturnValue()

        def get_state():
            return (x,)

        def set_state(vars_):
            nonlocal x
            (x,) = vars_

        def loop_body():
            nonlocal x
            ag__.converted_call(ag__.ld(tf).print, (ag__.ld(x),), None, fscope)
            x = ag__.converted_call(ag__.ld(tf).tanh, (ag__.ld(x),), None, fscope)

        def loop_test():
            return (ag__.converted_call(ag__.ld(tf).reduce_sum, (ag__.ld(x),), None, fscope) > 1)
        ag__.while_stmt(loop_test, loop_body, get_state, set_state, ('x',), {})
        try:
            do_return = True
            retval_ = (ag__.ld(x), ag__.ld(x))
        except:
            do_return = False
            raise
        return fscope.ret(retval_, do_return)

Error transforming entity <function f.<locals>.local_func at 0x7f15f5ff3af0>
WARNING: AutoGraph could not transform <function f.<locals>.local_func at 0x7f15f5ff3af0> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: name 'Tuple' is not defined
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
```