Skip to content

Fix AttributeError when TeamColour is None in _load_drivers_from_f1_livetiming#932

Open
Federicorao wants to merge 3 commits into
theOehrly:mainfrom
Federicorao:codex/issue-931-1781212923
Open

Fix AttributeError when TeamColour is None in _load_drivers_from_f1_livetiming#932
Federicorao wants to merge 3 commits into
theOehrly:mainfrom
Federicorao:codex/issue-931-1781212923

Conversation

@Federicorao

@Federicorao Federicorao commented Jun 11, 2026

Copy link
Copy Markdown

Fixes #931.

What changed

  • Guard live timing team colors before lowercasing them.
  • Preserve built-in official colors for known teams when a session's live timing metadata has TeamColour=None.
  • Skip only unknown teams that cannot be auto-generated because no team color is available.
  • Add a regression test for missing live timing TeamColour.

Why

Some sessions include driver entries with TeamColour set to None. The previous logic called .lower() unconditionally; using #000000 as a fallback is not desirable because it can produce incorrect plot colors. Known teams already have official color constants, so the missing live value should leave those constants intact.

Verification

  • .venv/bin/python -m pytest fastf1/tests/test_plotting.py -k "load_drivers_preserves_known_team_color_if_live_color_missing"
  • .venv/bin/python -m pytest fastf1/tests/test_plotting.py -k "get_team_name_by_driver or load_drivers_preserves_known_team_color_if_live_color_missing"
  • .venv/bin/python -m ruff check fastf1/plotting/_backend.py
  • .venv/bin/isort --check-only fastf1/plotting/_backend.py fastf1/tests/test_plotting.py

AI Disclosure

AI tools were used to inspect the issue and review feedback, implement the follow-up change, add the regression test, run local validation, and update this PR description. The submitted code changes are in fastf1/plotting/_backend.py and fastf1/tests/test_plotting.py.

@github-actions

Copy link
Copy Markdown

Your PR description is missing the required AI disclosure. Please review Fastf1's AI policy, and then update your PR to include a section titled AI Disclosure (verbatim). PRs without AI disclosures will not be reviewed.

@Casper-Guo Casper-Guo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add the required AI disclosure as requested.

The fix is not idiomatic in the context of Python. I also need to take a bit longer to look at what is actually happening with the request here to determine the appropriate fallback value. #000000 is almost certainly not a desirable team color given that users may be plotting on light background.

Comment thread fastf1/plotting/_backend.py Outdated
@Casper-Guo Casper-Guo self-assigned this Jun 11, 2026
@Federicorao

Copy link
Copy Markdown
Author

Updated this PR based on the review feedback.

  • Removed the #000000 fallback.
  • Known teams now keep their built-in official color when live timing omits TeamColour.
  • Unknown teams without a live color are skipped instead of being auto-generated with an arbitrary color.
  • Added a regression test for TeamColour=None.

Validated with focused plotting pytest coverage, ruff check fastf1/plotting/_backend.py, and isort --check-only on the touched files.

team_color = None
if team_color_raw:
team_color = f"#{team_color_raw.lower()}"
abbreviation = driver_entry.get('Tla', '')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

team_color = None case should be handled here. This simplifies the logic flow and makes parsing the code much easier. I don't believe handling this condition in two separate code paths later is better/cleaner

Comment thread fastf1/tests/test_plotting.py Outdated
}
}

monkeypatch.setattr(fastf1._api, 'driver_info', mock_driver_info)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I assume this monkeypatch is introduced to limit the scope of setattr? In either case, I would like to avoid setattr unless it is well justified and the ramifications clearly documented

@Casper-Guo

Copy link
Copy Markdown
Collaborator

Known teams now keep their built-in official color when live timing omits TeamColour.

This makes sense.

Unknown teams without a live color are skipped instead of being auto-generated with an arbitrary color.

Can you please explain the implication of this and how it differs from the old behavior?

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.

[BUG] plotting.get_team_name_by_driver() fails when live timing TeamColour is None

2 participants