Skip to content

fix(logging): resolve qualified method name dynamically in method_logger - #4082

Merged
quaquel merged 4 commits into
mesa:mainfrom
priyanshux-dev:my-first-contribution
Nov 14, 2026
Merged

quaquel merged 4 commits into
mesa:mainfrom
priyanshux-dev:my-first-contribution

Conversation

@priyanshux-dev

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Resolve the qualified class and method name dynamically via func.__qualname__ inside method_logger() instead of static definition-time stack inspection.

Bug / Issue

Fixes #4069.

method_logger() in mesa/mesa_logging.py previously evaluated inspect.getouterframes(inspect.currentframe())[1][3] at decorator definition time. Because class bodies execute before the class object is constructed, this captured <module> (or an outer enclosing frame name) rather than the actual class name, and was fragile under nested scopes or composed decorators.

Implementation

  • Replace the static inspect.getouterframes() lookup in method_logger() (mesa/mesa_logging.py) with dynamic func.__qualname__ evaluation inside the wrapper function, preserving the existing args[1::] and kwargs log format.
  • Add a deterministic regression test (test_method_logger_logs_qualname) in tests/test_mesa_logging.py verifying that decorated class methods log their qualified class and method name without altering return values.

Testing

  • Before/after verification: test_method_logger_logs_qualname fails on unmodified main (logging <module>.step) and passes with this fix.
  • Logging test suite: pytest tests/test_mesa_logging.py passed locally.

Additional Notes

This change is strictly scoped to method_logger() formatting and does not alter create_module_logger(), function_logger(), or any logger handler/level configuration.

- Replace static inspect.getouterframes() lookup at decorator definition time with dynamic func.__qualname__ evaluation inside the wrapper.
- Prevent '<module>' or outer frame names from being logged when decorating class methods.
- Add unit test in tests/test_mesa_logging.py verifying qualified class and method name logging.
@github-actions github-actions Bot added the testing Release notes label label Nov 13, 2026
@mesa-triage-bot

Copy link
Copy Markdown

Thanks for adding a focused logging test, @priyanshux-dev. Running the #4069 reproducer against current source logs SampleAgent.step, rather than <module>.step: the decorator factory executes inside the class body. The new test checks the additional enclosing scope in __qualname__.

Could you update #4069 with the model or debugging workflow where the current name causes a problem, including a minimal example and its actual output? That will clarify which failure this PR should address.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@priyanshux-dev

Copy link
Copy Markdown
Contributor Author

Thanks for the note! I've updated #4069 with a minimal reproducer showing where static frame inspection (inspect.getouterframes(inspect.currentframe())[1][3]) fails in real debugging workflows:

  • Wrapper / shorthand decorator factories (e.g., def my_logger(fn): return method_logger("mesa")(fn)), which log my_logger.<method> instead of the class name.
  • Dynamic / post-class decoration (MyAgent.step = method_logger("mesa")(MyAgent.step)), which logs <module>.step.
  • Nested classes (Outer.Inner.step), which lose outer class qualification.

Replacing frame inspection with func.__qualname__ resolves all three while avoiding stack-frame inspection overhead.

@quaquel quaquel added the bug Release notes label label Nov 14, 2026
@quaquel
quaquel merged commit 86a34d8 into mesa:main Nov 14, 2026
17 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
…ger (#4082)

* fix(logging): resolve qualified method name dynamically in method_logger

- Replace static inspect.getouterframes() lookup at decorator definition time with dynamic func.__qualname__ evaluation inside the wrapper.
- Prevent '<module>' or outer frame names from being logged when decorating class methods.
- Add unit test in tests/test_mesa_logging.py verifying qualified class and method name logging.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* style(tests) : move method_logger import to top of file.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 86a34d8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants