Skip to content

Robustness: Defensive NoneType handling during recordset or connection iteration #1512

Description

@UsmanGhias

First, thank you so much for maintaining loguru. It's an indispensable tool in our stack, and I really appreciate the care put into its design.

Summary

While working with custom sinks and handling edge cases where a record or connection object might be uninitialized or return None, I noticed that certain serialization or iteration flows can trigger an unhandled AttributeError. Adding a light defensive check for NoneType would make custom sink integrations a bit more resilient and yield a clearer error message.

Environment

  • Python version: 3.12.1
  • Operating System: Ubuntu 22.04
  • Loguru version: 0.7.2 (current main)

Steps to Reproduce

from loguru import logger

def awkward_sink(message):
    # Simulated edge case where record metadata might be empty/None
    record = message.record
    record["extra"] = None
    
    # Attempting to iterate or serialize when an inner attribute is None
    for _ in record["extra"]:
        pass

logger.add(awkward_sink)
logger.info("Triggering sink")

Expected Behavior

Loguru handles the edge case gracefully or raises a descriptive, context-aware error explaining that the target object is uninitialized or None.

Actual Behavior

An unhandled AttributeError: 'NoneType' object is not iterable bubbles up from deep within the handling/formatting sequence rather than a clean loguru-specific error.

Happy to submit a quick PR for this if the maintainers are open to it!

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions