[Bug] method_logger uses fragile definition-time inspect stack frame instead of func.__qualname__

Aperta Adatta ai principianti
#4,069 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Inizia leggendo method_logger() in mesa/mesa_logging.py e verifica come il suo wrapper costruisce il messaggio di log. Aggiorna il nome qualificato al momento della chiamata del metodo, come descritto nell’issue, poi esegui o aggiungi un test mirato per un metodo di classe decorato per confermare che l’output includa SampleAgent.step.

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

Descrizione

bug

Describe the bug
In mesa/mesa_logging.py, method_logger() attempts to capture the class name of a decorated method using inspect.getouterframes(inspect.currentframe())[1][3] at decorator definition time.

This has two main issues:

  1. Frame inspection at definition time evaluates before the class object is constructed, yielding <module> or outer context frame names instead of the actual class name.
  2. It is fragile to context nesting, decorator composition, or outer wrapper functions.

Expected behavior
method_logger() should dynamically capture and display the qualified class and method name (e.g., SampleAgent.step) when the decorated method is executed.

To Reproduce

from mesa.mesa_logging import method_logger

class SampleAgent:
    @method_logger("mesa")
    def step(self):
        pass

agent = SampleAgent()
agent.step()
# Expected log output format: calling SampleAgent.step with ...
# Actual behavior: inspect evaluates frame at definition time yielding <module>

Proposed Fix
Leverage Python's standard func.qualname inside the wrapper function execution rather than static stack frames at definition time:

def method_logger(name: str):
    logger = get_module_logger(name)

    def real_decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            qualname = func.__qualname__
            logger.debug(
                f"calling {qualname} with {args[1:]} and {kwargs}"
            )
            res = func(*args, **kwargs)
            return res

        return wrapper

    return real_decorator

Additional context
I am a first-time contributor and would love to open a Pull Request to fix this! Could you please assign this issue to me? 🙋‍♂️

Lingua principale
Python
Stelle
3.9k
Fork
1.4k
Merge medio
4g 8h
PR unite (30g)
44

Preparare l'ambiente

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 mesa/mesa

Tutte le issue di mesa/mesa

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.