Skip to content

Fix unit tests CosmosRichLogger crash on None msg and test pollution#2540

Merged
pankajkoti merged 2 commits into
mainfrom
fix-logger-test-pollution
Apr 8, 2026
Merged

Fix unit tests CosmosRichLogger crash on None msg and test pollution#2540
pankajkoti merged 2 commits into
mainfrom
fix-logger-test-pollution

Conversation

@tatiana
Copy link
Copy Markdown
Collaborator

@tatiana tatiana commented Apr 7, 2026

Although the unittests are passing in the CI, there are a few failing locally. An example, when running:

AIRFLOW_HOME=`pwd` hatch run tests.py3.10-3.1-1.11:test-cov

I was seeing:

FAILED                                 
tests/airflow/test_graph.py::test_create_task_metadata_unsupported - AssertionError: assert equals failed                                                            
  '\x1b[35m(astronomer-cosmos)\x1b[0m Unavailable conversion function for <u  'Unavailable conversion function for <unsupported> (node <unsupported.dbt-             
  nsupported> (node <unsupported.dbt-proj.unsupported>). Define a converter   proj.unsupported>). Define a converter function using render_config.node_c             
  function using render_config.node_converters.'    and also FAILED tests/dbt/test_selector.py::test_select_nodes_by_invalid_config - AssertionError: assert         
'Unsupported config key selector: invalid_config' in ['\x1b[35m(astronomer-cosmos)\x1b[0m Unsupported config key selector: invalid_config']                          
 +  where ['\x1b[35m(astronomer-cosmos)\x1b[0m Unsupported config key selector: invalid_config'] = <_pytest.logging.LogCaptureFixture object at                      
0x16f34f730>.messages                                                                                                                                                
FAILED tests/operators/test_kubernetes.py::test_dbt_kubernetes_operator_handle_warnings[\n        19:48:25  Concurrency: 4 threads (target='target')\n               
19:48:25\n        19:48:25  1 of 2 START test dbt_utils_accepted_range_table_col__12__0 ................... [RUN]\n        19:48:25  2 of 2 START test               
unique_table__uuid .......................................... [RUN]\n        19:48:27  1 of 2 WARN dbt_utils_accepted_range_table_col__12__0 .....................   
[WARN in 1.83s]\n        19:48:27  2 of 2 PASS unique_table__uuid ................................................ [PASS in 1.85s]\n        19:48:27\n               
19:48:27  Finished running 2 tests, 1 hook in 0 hours 0 minutes and 12.86 seconds (12.86s).\n        19:48:27\n        19:48:27  Completed with 1 warning:\n         
19:48:27\n        19:48:27  Warning in test dbt_utils_accepted_range_table_col__12__0 (models/ads/ads.yaml)\n        19:48:27  Got 252 results, configured to warn   
if >0\n        19:48:27\n        19:48:27    compiled Code at target/compiled/model/models/table/table.yaml/dbt_utils_accepted_range_table_col__12__0.sql\n          
19:48:27\n        19:48:27  Done. PASS=1 WARN=1 ERROR=0 SKIP=0 TOTAL=2\n        19:48:27  Command `dbt test` succeeded at 07:50:02.340364 after 43.98 seconds\n      
   19:48:27  Flushing usage events\n        -True] - TypeError: can only concatenate str (not "NoneType") to str                                                     
FAILED tests/operators/test_kubernetes.py::test_dbt_kubernetes_operator_handle_warnings[\n        19:48:25  Concurrency: 4 threads (target='target')\n               
19:48:25\n        19:48:25  1 of 2 START test dbt_utils_accepted_range_table_col__12__0 ................... [RUN]\n        19:48:25  2 of 2 START test               
unique_table__uuid .......................................... [RUN]\n        19:48:27  1 of 2 PASS 252 dbt_utils_accepted_range_table_col__12__0                     
..................... [PASS in 1.83s]\n        19:48:27  2 of 2 PASS unique_table__uuid ................................................ [PASS in 1.85s]\n           
19:48:27\n        19:48:27  Finished running 2 tests, 1 hook in 0 hours 0 minutes and 12.86 seconds (12.86s).\n        19:48:27\n        19:48:27  Done. PASS=2      
WARN=0 ERROR=0 SKIP=0 TOTAL=2\n        -False] - TypeError: can only concatenate str (not "NoneType") to str                                                         
FAILED tests/operators/test_kubernetes.py::test_dbt_kubernetes_operator_handle_warnings[\n        gibberish\n        -False] - TypeError: can only concatenate str   
(not "NoneType") to str                                        

The problems seemed to be:

  • 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.

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
Copy link
Copy Markdown

codecov Bot commented Apr 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.08%. Comparing base (678e508) to head (5df3617).
⚠️ Report is 2 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tatiana tatiana changed the title Fix CosmosRichLogger crash on None msg and test pollution Fix unit tests CosmosRichLogger crash on None msg and test pollution Apr 7, 2026
@tatiana tatiana marked this pull request as ready for review April 7, 2026 23:25
@tatiana tatiana requested review from a team, corsettigyg, dwreeves and jbandoro as code owners April 7, 2026 23:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 avoid TypeError when record.msg is None.
  • Add an autouse pytest fixture to demote cached CosmosRichLogger instances back to standard logging.Logger after 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.

Comment thread cosmos/log.py
Comment thread cosmos/log.py
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 pankajkoti merged commit 92810c4 into main Apr 8, 2026
84 checks passed
@pankajkoti pankajkoti deleted the fix-logger-test-pollution branch April 8, 2026 07:00
@tatiana tatiana added this to the Cosmos 1.14.1 milestone Apr 8, 2026
@pankajkoti pankajkoti mentioned this pull request Apr 23, 2026
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
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.

3 participants