Python Forum
change dataclass to frozen at runtime
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change dataclass to frozen at runtime
#1
I'm working with a dataclass that is code-generated by a tool I do not control:

@dataclass
class Concern:
    id: ConcernID
Note that the dataclass is not declared as frozen. I need this class to be frozen for the sake of Hashability, so I would like to modify the class at **runtime** to make it frozen. Is this possible?

thanks,

Joe
Reply
#2
I think you might have to create a new frozen version of the unfrozen dataclass at the most efficient point you can in your code.
Reply
#3
Could do someting like this.
from dataclasses import dataclass, make_dataclass, fields

class ConcernID:
    '''a example of how ConcernID class could be'''
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return self.value

    def __repr__(self):
        return f"ConcernID({self.value})"

@dataclass
class Concern:
    id: ConcernID

# Create a frozen version of the class at runtime
FrozenConcern = make_dataclass(
    'FrozenConcern',
    fields=[(f.name, f.type) for f in fields(Concern)],
    frozen=True
)

concern_id = ConcernID(123)
not_frozen = Concern(id=concern_id)
frozen_concern = FrozenConcern(id=concern_id)
If test will now frozen=True property prevents any changes to the instance attributes,ensuring immutability.
>>> not_frozen.id = 456
>>> not_frozen.id
456
>>> 
>>> frozen_concern.id = 456
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "<string>", line 4, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'id'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question how to type hint a function in a dataclass? Calab 3 765 Feb-27-2025, 04:40 AM
Last Post: Calab
  Extract args=, value that was passed to Multiprocessing.Proc object during runtime? haihal 1 627 Dec-08-2024, 07:04 AM
Last Post: Gribouillis
  class and runtime akbarza 4 1,588 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  append str to list in dataclass flash77 6 3,355 Mar-14-2024, 06:26 PM
Last Post: flash77
  painfully slow runtime when handling data dadazhu 3 2,028 Jan-20-2023, 07:11 PM
Last Post: snippsat
  Big O runtime nested for loop and append yarinsh 4 2,817 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,976 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  Object reference in Dict - not resolved at runtime benthomson 2 2,549 Apr-02-2020, 08:50 AM
Last Post: benthomson
  PyCharm asking for VC++ runtime 14.0 whereas I have already installed VC++ 19.0 SarmadiRizvi 1 2,471 Apr-02-2020, 06:17 AM
Last Post: snippsat
  Creating a set with dataclass and dict hobbitdur 11 15,221 Jan-16-2020, 11:22 AM
Last Post: buran

Forum Jump:

User Panel Messages

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