Python Forum
how to type hint a function in a dataclass?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to type hint a function in a dataclass?
#1
Question 
I wish to create a dataclass where one of the attributes will hold a function. It could be any function, so I won't know what parameters it will accept when the dataclass is used.

I see that python doesn't do strict typing, so the following code will work, but it technically is not correct. What is the correct way to declare my task attribute to be a function?

from dataclasses import dataclass

@dataclass
class Schedule:
    name: str
    task: str       # <--- what should this be?
    interval: int
    
    
def hello(name=None):
    print(f"Hello {name}!")


def hello2(name, age):
    print(f"Hello {name}! You are {age} years old.")
    

job1 = Schedule("Greeter", hello, 5)
print(job1)
print(job1.name)
job1.task("Bob")

job2 = Schedule("Age", hello2, 1)
job2.task("Bob", 22)
Reply
#2
from collections.abc import Callable
from dataclasses import dataclass


@dataclass
class Schedule:
    name: str
    task: Callable
    interval: int
Or if you want to be more specific you can type task as a function that takes a str and returns None
@dataclass
class Schedule:
    name: str
    task: Callable[[str], None]
    interval: int
Calab likes this post
Reply
#3
Since you don't know the exact signature of the function beforehand,
you can specify it to accept any parameters and return any type using Callable[..., Any]
from dataclasses import dataclass
from typing import Callable, Any

@dataclass
class Schedule:
    name: str
    # Correct type for a function with any signature
    task: Callable[..., Any]
    interval: int

def hello(name=None):
    print(f"Hello {name}!")

def hello2(name, age):
    print(f"Hello {name}! You are {age} years old.")

job1 = Schedule("Greeter", hello, 5)
print(job1)
print(job1.name)
job1.task("Bob")

job2 = Schedule("Age", hello2, 1)
job2.task("Bob", 22)
Calab and Larz60+ like this post
Reply
#4
Thanks for the replies guys... For some reason I'm not getting email notifications when someone replies.

These are good answers, but my methodology wasn't sound. I need to pass this data to Ray functions and these "callable" variables won't pickle.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  change dataclass to frozen at runtime jpanico 2 1,145 Oct-06-2024, 09:38 AM
Last Post: snippsat
  How can I create this type hint. deanhystad 0 592 Aug-05-2024, 07:55 PM
Last Post: deanhystad
  Is changing processes by using match with type function impossible? cametan 1 721 May-30-2024, 02:16 PM
Last Post: cametan
  append str to list in dataclass flash77 6 3,410 Mar-14-2024, 06:26 PM
Last Post: flash77
  determine parameter type in definition function akbarza 1 1,246 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  i want to use type= as a function/method keyword argument Skaperen 9 3,606 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  match type with value in csv parsing function vinci 2 2,133 Jan-21-2022, 12:19 PM
Last Post: Larz60+
  Import a module for use in type hint? Milosz 0 2,043 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Bug ? when dataclass field name == field type Cyril 0 2,026 Oct-22-2020, 03:26 AM
Last Post: Cyril
  Creating function inside classes using type wolfmansbrother 3 3,274 Mar-20-2020, 01:21 AM
Last Post: wolfmansbrother

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020