Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show the code that is generated when using @dataclass class decorator?

Python 3.7 introduces the dataclasses module that contains a @dataclass decorator. This decorator can generate class functions. How can I print these generated functions?

like image 320
OrangeTux Avatar asked Nov 19 '18 20:11

OrangeTux


People also ask

What is __ Post_init __ Python?

The __post_init__ method is called just after initialization. In other words, it is called after the object receives values for its fields, such as name , continent , population , and official_lang .

What does @dataclass do in Python?

The dataclass allows you to define classes with less code and more functionality out of the box. This Person class has the __init__ method that initializes the name and age attributes. If you want to have a string representation of the Person object, you need to implement the __str__ or __repr__ method.

What does the Dataclass decorator do?

This function is a decorator that is used to add generated special methods to classes, as described below. The dataclass() decorator examines the class to find field s. A field is defined as a class variable that has a type annotation.

Does Python 3.6 have Dataclasses?

Dataclasses, introduced in Python 3.7 (and backported to Python 3.6), provide a handy way to make classes less verbose. Many of the common things you do in a class, like instantiating properties from the arguments passed to the class, can be reduced to a few basic instructions.


2 Answers

After inspecting the dataclass implementation, the methods seems to be generated by a dataclasses._create_fn. To get the original generated code, I mocked the function as:

import dataclasses

_original_create_fn = dataclasses._create_fn

def _new_create_fn(name, args, body, **kwargs):
    args_str = ', '.join(args)
    body_str = '\n'.join('  ' + l for l in body)
    print(f'def {name}({args_str}):\n{body_str}\n')
    return _original_create_fn(name, args, body, **kwargs)

dataclasses._create_fn = _new_create_fn


# After the dataclasses as been mocked, creating new dataclass
# will display their source code
@dataclasses.dataclass
class A:
    x: int
    y: int

Which display something like:

def __init__(self, x:_type_x, y:_type_y):
  self.x=x
  self.y=y

def __repr__(self):
  return self.__class__.__qualname__ + f"(x={self.x!r}, y={self.y!r})"

def __eq__(self, other):
  if other.__class__ is self.__class__:
   return (self.x,self.y,)==(other.x,other.y,)
  return NotImplemented
like image 146
Conchylicultor Avatar answered Oct 23 '22 03:10

Conchylicultor


I asked the same question myself. Dataclasses part project should have the verbose option, but is not.

Found out this video, helpful. According to the video dataclasses.py is a code generator. So this should be our first idea how to get the code.


I tried this code:

from dataclasses import dataclass
import inspect
import os
from uncompyle6 import PYTHON_VERSION, deparse_code2str

@dataclass
class C:
    name: str
    value: int = 34

inspect.getmembers(C) #('__init__', <function __init__(self, name: str, value: int = 34) -> None>)

co= C.__init__.__code__ # let's find out code for the __init__ method from code object

code = deparse_code2str(
                code=co,
                version=PYTHON_VERSION,
                out=open(os.devnull, "w"))
print(code)

Will print

self.name = name
self.value = value

The code acutally uses the inspector to understand the class, and then to decompile the methods using Python decompiler.

Here are the methods discovered:

def __eq__(self, other):
    if other.__class__ is self.__class__:
        return (self.name, self.value) == (
            other.name,
            other.value,
        )
    else:
        return NotImplemented

def __init__(self, name: str, value: int = 34) -> None:
    self.name = name
    self.value = value

def __repr__(self):
    key = (id(self), _thread.get_ident())
    if key in repr_running:
        return "..."
    else:
        repr_running.add(key)
        try:
            result = user_function(self)
        finally:
            repr_running.discard(key)

        return result

There is actually a project that is doing @dataclass discovery. I installed it and it worked.

from dataclasses import dataclass
import inspect
import os
import dis
from DataclassInspector.inspector import Inspector

@dataclass
class C:
    name: str
    value: int = 34

inspected = Inspector(C)
print(inspected._generate_code())

Provided the output like this:

from dataclasses import Field, _MISSING_TYPE, _DataclassParams
class C:
    __dataclass_fields__ = {'name': "Field(name='name', type=str, default=_MISSING_TYPE, default_factory=_MISSING_TYPE, init=True, repr=True, hash=None, compare=True, metadata={}, _field_type=_FIELD)", 'value': "Field(name='value', type=int, default=34, default_factory=_MISSING_TYPE, init=True, repr=True, hash=None, compare=True, metadata={}, _field_type=_FIELD)"}
    __dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)
    name: str
    value: int = 34

    def __eq__(self, other):
        if other.__class__ is self.__class__:
            return (self.name, self.value) == (other.name, other.value)
        else:
            return NotImplemented


    __hash__ = None

    def __init__(self, name: str, value: int = 34) -> None:
        self.name = name
        self.value = value



    def __repr__(self):
        key = (
         id(self), _thread.get_ident())
        if key in repr_running:
            return '...'
        else:
            repr_running.add(key)
            try:
                result = user_function(self)
            finally:
                repr_running.discard(key)

            return result
like image 40
prosti Avatar answered Oct 23 '22 04:10

prosti