Infinite recursion error on deep copy `AzureBackendConfig` due to __getattr__ implementation
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
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.
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:
- Dominant language
- Python
- Stars
- 160
- Forks
- 113
- Avg merge
- 6d 23h
- Merged PRs (30d)
- 1
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.
More from microsoft/azure-quantum-python
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
-
Export types Open
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
microsoft/azure-quantum-python#650 · 1 comment ·
-
azure-quantum
Difficulty 2/5 1-3 hours Newbie friendliness 20/100
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
microsoft/azure-quantum-python#416 · 3 comments ·
All issues in microsoft/azure-quantum-python
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
syfoud/Simulated_Scepter#172 ·
-
A cancelled tests run makes the coverage comment workflow fail and reports it as a red check on main Openarea: ci bug perceived difficulty: 3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Nitjsefnie-Harness-Commons/daedalus#921 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
EleutherAI/lm-evaluation-harness#4207 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/clickhouse-connect#1057 ·