Skip to content

Infinite recursion error on deep copy AzureBackendConfig due to __getattr__ implementation #708

@jessetvogel

Description

@jessetvogel

The class AzureBackendConfig in azure.quantum.qiskit.backends.backend has a field metadata containing any remaining properties of the class. The __getattr__ method of this class is implemented in such a way that, if the attribute is not found, the metadata dict is checked. However, this leads to a RecursionError: maximum recursion depth exceeded when an instance of the class is copied using copy.deepcopy (which apparently happens sometimes).

The relevant piece of code:

class AzureBackendConfig:

    ...

    def __getattr__(self, name: str) -> Any:
        if name == "max_experiments":
            return 1
        try:
            return self.__dict__[name]
        except KeyError as exc:
            if name in self.metadata:
                return self.metadata[name]
            raise AttributeError(
                f"'{type(self).__name__}' object has no attribute '{name}'"
            ) from exc

Simplified reproduction of the error:

import copy
from dataclasses import dataclass, field
from typing import Any

@dataclass
class A:
    metadata: dict[str, Any] = field(default_factory=dict)

    def __getattr__(self, name: str) -> Any:
        try:
            return self.__dict__[name]
        except KeyError as exc:
            if name in self.metadata:
                return self.metadata[name]
            raise AttributeError(
                f"'{type(self).__name__}' object has no attribute '{name}'"
            ) from exc

a = A()

copy.deepcopy(a)  # RecursionError: maximum recursion depth exceeded

A possible solution would be to change the problematic line to:

if "metadata" in self.__dict__ and name in self.metadata:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions