Skip to content

Comments

swev-id: pytest-dev__pytest-10051 keep caplog records in sync#8

Open
casey-brooks wants to merge 1 commit intopytest-dev__pytest-10051from
fix/caplog-clear-sync-10051
Open

swev-id: pytest-dev__pytest-10051 keep caplog records in sync#8
casey-brooks wants to merge 1 commit intopytest-dev__pytest-10051from
fix/caplog-clear-sync-10051

Conversation

@casey-brooks
Copy link

Summary

  • clear the handler records and stream in place via caplog.clear
  • add regression coverage for setup/call/teardown phase synchronization
  • cast FileReader return value to satisfy mypy 0.960

Testing

  • . .venv/bin/activate && pytest testing/logging/test_fixture.py
  • . .venv/bin/activate && pytest testing/logging

Linting

  • . .venv/bin/activate && PIP_EXTRA_INDEX_URL=file:///workspace/simple pre-commit run --files src/_pytest/logging.py testing/logging/test_fixture.py src/_pytest/assertion/rewrite.py
  • . .venv/bin/activate && rst-lint --encoding utf-8 RELEASING.rst README.rst TIDELIFT.rst

Note: tox -e linting currently fails because upstream rst-lint dropped support for the --encoding flag. Manual invocations above confirm the docs lint clean.

Reproduction

import logging


def test_clear_desync(caplog):
    logger = logging.getLogger(__name__)
    logger.warning("before clear")
    call_records = caplog.get_records("call")
    caplog.clear()
    logger.warning("after clear")
    assert call_records == caplog.records

Observed failure before this change:

E   AssertionError: assert ['before clear'] == ['after clear']

Fix

LogCaptureFixture.clear() now keeps the handler list and stream objects in place (clearing their contents instead of swapping them out). This preserves identity between caplog.records and caplog.get_records("call") mid-phase while leaving prior phase snapshots untouched. Tests cover both sync and phase separation. A small mypy cast keeps tox -e linting green on this branch.

@casey-brooks casey-brooks requested a review from a team December 25, 2025 19:55
@casey-brooks
Copy link
Author

Local Validation

  • . .venv/bin/activate && pytest testing/logging/test_fixture.py
    ⮑ 17 passed, 0 failed, 0 skipped
  • . .venv/bin/activate && pytest testing/logging
    ⮑ 61 passed, 0 failed, 0 skipped
  • . .venv/bin/activate && PIP_EXTRA_INDEX_URL=file:///workspace/simple pre-commit run --files src/_pytest/logging.py testing/logging/test_fixture.py src/_pytest/assertion/rewrite.py
    ⮑ hooks passed with no changes
  • . .venv/bin/activate && rst-lint --encoding utf-8 RELEASING.rst README.rst TIDELIFT.rst
    ⮑ no issues detected

tox -e linting still errors because the bundled rst-lint no longer accepts --encoding; manual invocations above cover the same surface.

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updates keep caplog's handler state in sync while clearing its content, and the new regression coverage plus the FileReader typing fix look solid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants