Fix unit tests CosmosRichLogger crash on None msg and test pollution#2540
Merged
Conversation
CosmosRichLogger.handle() crashed with TypeError when record.msg was None (e.g. logging a None return value). Guard against this by checking for None and converting to str. Add an autouse fixture in conftest.py that demotes any CosmosRichLogger instances back to logging.Logger after each test. This prevents test_log.py (which monkeypatches rich_logging=True) from permanently polluting the logging cache with CosmosRichLogger instances that add the (astronomer-cosmos) prefix to subsequent tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2540 +/- ##
=======================================
Coverage 98.08% 98.08%
=======================================
Files 103 103
Lines 7482 7483 +1
=======================================
+ Hits 7339 7340 +1
Misses 143 143 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
CosmosRichLogger crash on None msg and test pollution
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses local unit test failures caused by CosmosRichLogger crashing when a LogRecord has msg=None, and by cross-test pollution from cached CosmosRichLogger instances that unintentionally prepend the (astronomer-cosmos) prefix in unrelated tests.
Changes:
- Add a guard in
CosmosRichLogger.handle()to avoidTypeErrorwhenrecord.msgisNone. - Add an autouse pytest fixture to demote cached
CosmosRichLoggerinstances back to standardlogging.Loggerafter each test run.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
tests/conftest.py |
Adds an autouse fixture to clean up cached CosmosRichLogger instances between tests to prevent log-prefix pollution. |
cosmos/log.py |
Updates CosmosRichLogger.handle() to avoid crashing when record.msg is None. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Ensures CosmosRichLogger.handle() does not crash with TypeError when record.msg is None (e.g. logger.info(None)). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
pankajkoti
approved these changes
Apr 8, 2026
Merged
tatiana
pushed a commit
that referenced
this pull request
Apr 23, 2026
Bug Fixes * Fix ``ExecutionMode.WATCHER`` producer retry behaviour by @tatiana in #2559 * Prevent watcher producer skip propagating to downstream tasks via gateway task by @johnhoran and @tatiana in #2597 * Keep watcher sensor polling when producer is still running by @pankajkoti in #2592 * Fix circular import error in Cosmos plugin discovery under Astro Runtime by @tatiana in #2538 * Fix ``CosmosRichLogger`` crash on ``None`` log message by @tatiana in #2540 * Enable inlets and outlets using dbt Fusion on Airflow 3 by @ichirotakami in #2561 * Fix incorrectly skipped source downstream tasks in ``ExecutionMode.WATCHER`` by @pankajastro in #2563 * Fix duplicate logs in ``dbt build`` when source freshness is enabled by @pankajastro in #2564 * Warn and normalize when ``source_rendering_behavior=None`` is passed by @pankajastro in #2570 * Gracefully handle ``Variable.set()`` failures on Astro Remote Execution by @hkc-8010 in #2573 * Skip malformed YAML selectors instead of failing entirely by @YourRoyalLinus in #2577 Docs * Update watcher test behavior docs for Cosmos 1.14.0 by @tatiana in #2549 * Add redirect for moved partial-parsing docs page by @tatiana in #2550 * Document ``ExecutionMode.WATCHER`` and ``depends_on_past`` limitation by @tatiana in #2602 * Restore memory-optimised imports docs for Cosmos < 1.14.0 by @pankajkoti in #2604 Others * Speed up Airflow 3.1+ integration tests by caching InProcessExecutionAPI by @pankajkoti in #2547 * Improve stability of cache hash unit tests by @tatiana in #2539 * Fix mypy 1.20.0 type check failures by @pankajkoti in #2546 * Fix CI failures caused by docs build memory exhaustion by @pankajkoti in #2580 * Fix dbt Fusion broken integration tests by @tatiana in #2581 * Fix flaky ``cosmos_manifest_selectors_example`` DAG in CI by @pankajkoti in #2593 * Reduce pre-commit autoupdate frequency PRs by @tatiana in #2544 * Bump ``reviewdog/action-actionlint`` from 1.71.0 to 1.72.0 by @dependabot in #2542 * Skip watcher gateway test on Airflow 3.0 by @tatiana in #2607 closes: astronomer/oss-integrations-private#381
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Although the unittests are passing in the CI, there are a few failing locally. An example, when running:
I was seeing:
The problems seemed to be:
CosmosRichLogger.handle()crashed withTypeErrorwhenrecord.msgwasNone(e.g. logging aNonereturn value). Guard against this by checking for None and converting to str.conftest.pythat demotes anyCosmosRichLoggerinstances back tologging.Loggerafter each test. This preventstest_log.py(which monkeypatchesrich_logging=True) from permanently polluting the logging cache withCosmosRichLoggerinstances that add the (astronomer-cosmos) prefix to subsequent tests.