As @jonrsharpe noted in a comment, this can be done with collections.abc.Callable:

from collections.abc import Callable

def my_function(func: Callable):

Note: Callable on its own is equivalent to Callable[..., Any]. Such a Callable takes any number and type of arguments (...) and returns a value of any type (Any). If this is too unconstrained, one may also specify the types of the input argument list and return type.

For example, given:

def sum(a: int, b: int) -> int: return a+b

The corresponding annotation is:

Callable[[int, int], int]

That is, the parameters are sub-scripted in the outer subscription with the return type as the second element in the outer subscription. In general:

Callable[[ParamType1, ParamType2, ..., ParamTypeN], ReturnType]
Answer from Dimitris Fasarakis Hilliard on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
1 week ago - The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc. This module provides runtime support for type hints.
Discussions

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
Why Type Hinting Sucks!
Counterpoint: idk wtf some of the variables are supposed to be in this repo I didn't create but must now maintain so even a superficial attempt at type hinting is appreciated More on reddit.com
🌐 r/Python
290
935
February 13, 2023
Type hinting functions with multipe return types including None
None should probably be included, but in case where it's either type_x or None, I feel I kinda like Optional[type_x] (from typing) instead . More on reddit.com
🌐 r/learnpython
11
1
August 12, 2022
Annotating args and kwargs in Python
You shouldn't use **kwargs in an API - APIs are boundaries, and boundaries should be as explicit as possible. You also shouldn't use *args, unless it's a simple varargs interface (like max(...)) or something with a clear definition (like str.format(...), and even then, I'm not completely on board). More on reddit.com
🌐 r/Python
33
105
January 9, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › type-hints-in-python
Type Hints in Python - GeeksforGeeks
May 3, 2025 - Type hints are a feature in Python that allow developers to annotate their code with expected types for variables and function arguments.
🌐
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.

🌐
Real Python
realpython.com › ref › glossary › type-hint
type hint | Python Glossary – Real Python
In Python, a type hint is a syntactic construct that allows you to indicate the expected data types of variables, function arguments, and return values. They provide a way to improve your code’s maintainability by explicitly declaring the ...
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-type-hinting-and-annotations-work-319d952247a6
How Python’s Type Hinting and Annotations Work | Medium
July 14, 2024 - It provides a way to indicate the expected data types of function arguments and return values, adding a layer of clarity to the code. Python remains a dynamically typed language, meaning that type checking is not enforced at runtime.
🌐
Open Water Foundation
learn.openwaterfoundation.org › owf-learn-python › lessons › type-hints › type-hints
Type Hints - OWF Learn Python
The build-in file type in Python requires the following type hint: from typing import TextIO def some_function(text_file_pointer: TypeIO) -> None: """ Example of passing a `file` type.
Find elsewhere
🌐
Ryan Himmelwright
ryan.himmelwright.net › post › python-type-hinting-intro
Intro to Python Type Hinting | λ ryan.himmelwright.net
To use type hinting in python, just specify the type when defining variables using the format var_name: type. Additionally, to define a return type for a function, add a -> symbol at the end of the function def pointing to the return type.
🌐
Molssi
education.molssi.org › type-hints-pydantic-tutorial › chapters › TypeHintsInPython.html
Type Hints in Python — Python Type Hints, Dataclasses, and Pydantic
Python allows the ability to annotate variables and outputs through the power of “Type Hints.” These augment the arguments and optionally the functional returns provide additional data about what types are expected for a particular argument.
🌐
Towards Data Science
towardsdatascience.com › home › data science › type hints in python
Type Hints in Python | Towards Data Science
July 11, 2023 - Type hints indicate the datatypes of both the inputs and outputs in functions (it applies to class methods as well). A problem many Python users complain about is the freedom we have to change a variable type.
🌐
Mypy
mypy.readthedocs.io › en › stable › cheat_sheet_py3.html
Type hints cheat sheet - mypy 2.4.0 documentation
# For most types, just use the name of the type in the annotation # Note that mypy can usually infer the type of a variable from its value, # so technically these annotations are redundant x: int = 1 x: float = 1.0 x: bool = True x: str = "test" x: bytes = b"test" # For collections, the type of the collection item is in brackets x: list[int] = [1] x: set[int] = {6, 7} # For mappings, we need the types of both keys and values x: dict[str, float] = {"field": 2.0} # For tuples of fixed size, we specify the types of all the elements x: tuple[int, str, float] = (3, "yes", 7.5) # For tuples of varia
🌐
Molssi
education.molssi.org › python-package-best-practices › 06-type-hinting.html
Type Hinting in Python — MolSSI Best Practices documentation
Above, we want to specify that ... The same syntax can be used to specify function inputs. We can add type hints to a function to specify what types the function works on....
🌐
Dagster
dagster.io › blog › python-type-hinting
Using Type Hinting in Python Projects
August 11, 2023 - You can provide type hints for function parameters and return values. This helps other developers understand what types of arguments are expected by the function and what type the function returns. def greet(name: str) -> str: \ return f"Hello, ...
🌐
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 - Type hints indicate that the data the function returns is a list of strings. Therefore the IDE “knows” that item should be a str in the final for loop since result is a list of strings.
🌐
Python
peps.python.org › pep-0484
PEP 484 – Type Hints - Python Enhancement Proposals
It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention. Any function without annotations should be treated as having the most general type possible, or ignored, by any type checker.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python type hints
Python Type Hints
April 2, 2025 - If a function doesn’t explicitly return a value, you can use None to type hint the return value.
🌐
EITCA
eitca.org › home › what is type hinting and how can it be used to specify the expected types of function parameters?
What is type hinting and how can it be used to specify the expected types of function parameters? - EITCA Academy
August 3, 2023 - To specify the expected types of function parameters using type hinting, you can use the colon (:) followed by the type annotation after the parameter name in the function definition. The type annotation can be any valid Python expression that evaluates to a type.
🌐
JetBrains
jetbrains.com › help › pycharm › type-hinting-in-product.html
Type hinting in PyCharm | PyCharm Documentation
August 14, 2026 - Place the caret at the function name, and press Alt+Enter. In the list of intention actions that opens, choose Specify return type using annotation. This feature is available only with Python 3. For comment-based type hints, PyCharm suggests an intention action that allows you to convert a comment-based type hint to a variable annotation.
🌐
Codefinity
codefinity.com › blog › A-Comprehensive-Guide-to-Python-Type-Hints
A Comprehensive Guide to Python Type Hints
Type hints in Python are a major ... language. They serve as a form of documentation that allows developers to explicitly state the expected data types of function arguments and return values....
🌐
Real Python
realpython.com › lessons › type-hinting
Type Hinting (Video) – Real Python
Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. Here’s an example of adding type information to a function.
Published: October 29, 2019