Skip to content

Add --color cli option to force colored output#22806

Merged
MichaReiser merged 2 commits intoastral-sh:mainfrom
cwkang1998:feat/add-color-cli-option
Jan 23, 2026
Merged

Add --color cli option to force colored output#22806
MichaReiser merged 2 commits intoastral-sh:mainfrom
cwkang1998:feat/add-color-cli-option

Conversation

@cwkang1998
Copy link
Contributor

@cwkang1998 cwkang1998 commented Jan 22, 2026

Summary

Closes #22716.

This PR adds a new --color cli global options to ruff that allows turning the colored output on or off, similar to that of ty.

ruff --color always check `/path/to/file.py`.

This should solve issues with the environment variables for forcing color mentioned in #22716.

New --color cli option

image

Screenshots

--color always image
--color never image

With the new changes, it should still support environment variables that enable/disable the color output, but --color cli option will take precedence over environment variables: that is, given FORCE_COLOR and --color never, ruff will respect --color never and output without color.

--color takes precedences

image

Test Plan

I've added 2 tests specifically to test for how the environment variables will interact with each other:

  1. environment variable should still be respected when no --color value is passed via cli.
  2. When a value is passed with --color

@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 22, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser added the cli Related to the command-line interface label Jan 23, 2026
}
// Default to no override, but respect FORCE_COLOR.
Some(TerminalColor::Auto) | None => {
colored::control::unset_override();
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason that we need to call unset_override here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is added whilst looking at ty's implementation.

Was thinking that it might be necessary to unset it here so that it'll reset any manual override before it.

}

#[cfg(test)]
mod test_set_colored_override {
Copy link
Member

@MichaReiser MichaReiser Jan 23, 2026

Choose a reason for hiding this comment

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

Unfortunately, we can't keep those unit tests as they'll mess up the colored output of other tests (they modify global state).

If you want to test this code, I suggest changing set_colored_override to colored_override(color_option: Option<TerminalColor>, force_color_env: Option<OsString>) -> Option<bool>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I missed this. Thanks for pointing this out!

@MichaReiser MichaReiser enabled auto-merge (squash) January 23, 2026 08:31
@MichaReiser MichaReiser merged commit da8ee78 into astral-sh:main Jan 23, 2026
40 checks passed
@cwkang1998
Copy link
Contributor Author

@MichaReiser Thanks for the review!

@cwkang1998 cwkang1998 deleted the feat/add-color-cli-option branch January 23, 2026 13:51
carljm added a commit that referenced this pull request Jan 30, 2026
* main: (62 commits)
  [`refurb`] Do not add `abc.ABC` if already present (`FURB180`) (#22234)
  [ty] Add a new `assert-type-unspellable-subtype` diagnostic (#22815)
  [ty] Avoid duplicate syntax errors for `await` outside functions (#22826)
  [ty] Fix unary operator false-positive for constrained TypeVars (#22783)
  [ty] Fix binary operator false-positive for constrained TypeVars (#22782)
  [ty] Fix false-positive `unsupported-operator` for "symmetric" TypeVars (#22756)
  [`pydocstyle`] Clarify which quote styles are allowed (`D300`) (#22825)
  [ty] Use distributed versions of AND and OR on constraint sets (#22614)
  [ty] Add support for dict literals and dict() calls as default values for parameters with TypedDict types (#22161)
  Document `-` stdin convention in CLI help text (#22817)
  [ty] Make `infer_subscript_expression_types` a method on `Type` (#22731)
  [ty] Simplify `OverloadLiteral::spans` and `OverloadLiteral::parameter_span` (#22823)
  [ty] Require both `*args` and `**kwargs` when calling a `ParamSpec` callable (#22820)
  [ty] Handle tagged errors in conformance (#22746)
  Add `--color` cli option to force colored output (#22806)
  Identify notebooks by LSP didOpen instead of `.ipynb` file extension (#22810)
  [ty] Fix docstring rendering for literal blocks after doctests (#22676)
  [ty] Update salsa to fix out-of-order query validation (#22498)
  [ty] Inline cycle initial and recovery functions (#22814)
  [ty] Pass the generic context through the decorator (#22544)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli Related to the command-line interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a cli option to force colored output

2 participants