Repository: scikit-learn/scikit-learn
File: build_tools/get_comment.py (Python)

Code before:
    """Create a new comment or update the existing linting comment."""

    if comment is not None:
        print("Updating existing comment")
        comment.edit(message)
    else:
        print("Creating new comment")
        issue.create_comment(message)


def update_linter_fails_label(linting_failed, issue):
    """Add or remove the label indicating that the linting has failed."""

    label = "CI:Linter failure"

    if linting_failed:
        issue.add_to_labels(label)

    else:
        try:
            issue.remove_from_labels(label)
        except GithubException as exception:
            # The exception is ignored if raised because the issue did not have the
            # label already
            if not exception.message == "Label does not exist":
                raise exception


if __name__ == "__main__":
    repo_str = os.environ["GITHUB_REPOSITORY"]
    token = os.environ["GITHUB_TOKEN"]
    pr_number = os.environ["PR_NUMBER"]
    sha = os.environ["BRANCH_SHA"]
    log_file = os.environ["LOG_FILE"]
    run_id = os.environ["RUN_ID"]
    versions_file = os.environ["VERSIONS_FILE"]

    versions = get_versions(versions_file)

    for var, val in [
        ("GITHUB_REPOSITORY", repo_str),
        ("GITHUB_TOKEN", token),
        ("PR_NUMBER", pr_number),
        ("LOG_FILE", log_file),
        ("RUN_ID", run_id),
    ]:
        if not val:
            raise ValueError(f"The following environment variable is not set: {var}")

    if not re.match(r"\d+$", pr_number):
        raise ValueError(f"PR_NUMBER should be a number, got {pr_number!r} instead")

Review comment (suggestion):
Nitpick: using a bare `raise` creates a traceback which is a tiny bit simpler (basically you don't get the `raise exception` line)

```suggestion
                raise
```

Code after:
    """Create a new comment or update the existing linting comment."""

    if comment is not None:
        print("Updating existing comment")
        comment.edit(message)
    else:
        print("Creating new comment")
        issue.create_comment(message)


def update_linter_fails_label(linting_failed, issue):
    """Add or remove the label indicating that the linting has failed."""

    label = "CI:Linter failure"

    if linting_failed:
        issue.add_to_labels(label)

    else:
        try:
            issue.remove_from_labels(label)
        except GithubException as exception:
            # The exception is ignored if raised because the issue did not have the
            # label already
            if not exception.message == "Label does not exist":
                raise


if __name__ == "__main__":
    repo_str = os.environ["GITHUB_REPOSITORY"]
    token = os.environ["GITHUB_TOKEN"]
    pr_number = os.environ["PR_NUMBER"]
    sha = os.environ["BRANCH_SHA"]
    log_file = os.environ["LOG_FILE"]
    run_id = os.environ["RUN_ID"]
    versions_file = os.environ["VERSIONS_FILE"]

    versions = get_versions(versions_file)

    for var, val in [
        ("GITHUB_REPOSITORY", repo_str),
        ("GITHUB_TOKEN", token),
        ("PR_NUMBER", pr_number),
        ("LOG_FILE", log_file),
        ("RUN_ID", run_id),
    ]:
        if not val:
            raise ValueError(f"The following environment variable is not set: {var}")

    if not re.match(r"\d+$", pr_number):
        raise ValueError(f"PR_NUMBER should be a number, got {pr_number!r} instead")