AutoGraph did convert this function: NameError: name 'Tuple' is not defined
Hi @albertz, thank you for reporting the issue.
@tilakrayal, I was able to replicate the issue in colab using TF v2.11 and tf-nightly(2.13.0-dev20230228). Please find the gists for the same  [here-TF v2.11](https://colab.sandbox.google.com/gist/synandi/e0493a9e1179200485969ff31ed61354/59796.ipynb) and [here-tf-nightly](https://colab.sandbox.google.com/gist/synandi/f78dec08ce6f12d5fe0415849058418d/59796_nightly.ipynb). Thanks!   Hi @albertz ,
It seems we need to import Tuple outside the function.Please refer to the attached [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/06436d52cd7bbc2c00baaa63c4e19c00/59796_r1.ipynb).
By importing the Tuple at Global level  it is working fine. I know.

But this is a bug. This should but be necessary.
 Hi @albertz ,

You need to wrap the entire function `f() ` with `tf.function` decorator and inside that you can import Tuple and make it workable.This is applicable for both 1.x and 2.x versions.Please refer attached [gist](https://colab.research.google.com/gist/SuryanarayanaY/8318af1219eeda9d47176aba9887ee45/59796_r2-2-x-v.ipynb).

Whenever we call `f()` with some input,as it is having `local_func` under `tf.function` decorator and `tf.function` when called will have two stages normally.First stage is tracing for creation of tf.Graph and the second stage is execution of graph(only TF operations).

If we enclose the complete function under tf.function then this works as intended.

 In my case, I cannot do that. The outer function must be a regular (non-TF) Python function.

But in any case, this is still a TF bug. It should not be necessary to use such workarounds.

<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
```
