I would use types.FunctionType to represent a function:

>>> import types
>>> types.FunctionType
<class 'function'>
>>>
>>> def func():
...     pass
...
>>> type(func)
<class 'function'>
>>> isinstance(func, types.FunctionType)
True
>>>

You could also use a string literal such as 'function', but it looks like you want an actual type object.

Answer from user2555451 on Stack Overflow
🌐
Reddit
reddit.com › r/programminglanguages › type syntax/hint to specify a function pointer type with a return type to itself
r/ProgrammingLanguages on Reddit: Type syntax/hint to specify a function pointer type with a return type to itself
December 29, 2022 -

Anyone knows of a PL in which a variable can be declared/defined with a function pointer type, the function pointer type having a return type that is the function pointer type itself?

In C, it would look something like:

typedef X (*workflow_step)();

What I am looking for is that X is a function pointer to the type being defined by the typedef.

How is this syntax problem even called? Anyone knows?

I have looked into C, python (callable type hint), rust, D, F#... And no luck. They all seem to lack this.

Discussions

ctypes POINTER(c_type) is not allowed as function arg type
Are you reporting a bug, or opening a feature request? This looks like a bug in Mypy. Probably related to #5144. Here is my usecase: I wrote a small wrapper to declare foreign function signature wi... More on github.com
🌐 github.com
4
September 20, 2019
Implement __class_getitem__ for ctypes.pointer - Ideas - Discussions on Python.org
Proposal Make ctypes.pointer support __class_getitem__. As it is currently a function, we can change it to a class, with __new__ implementing the current features of the function. This should not affect existing behavior significantly. We could make pointer[...] return a GenericAlias as usual ... More on discuss.python.org
🌐 discuss.python.org
1
January 8, 2023
How to specify the type signature of funtion pointers to jitclass member function
(Previous post was hidden and still not released after several days, so try to post again. Will remove that one if released.) Basically what I’m trying to do is to have a function pointer that points to different member functions according to some argument. More on numba.discourse.group
🌐 numba.discourse.group
10
0
October 3, 2023
How to pass a python function to a C function as a function pointer in Cython
Hi all, I am writing a cython class like this. cdef class Button: cdef CButton* ptr cdef object _onClick def __cinit__(self, ...): self.ptr = make_btn_ptr_in_C() @onClick.setter def onClick(self, pfunc): self._onClick = pfunc setClickHandler(self.ptr, self._onClick) This setClickHandler ... More on discuss.python.org
🌐 discuss.python.org
18
0
February 7, 2024
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
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.
🌐
Real Python
realpython.com › pointers-in-python
Pointers in Python: What's the Point? – Real Python
March 18, 2026 - Because you defined add_one() in this shared object, you can access it as if it were any other Python object. Before you call the function though, you should specify the function signature. This helps Python ensure that you pass the right type to the function. In this case, the function signature is a pointer to an integer.
🌐
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.
🌐
GitHub
github.com › python › mypy › issues › 7540
ctypes POINTER(c_type) is not allowed as function arg type · Issue #7540 · python/mypy
September 20, 2019 - This requires python3.7 or newer and works fine but mypy does not like my val variable annotation. Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro. ... # Naive approach @func(mylib) def MyFuncOne(id: ctypes.c_uint32, val: ctypes.POINTER(ctypes.c_uint32) -> ctypes.uint32: ... # Try to use variable, since POINTER is declared in typeshed as # (type: Type[_CT]) -> Type[pointer[_CT]]` PTR_UINT32 = ctypes.POINTER(ctypes.c_uint32) @func(mylib) def MyFuncTwo(id: ctypes.c_uint32, val: PTR_UINT32) -> ctypes.uint32: ...
Author: python
🌐
GeeksforGeeks
geeksforgeeks.org › turning-a-function-pointer-to-callable
Turning a Function Pointer to Callable | GeeksforGeeks
April 2, 2019 - The code below uses the llvmpy extension to make an assembly function, obtain a function pointer to it, and turn it into a Python callable. Code #4 : ... from llvm.core import Module, Function, Type, Builder mod = Module.new('example') f = Function.new(mod, Type.function( Type.double(), [Type.double(), Type.double()], False), 'foo') block = f.append_basic_block('entry') builder = Builder.new(block) x2 = builder.fmul(f.args[0], f.args[0]) y2 = builder.fmul(f.args[1], f.args[1]) r = builder.fadd(x2, y2) builder.ret(r) Output :
🌐
Python.org
discuss.python.org › ideas
Implement __class_getitem__ for ctypes.pointer - Ideas - Discussions on Python.org
January 8, 2023 - Proposal Make ctypes.pointer support __class_getitem__. As it is currently a function, we can change it to a class, with __new__ implementing the current features of the function. This should not affect existing behavior…
Find elsewhere
🌐
Numba Discussion
numba.discourse.group › support: how do i do ...?
How to specify the type signature of funtion pointers to jitclass member function - Support: How do I do ...? - Numba Discussion
October 3, 2023 - (Previous post was hidden and still not released after several days, so try to post again. Will remove that one if released.) Basically what I’m trying to do is to have a function pointer that points to different member functions according to some argument.
🌐
Python.org
discuss.python.org › python help
How to pass a python function to a C function as a function pointer in Cython - Python Help - Discussions on Python.org
February 7, 2024 - Hi all, I am writing a cython class like this. cdef class Button: cdef CButton* ptr cdef object _onClick def __cinit__(self, ...): self.ptr = make_btn_ptr_in_C() @onClick.setter def on…
🌐
Codegive
codegive.com › blog › function_pointers_in_python.php
Function pointers in python
This provides compile-time safety. Python: Functions are dynamically typed. A variable can hold a reference to any function (or callable). Type hinting (Callable) helps add static type checking for readability and tooling, but it doesn't enforce types at runtime in the same way C++ does.
🌐
Real Python
realpython.com › ref › glossary › type-hint
type hint | Python Glossary – Real Python
A syntactic construct that allows you to indicate the expected data types of variables, function arguments, and return values.
🌐
Stack Overflow
stackoverflow.com › questions › 77619149 › python-ctypes-pointer-type-hinting
Python: ctypes _Pointer type hinting - Stack Overflow
December 7, 2023 - from ctypes import * from ctypes import _Pointer from typing import TypeVar, Callable T = TypeVar('T') def foreach(array: _Pointer[T], cond: Callable[[T], bool]): i = 0 v: T = array[i] while cond(v): yield v i += 1 v = array[i] # Example c_array = ... # _Pointer[int] for i in foreach(c_array, lambda i: i < 10): print(i) # 'i' should be recognized as 'int', not 'Any' The function works as intented and my IDE recognizes the types.
🌐
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.
🌐
Google Groups
groups.google.com › g › cython-users › c › 0ouYUUa60R4
passing a pointer function to another function which can be called in python
September 24, 2017 - In that case, ars.normal would already be a wrapper of the function pointer, so in that case you'd need to write your function that is supposed to receive the function to accept a wrapper of that type and you'd have to figure out a way to access the pointer. Note that, if you go this route, your type-safety is entirely based on your ars.normal.argtypes definition. You should probably declare the return type somewhere as well. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... Sorry again for my posts here: I ended up writing the following python function (wrapper) in order to get access to my "initial" and "sample" C-type functions: def py_ars(int ns, int m, double emax,
🌐
Reddit
reddit.com › r/python › what is too much type hinting for you?
r/Python on Reddit: What is too much type hinting for you?
July 27, 2024 -

For me it's :

from typing import Self

class Foo:
    def __init__(self: Self) -> None:
        ...

The second example is acceptable in my opinion, as the parameter are one type and the type hint for the actual attributes is for their entire lifetimes within the instance :

class Foo:
    def __init__(self, par1: int, par2: tuple[float, float]):
        self.par1: int = par1
        self.par2: tuple[float, float] | None = par2

Edit: changed the method in the first example from bar to __init__

🌐
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.