Skip to content

Fixed cmd2 bypassing NO_COLOR and allow_style when setting prompt-toolkit's color depth.#1706

Merged
kmvanbrunt merged 2 commits into
mainfrom
pt_color_depth
Jul 7, 2026
Merged

Fixed cmd2 bypassing NO_COLOR and allow_style when setting prompt-toolkit's color depth.#1706
kmvanbrunt merged 2 commits into
mainfrom
pt_color_depth

Conversation

@kmvanbrunt

Copy link
Copy Markdown
Member

No description provided.

@kmvanbrunt kmvanbrunt requested a review from tleonhardt as a code owner July 7, 2026 03:28
@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.59%. Comparing base (d35f7d7) to head (2cb95e2).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1706      +/-   ##
==========================================
- Coverage   99.60%   99.59%   -0.02%     
==========================================
  Files          23       23              
  Lines        5894     5900       +6     
==========================================
+ Hits         5871     5876       +5     
- Misses         23       24       +1     
Flag Coverage Δ
unittests 99.59% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Comment thread cmd2/cmd2.py
def allow_style(self, value: ru.AllowStyle) -> None:
"""Setter property needed to support do_set when it updates allow_style."""
ru.ALLOW_STYLE = value
self.main_session.color_depth = pt_resolve_color_depth()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is a common pattern for subclasses of cmd2.Cmd to configure properties like self.allow_style in their __init__ before calling super().__init__(). Doing so here will crash the application because main_session has not been instantiated yet. Using getattr would makes this setter safe at any point in the lifecycle, i.e.:

if getattr(self, "main_session", None) is not None:
    self.main_session.color_depth = pt_resolve_color_depth()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is true for a few of our settables. They expect certain instance members to exist before than can safely be set.

  1. allow_style
  2. always_prefix_settables
  3. traceback_show_locals
  4. traceback_width

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should either attempt to ruggedize their use like I was suggesting above or do a very good job of documenting that these are special and certain instance members need to exist before they can be safely set (likely by saying don't set them before calling parent class dunder init).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it's best to leave the code alone. __init__() functions are meant to establish the baseline state for an instance. Setting a parent's instance variables before calling super().__init__() doesn't seem safe since those values could be overwritten by __init__().

Comment thread cmd2/pt_utils.py Outdated
@kmvanbrunt kmvanbrunt merged commit 38fc582 into main Jul 7, 2026
28 of 29 checks passed
@kmvanbrunt kmvanbrunt deleted the pt_color_depth branch July 7, 2026 16:21
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