Skip to content

Fix inverted assertion in remove_handler#46227

Merged
Rocketknight1 merged 1 commit into
huggingface:mainfrom
SebTardif:fix/inverted-remove-handler-assert
May 28, 2026
Merged

Fix inverted assertion in remove_handler#46227
Rocketknight1 merged 1 commit into
huggingface:mainfrom
SebTardif:fix/inverted-remove-handler-assert

Conversation

@SebTardif
Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes an inverted assertion in remove_handler() in src/transformers/utils/logging.py.

The current assert checks handler not in _get_library_root_logger().handlers, which raises AssertionError when calling remove_handler with a registered handler (the valid use case), while silently passing for unregistered handlers (the invalid use case). This one-word fix inverts the condition to correctly validate that the handler exists before removal.

Bug introduced in: #10633 (2021-03-10)
Previously reported in: #21506 (never fixed)

Before

assert handler is not None and handler not in _get_library_root_logger().handlers
  • remove_handler(registered_handler) raises AssertionError
  • remove_handler(unknown_handler) passes silently

After

assert handler is not None and handler in _get_library_root_logger().handlers
  • remove_handler(registered_handler) succeeds
  • remove_handler(unknown_handler) raises AssertionError

Fixes #21506

The assert condition in remove_handler checks 'handler not in
handlers', which raises AssertionError when removing a registered
handler and silently passes for unregistered ones. Invert the
condition to correctly validate that the handler exists before
removal.

Fixes huggingface#21506

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Copy link
Copy Markdown
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

Yeah this seems obviously correct. Not sure why it hasn't surfaced already, but I guess the path is just rarely used!

@Rocketknight1 Rocketknight1 enabled auto-merge May 28, 2026 12:46
@Rocketknight1 Rocketknight1 added this pull request to the merge queue May 28, 2026
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Merged via the queue into huggingface:main with commit a8a01e5 May 28, 2026
31 checks passed
yuchenxie4645 pushed a commit to yuchenxie4645/transformers that referenced this pull request May 28, 2026
The assert condition in remove_handler checks 'handler not in
handlers', which raises AssertionError when removing a registered
handler and silently passes for unregistered ones. Invert the
condition to correctly validate that the handler exists before
removal.

Fixes huggingface#21506

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
kashif pushed a commit to kashif/transformers that referenced this pull request Jun 1, 2026
The assert condition in remove_handler checks 'handler not in
handlers', which raises AssertionError when removing a registered
handler and silently passes for unregistered ones. Invert the
condition to correctly validate that the handler exists before
removal.

Fixes huggingface#21506

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
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.

A possible bug in the transformers logging file

3 participants