You can use the typing module for Callable annotations.

The Callable annotation is supplied a list of argument types and a return type:

from typing import Callable

def func_b(func: Callable[[int], int]) -> int:
    return func(3)
Answer from Alex on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
1 week ago - The last parameter to Concatenate must be a ParamSpec or ellipsis (...). For example, to annotate a decorator with_lock which provides a threading.Lock to the decorated function, Concatenate can be used to indicate that with_lock expects a callable which takes in a Lock as the first argument, and returns a callable with a different type signature.
Discussions

Type annotation for argument that is a function
You can use typing.Callable: from typing import Callable def foo() -> str: return "Hello, world!" def bar(func: Callable[[], str]) -> str: return f"And I say: '{func()}'" Usage: Callable[[argument_types], return_type] More on reddit.com
🌐 r/learnpython
8
3
May 1, 2019
python - How can I specify the function type in my type hints? - Stack Overflow
How can I specify the type hint of a variable as a function type? There is no typing.Function, and I could not find anything in the relevant PEP, PEP 483. More on stackoverflow.com
🌐 stackoverflow.com
What is the point of type hinting when Python doesn't even respect it!?
because its a hint More on reddit.com
🌐 r/learnpython
26
0
August 15, 2024
Do you use static type hints in your code?
The older I get, the more I want static types. I like that the hints are optional, but I'd like them to be enforced if I do add them. More on reddit.com
🌐 r/Python
217
180
December 1, 2017
🌐
Python
peps.python.org › pep-0484
PEP 484 – Type Hints | peps.python.org
For all these reasons, square brackets (e.g. List[int]) are (and have long been) the preferred syntax for generic type parameters. They can be implemented by defining the __getitem__() method on the metaclass, and no new syntax is required at all. This option works in all recent versions of Python (starting with Python 2.2). Python is not alone in this syntactic choice – generic classes in Scala also use square brackets. One line of argument points out that PEP 3107 explicitly supports the use of arbitrary expressions in function annotations.
🌐
Reddit
reddit.com › r/learnpython › type annotation for argument that is a function
r/learnpython on Reddit: Type annotation for argument that is a function
May 1, 2019 -

I built a wrapper for an internal backups tools in my company so I can save my team the hassle of setting everything up.

Recently I found out that the tool didn't play well with some characters in the filename of a legacy system that I am not allowed to modify.

I figured I would allow the user to optionally pass in a function that manipulates the basename of file in whichever way they wanted, as long as that function returned a string. I want the function to be called

function(base_key, *function_args) 

But mypy seems to dislike this, giving me the error:

Error: List or tuple expected as variable arguments

How should I annotate this code?

I am having trouble googling my problem, I get useless results about stuff before the typing module, or stuff about how to annotate functions, not how to annotate arguments that are functions. Help please?

Full method code:

def backup(self, file_list: List[str], prefix: str = '', sufix: str = '', 
           function: Optional[Callable[..., str]] = None, 
           function_args: Optional[List[Any]] = []) -> None:
    self.sufix = sufix
    self.prefix = prefix

    for filename in file_list:
        base_key = os.path.basename(filename)
        if function:
            
            base_key = function(base_key, *function_args) 

        key = self.prefix + base_key + self.sufix
    self.upload(filename, self.storage_unit, key)
🌐
CodeBasics
code-basics.com › programming › python course › type annotations
Type Annotations | Python | CodeBasics
[Python] — Type Annotations — **Type annotations** are an ability to specify parameter types and return values for functions in Python. This is not a mandatorType annotations allow programmers to specify parameter ty...
🌐
Medium
leapcell.medium.com › explaining-python-type-annotations-a-comprehensive-guide-to-the-typing-module-87d9e3599d59
Explaining Python Type Annotations: A Comprehensive Guide to the typing Module | by Leapcell | Medium
March 12, 2025 - In this process_numbers function, the numbers parameter is annotated as List[int], which clearly indicates that numbers must be a list containing integers. The return value type of the function is annotated as int, that is, the function will return an integer.
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Functions › TypeAnnotations.html
12.7. Type Annotations — Foundations of Python Programming
A type annotation, sometimes called a type hint, is an optional notation that specifies the type of a parameter or function result. It tells the programmer using the function what kind of data to pass to the function, and what kind of data to expect when the function returns a value.
Find elsewhere
🌐
Real Python
realpython.com › ref › glossary › function-annotation
function annotation | Python Glossary – Real Python
A way to attach type information or type hints to function arguments and return values.
🌐
LogRocket
blog.logrocket.com › home › understanding type annotation in python
Understanding type annotation in Python - LogRocket Blog
June 4, 2024 - To annotate return value type, add -> immediately after closing the parameter parentheses, just before the function definition colon(:): def announcement(language: str, version: float) -> str: ... The function now has type hints showing that ...
🌐
The Python Coding Book
thepythoncodingbook.com › home › blog › using type hints when defining a python function [intermediate python functions series #6]
Using type hints when defining a Python function
March 19, 2023 - The type hint follows immediately after the parameter name and a colon. The function’s signature shows str as the data type annotation for person and int as the annotation for number.
🌐
FastAPI
fastapi.tiangolo.com › python-types
Python Types Intro - FastAPI
Python itself doesn't do anything with this Annotated. And for editors and other tools, the type is still str. But you can use this space in Annotated to provide FastAPI with additional metadata about how you want your application to behave. The important thing to remember is that the first type parameter you pass to Annotated is the actual type.
🌐
GitHub
github.com › python › typing › discussions › 1501
Annotating a function with passthrough kwargs · python/typing · Discussion #1501
Hello, I have a function which has no parameters other than kwargs which are to be passed onto a third party library call. Here is a simplified example def third_party_func(*, a: int, b: int): retu...
Author   python
🌐
Python
typing.python.org › en › latest › spec › annotations.html
Type annotations — typing documentation
In its basic form, type hinting is used by filling function annotation slots with classes: def greeting(name: str) -> str: return 'Hello ' + name · This states that the expected type of the name argument is str. Analogically, the expected return type is str. Expressions whose type is assignable to a specific argument type are also accepted for that argument.
🌐
GeeksforGeeks
geeksforgeeks.org › python › function-annotations-python
Function Annotations in Python - GeeksforGeeks
May 10, 2019 - Purpose of function annotations: The benefits from function annotations can only be reaped via third party libraries. The type of benefits depends upon the type of the library, for example · Python supports dynamic typing and hence no module is provided for type checking. Annotations like · [def foo(a:”int”, b:”float”=5.0) -> ”int”] (syntax described in detail in the next section) can be used to collect information about the type of the parameters and the return type of the function to keep track of the type change occurring in the function.
🌐
Tutorialspoint
tutorialspoint.com › home › python › python function annotations
Python Function Annotations
February 21, 2009 - Annotation is mentioned as an expression after putting a colon in front of the argument. Remember that Python is a dynamically typed language, and doesn't enforce any type checking at runtime.
🌐
GitHub
github.com › python › typing › discussions › 1163
How to annotate modifications for function arguments with `ParamSpec` and `TypeVar` · python/typing · Discussion #1163
I want to be able to annotate something that takes a function and creates a new one that alters the argument types it accepts. A quick example, using Ray: import ray ray.init() @ray.remote def do_t...
Author   python
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-type-hinting-and-annotations-work-319d952247a6
How Python’s Type Hinting and Annotations Work | Medium
July 14, 2024 - This attribute is a dictionary that maps variable names to their respective types. ... Here, the __annotations__ dictionary contains the type hints for the function's parameters a and b, as well as its return type.
🌐
Dagster
dagster.io › blog › python-type-hinting
Using Type Hinting in Python Projects
In Python, there is a distinction between atomic and composite types when it comes to type hinting. Atomic types, such as int, float, and str, are simple and indivisible, and their type annotations can be provided directly using the type itself, like str. def my_function(my_string: str) -> int: return len(my_string)
🌐
Reddit
reddit.com › r/learnpython › what is the point of type hinting when python doesn't even respect it!?
r/learnpython on Reddit: What is the point of type hinting when Python doesn't even respect it!?
August 15, 2024 -

So infuriating because I feel like my function is lying to me when it ends up letting other objects through the parameter.

Now every function I created needs an instance check which is unintuitive, verbose, and easily forgotten.

def wtf(string: str, integer:int):
   return 'TYPE HINT DOESNT DO ANYTHING' 

print( wtf( 123, 'wtf')  )
>> 'TYPE HINT DOESNT DO ANYTHING'

EDIT:

Turns out it is a noob mistake. Type hint only functions as a signal for IDE, but developers get to do whatever they want to your parameter.

If you really must enforce it, you got to have the line

if not isinstance(string, str): 
  raise TypeError()

Still, I don't like that this behavior isn't taught until it happens. So much time wasted on debugging production code when you take for granted your function is accepting only restricted data when it doesn't.