ci: auto-detect Python version from HA Core for test matrix#1
Conversation
This changes the update-test-matrix.py script to fetch the required Python version for a given HA Core version by fetching the `pyproject.toml` for that version. It also update the test matrix with the updated script.
📝 WalkthroughWalkthroughThe GitHub Actions test workflow and its update script were enhanced to use more precise Python patch versions across Home Assistant core versions. The workflow matrix now specifies exact versions (e.g., 3.12.0, 3.13.2), and the update script was improved to fetch Python version requirements from the HA Core repository with network error handling and caching. Changes
Sequence DiagramsequenceDiagram
actor User
participant Script as update-test-matrix.py
participant Network as HA Core Repository
participant Fallback as Version Mapping Logic
User->>Script: Run script
Script->>Script: Call get_python_version(ha_version)
Script->>Script: Check cache (functools.cache)
alt Cache hit
Script-->>Script: Return cached version
else Cache miss
Script->>Network: Fetch pyproject.toml
alt Success
Network-->>Script: Return pyproject.toml
Script->>Script: Parse requires-python field
Script-->>Script: Extract min Python version
else URLError (network failure)
Network-->>Script: Connection error
Script->>Fallback: Use hardcoded fallback logic
Fallback-->>Script: Return mapped version
end
end
Script->>Script: Cache result
Script-->>User: Generate pytest matrix with Python version
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@scripts/update-test-matrix.py`:
- Around line 86-116: The function get_python_version silently returns "3.13"
for unknown/future ha_version values; change it to fail loudly instead: after
the fetch and fallback logic (inside get_python_version) remove the hardcoded
return "3.13" and raise a clear exception (e.g., RuntimeError or ValueError)
that includes the ha_version so CI will fail and prompt updating the mapping;
ensure the exception message mentions get_python_version and the problematic
ha_version so callers can surface the failure.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 09fb8381-542e-4d6e-9ecb-076acebc43bf
📒 Files selected for processing (2)
.github/workflows/pytest.yamlscripts/update-test-matrix.py
| @functools.cache | ||
| def get_python_version(ha_version: str) -> str: | ||
| """Determine Python version based on HA Core version.""" | ||
| """Determine Python version based on HA Core version by fetching from GitHub.""" | ||
| url = f"https://raw.githubusercontent.com/home-assistant/core/{ha_version}/pyproject.toml" | ||
| try: | ||
| with urllib.request.urlopen(url, timeout=10) as response: # noqa: S310 | ||
| content = response.read().decode() | ||
| # Look for requires-python = ">=3.14.2" | ||
| match = re.search(r'requires-python\s*=\s*">=(\d+\.\d+(?:\.\d+)?)', content) | ||
| if match: | ||
| return match.group(1) | ||
| except urllib.error.URLError as e: | ||
| print(f"Warning: Could not fetch python version for {ha_version}: {e}") | ||
|
|
||
| # Fallback to hardcoded logic if fetch fails | ||
| parts = ha_version.split(".") | ||
| year, month = int(parts[0]), int(parts[1]) | ||
| # 2024.x and 2025.1 use Python 3.12, 2025.2+ use Python 3.13 | ||
| if year == 2024 or (year == 2025 and month == 1): | ||
| return "3.12" | ||
| if len(parts) >= 2: | ||
| try: | ||
| year, month = int(parts[0]), int(parts[1]) | ||
| # 2024.x and 2025.1 use Python 3.12, 2025.2 to 2026.2 use Python 3.13 | ||
| if year == 2024 or (year == 2025 and month == 1): | ||
| return "3.12" | ||
| if year == 2025 or (year == 2026 and month <= 2): | ||
| return "3.13" | ||
| except ValueError: | ||
| pass | ||
|
|
||
| if ha_version == "dev": | ||
| return "3.14" | ||
|
|
||
| return "3.13" |
There was a problem hiding this comment.
Avoid silent fallback to 3.13 for unknown/future versions.
If network fetch fails, Line 116 currently defaults to "3.13" for unmatched versions (e.g., newer HA releases), which can silently produce an incorrect CI matrix.
Proposed fix
@@
def get_python_version(ha_version: str) -> str:
@@
- try:
+ fetch_failed = False
+ try:
with urllib.request.urlopen(url, timeout=10) as response: # noqa: S310
content = response.read().decode()
@@
except urllib.error.URLError as e:
print(f"Warning: Could not fetch python version for {ha_version}: {e}")
+ fetch_failed = True
@@
- if ha_version == "dev":
+ if ha_version == "dev":
return "3.14"
- return "3.13"
+ if fetch_failed:
+ raise RuntimeError(
+ f"Unable to determine Python version for HA Core {ha_version}: "
+ "remote fetch failed and no fallback mapping matched."
+ )
+ return "3.13"🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@scripts/update-test-matrix.py` around lines 86 - 116, The function
get_python_version silently returns "3.13" for unknown/future ha_version values;
change it to fail loudly instead: after the fetch and fallback logic (inside
get_python_version) remove the hardcoded return "3.13" and raise a clear
exception (e.g., RuntimeError or ValueError) that includes the ha_version so CI
will fail and prompt updating the mapping; ensure the exception message mentions
get_python_version and the problematic ha_version so callers can surface the
failure.
Summary
update-test-matrix.pynow fetches the required Python version from each HA Core release'spyproject.tomlinstead of hardcoding3.12.0,3.13.0,3.13.2)Test plan
python scripts/update-test-matrix.pyand verify it fetches versions correctly🤖 Generated with Claude Code
Summary by CodeRabbit