@@ -53,8 +53,12 @@ class ExtraContext(cloup.Context):
5353 """Like ``cloup._context.Context``, but with the ability to populate the context's
5454 ``meta`` property at instantiation.
5555
56- Also inherits ``color`` property from parent context. And sets it to `True` for
57- parentless contexts at instantiatiom, so we can always have colorized output.
56+ Also defaults ``color`` to ``True`` for root contexts (i.e. without a parent), so
57+ help screens are always colorized — even when piped. Click's own default is ``None``
58+ (auto-detect via TTY), which strips colors in non-interactive contexts.
59+
60+ Parent-to-child color inheritance is handled by Click itself at ``Context.__init__``
61+ time, so no property override is needed.
5862
5963 .. todo::
6064 Propose addition of ``meta`` keyword upstream to Click.
@@ -66,45 +70,22 @@ class ExtraContext(cloup.Context):
6670 def __init__ (self , * args , meta : dict [str , Any ] | None = None , ** kwargs ) -> None :
6771 """Like parent's context but with an extra ``meta`` keyword-argument.
6872
69- Also force ``color`` property default to `True` if not provided by user and
70- this context has no parent.
73+ Also force ``color`` default to `` True`` if not provided by user and this
74+ context has no parent.
7175 """
7276 super ().__init__ (* args , ** kwargs )
7377
78+ # Click defaults root ``ctx.color`` to ``None`` (auto-detect via TTY), which
79+ # strips colors when piped. Override to ``True`` for parentless contexts so
80+ # help screens are always colorized by default. The ``ColorOption`` callback
81+ # will set the final value later, respecting ``--no-color`` and env vars.
82+ if not self .parent and self .color is None :
83+ self .color = True
84+
7485 # Update the context's meta property with the one provided by user.
7586 if meta :
7687 self ._meta .update (meta )
7788
78- # Transfer user color setting to our internally managed value.
79- self ._color : bool | None = kwargs .get ("color" , None )
80-
81- # A Context created from scratch, i.e. without a parent, and whose color
82- # setting is set to auto-detect (i.e. is None), will defaults to forced
83- # colorized output.
84- if not self .parent and self ._color is None :
85- self ._color = True
86-
87- @property
88- def color (self ) -> bool | None :
89- """Overrides ``Context.color`` to allow inheritance from parent context.
90-
91- Returns the color setting of the parent context if it exists and the color is
92- not set on the current context.
93- """
94- if self ._color is None and self .parent :
95- return self .parent .color
96- return self ._color
97-
98- @color .setter
99- def color (self , value : bool | None ) -> None :
100- """Set the color value of the current context."""
101- self ._color = value
102-
103- @color .deleter
104- def color (self ) -> None :
105- """Reset the color value so it defaults to inheritance from parent's."""
106- self ._color = None
107-
10889
10990def default_extra_params () -> list [click .Option ]:
11091 """Default additional options added to ``@command`` and ``@group``.
0 commit comments