microsoft / microsoft/azure-quantum-python

Infinite recursion error on deep copy `AzureBackendConfig` due to __getattr__ implementation

Open Beginner friendly
#708 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
160
Forks
113
Avg merge
6d 23h
Merged PRs (30d)
1

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in azure-quantum/azure/quantum/qiskit/backends/backend.py at AzureBackendConfig.getattr, then run the deepcopy reproduction from the issue. Verify that copying an instance with empty metadata no longer raises RecursionError, while missing attributes still raise AttributeError and metadata-backed attributes continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.