Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to FORCE_COLOR environment variable if MYPY_FORCE_COLOR is not present #13814

Merged
merged 2 commits into from Oct 4, 2022

Conversation

AlexWaygood
Copy link
Member

Fixes #13806

This PR adds support for a FORCE_COLOR environment variable. If both MYPY_FORCE_COLOR and FORCE_COLOR are present, mypy will continue to use MYPY_FORCE_COLOR over FORCE_COLOR. However, if only FORCE_COLOR is set, mypy will use that environment variable in much the same way it currently uses MYPY_FORCE_COLOR.

MYPY_FORCE_COLOR appears to be undocumented and untested currently, so this PR doesn't add any tests. However, @hugovk has tested this change manually and using GitHub Actions, and reports that it appears to work as expected.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Ok!

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Ok!

@hauntsaninja hauntsaninja merged commit 0cab544 into python:master Oct 4, 2022
@AlexWaygood AlexWaygood deleted the force-color branch October 4, 2022 21:31
@github-actions
Copy link
Contributor

github-actions bot commented Oct 4, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Comment on lines +522 to +523
def should_force_color() -> bool:
return bool(int(os.getenv("MYPY_FORCE_COLOR", os.getenv("FORCE_COLOR", "0"))))
Copy link

Choose a reason for hiding this comment

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

fyi, if FORCE_COLOR is used for a different purpose (e.g. a different python library), it could contain a string, and this would raise an error.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, would be better to just check whether FORCE_COLOR exists, with any value (even 0!).

Copy link
Member Author

Choose a reason for hiding this comment

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

(even 0!)

...Really? If somebody puts FORCE_COLOR: 0 in a GitHub Actions workflow, they expect it to force color??

Copy link
Member Author

Choose a reason for hiding this comment

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

(even 0!)

...Really? If somebody puts FORCE_COLOR: 0 in a GitHub Actions workflow, they expect it to force color??

From a review of the Python projects @hugovk linked to in #13806 (comment), it looks like the answer is: yup! tox is the only project on that list that will treat FORCE_COLOR: 0 as indicating that the user does not want color.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

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

@AlexWaygood thanks, but this is not what i was talking about. i was talking about a possible ValueError in case the variable is not a number (e.g. False).

Copy link
Member

Choose a reason for hiding this comment

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

...Really? If somebody puts FORCE_COLOR: 0 in a GitHub Actions workflow, they expect it to force color??

Yes, that's what NO_COLOR does! 😅

Command-line software which adds ANSI color to its output by default should check for a NO_COLOR environment variable that, when present and not an empty string (regardless of its value), prevents the addition of ANSI color.

https://no-color.org/

Here's how pytest does it:

https://github.com/pytest-dev/pytest/blob/b7d4de1ea983d3c4d3d9e235f63942abc6bfb167/src/_pytest/_io/terminalwriter.py#L26-L37

Copy link
Member Author

Choose a reason for hiding this comment

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

@AlexWaygood thanks, but this is not what i was talking about. i was talking about a possible ValueError in case the variable is not a number (e.g. False).

Yeah, I understood! #13853 means that you won't get a ValueError for FORCE_COLOR if you provide a value for that variable that can't be passed to the int() constructor. (It preserves the behaviour for MYPY_FORCE_COLOR that was present prior to this PR, however, since that's longstanding behaviour, and "fixing" it might be a breaking change in some ways.)

#13853 looks like it might be rejected, though, so this may be a moot point :)

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.

Support FORCE_COLOR in addition to MYPY_FORCE_COLOR
5 participants