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

Aperta Adatta ai principianti
#708 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
azure, python
Ambito
backend

Direzione di ricerca

Inizia in azure-quantum/azure/quantum/qiskit/backends/backend.py, in AzureBackendConfig.getattr, quindi esegui la riproduzione di deepcopy dall’issue. Verifica che la copia di un’istanza con metadati vuoti non generi più RecursionError, mentre gli attributi mancanti continuino a generare AttributeError e gli attributi basati sui metadati continuino a funzionare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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:
Lingua principale
Python
Stelle
160
Fork
113
Merge medio
6g 23h
PR unite (30g)
1

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/azure-quantum-python

Tutte le issue di microsoft/azure-quantum-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.