microsoft / microsoft/azure-quantum-python
Infinite recursion error on deep copy `AzureBackendConfig` due to __getattr__ implementation
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).
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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