Fixed cmd2 bypassing NO_COLOR and allow_style when setting prompt-toolkit's color depth.#1706
Conversation
…lkit's color depth.
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
| 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() |
There was a problem hiding this comment.
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()There was a problem hiding this comment.
This is true for a few of our settables. They expect certain instance members to exist before than can safely be set.
allow_stylealways_prefix_settablestraceback_show_localstraceback_width
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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__().
No description provided.