Skip to content

functools.update_wrapper copies all __dict__ attributes to the wrapper by default, which can be surprising when used on callable instances #105933

@purpledog

Description

@purpledog

Bug report

functools.update_wrapper silently remove dataclass function wrapper copies all attributes from wrapped callable to wrapper, which can lead to surprising behavior. In this case the fun attribute is copied, totally changing the behavior of the code:

Repro:

import dataclasses
import functools
from typing import Any, Callable

@dataclasses.dataclass()
class PlusOne:
  fun: Callable[..., int]

  def __post_init__(self):
    functools.update_wrapper(self, self.fun)
  
  def __call__(self, *args, **kwargs):
    return self.fun(*args, **kwargs) + 1

def identity(x):
  return x

print(PlusOne(identity)(0))  # 1
print(PlusOne(PlusOne(identity))(0))  # 1

Removing __post_init__ fixes the issue.

Your environment

Linux, 3.10.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions