Search code examples
pythonfunctiontyping

Typing: NameError: name 'function' is not defined


While trying to specify the return type of a function that returns a list of functions, I am experiencing some difficulties. The typechecker retrurns:

Traceback (most recent call last):
  File "/home/name/anaconda/envs/snncompare/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/name/anaconda/envs/snncompare/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/name/git/Hiveminds/Apk-controller/src/apkcontroller/__main__.py", line 5, in <module>
    from src.apkcontroller.scripts.org_torproject_android import Apk_script
  File "/home/name/git/Hiveminds/Apk-controller/src/apkcontroller/scripts/org_torproject_android.py", line 8, in <module>
    from src.apkcontroller.Screen import Screen
  File "/home/name/git/Hiveminds/Apk-controller/src/apkcontroller/Screen.py", line 10, in <module>
    class Screen:
  File "/home/name/git/Hiveminds/Apk-controller/src/apkcontroller/Screen.py", line 105, in Screen
    ) -> List[function]:
NameError: name 'function' is not defined

For code:

  @typechecked
    def get_next_actions(
        self,
        required_objects: Dict[str, str],
        optional_objects: Dict[str, str],
    ) -> List[function]:
        """Looks at the required objects and optinoal objects and determines
        which actions to take next.

        An example of the next actions could be the following List:
        0. Select a textbox.
        1. Send some data to a textbox.
        2. Click on the/a "Next" button.

        Then the app goes to the next screen and waits a pre-determined
        amount, and optionally retries a pre-determined amount of attempts.

        TODO: determine how to put this unique function on the right node.
        """
        print(
            "TODO: determine how to specify how to compute the next action"
            + f" for this screen. {required_objects},{optional_objects}"
        )
        return [actions_1, actions_2]


@typechecked
def actions_1(d: AutomatorDevice) -> None:
    """Performs the actions in option 1 in this screen."""
    print(f"TODO: perform actions 1.{d}")


@typechecked
def actions_2(d: AutomatorDevice) -> None:
    """Performs the actions in option 2 in this screen."""
    print(f"TODO: perform actions 2.{d}")

Which is called with:

screen.get_next_actions(screen.required_objects,screen.optional_objects)

Question

How can I specify the return type of a function, as being a list of functions?

Approach I

I tried importing:

from typing import Function

However, that object does not exist in the typing library.


Solution

  • You'll want typing.Callable. (Functions aren't the only things that are callable.)

    from typing import Callable
    

    The type for a

    def actions_1(d: AutomatorDevice) -> None
    

    is

    Callable[[AutomatorDevice], None]
    

    and a list of them is naturally

    List[Callable[[AutomatorDevice], None]]
    

    (though you could go for Collection or Iterable too).