Skip to content

Fix flake8 F824 CI blocker and upgrade CI pipeline#4

Merged
joshuvavinith merged 2 commits intomainfrom
copilot/fix-flake8-f824-error
Mar 6, 2026
Merged

Fix flake8 F824 CI blocker and upgrade CI pipeline#4
joshuvavinith merged 2 commits intomainfrom
copilot/fix-flake8-f824-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 6, 2026

CI was consistently failing due to a stale global _default_bot declaration in api.py (flake8 F824 — undefined name in global statement, part of the blocking --select=E9,F63,F7,F82 rule set). The workflow also lacked a proper test matrix, coverage reporting, and dependency caching.

Changes

api.py

  • Removed dead global _default_bot from _get_or_create_session — the function already operates directly on the module-level _sessions dict
# Before
def _get_or_create_session(session_id: Optional[str]) -> tuple[str, ChatBot]:
    global _default_bot   # ← F824: _default_bot never assigned
    if session_id is None:
        ...

# After
def _get_or_create_session(session_id: Optional[str]) -> tuple[str, ChatBot]:
    if session_id is None:
        ...

.github/workflows/ci.yml

Replaced the single lint-and-test job with three sequenced jobs:

  • lint — Python 3.12 only; blocking lint (E9,F63,F7,F82) + non-blocking style pass (--exit-zero --max-line-length=100); pip cache enabled
  • test (needs: lint) — Matrix over Python 3.10 / 3.11 / 3.12; runs pytest --cov=. --cov-report=xml; uploads coverage.xml per version as an artifact; sets OPENAI_API_KEY to a clearly invalid placeholder to enforce offline test mode
  • docker-build (needs: test) — Builds both MODE=web and MODE=api images

requirements.txt

  • Added pytest-cov>=4.0.0 to support coverage collection in the test job

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.kaggle.com
    • Triggering command: /home/REDACTED/.local/bin/pytest pytest test_chatbot.py -v --tb=short (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Problem

The GitHub Actions CI pipeline is consistently failing on every push to main. Two things need to be addressed:

1. Fix api.py — flake8 F824 error (immediate blocker)

In api.py, the function _get_or_create_session contains a stale global _default_bot statement. The variable _default_bot is never assigned anywhere in the file, making the global declaration dead code. flake8 catches this as F824 — which is part of the blocking rule set (--select=E9,F63,F7,F82) and causes the build to fail with exit code 1.

File: api.py
Line to remove:

def _get_or_create_session(session_id: Optional[str]) -> tuple[str, ChatBot]:
    """Return (session_id, ChatBot) for the given session; create if missing."""
    global _default_bot   # <-- DELETE THIS LINE

Simply remove the global _default_bot line. The function works correctly without it since it already uses the module-level _sessions dict directly.


2. Implement a full, robust CI pipeline

Replace (or significantly upgrade) .github/workflows/ci.yml with a complete CI pipeline that covers all of the following:

Repository context

  • Language: Python 3.12
  • Files: ai_chatbot.py, api.py (FastAPI), web_demo.py (Streamlit), test_chatbot.py, requirements.txt, Dockerfile
  • Test runner: pytest test_chatbot.py -v
  • Linter: flake8
  • No OpenAI API key needed — tests run fully offline using pattern-matching mode

Required CI jobs

Job 1: lint

  • Runs on ubuntu-latest with Python 3.12
  • Installs flake8
  • Run blocking lint: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
  • Run non-blocking (warnings only, --exit-zero) lint for style issues with --max-line-length=100
  • Should fail fast if there are syntax errors or undefined names

Job 2: test

  • Depends on lint passing
  • Runs on a matrix of Python versions: ["3.10", "3.11", "3.12"]
  • Installs all requirements.txt dependencies
  • Runs pytest test_chatbot.py -v --tb=short with coverage: pytest --cov=. --cov-report=xml
  • Uploads the coverage XML as an artifact
  • Sets OPENAI_API_KEY env var to empty string to ensure offline mode is used

Job 3: docker-build

  • Depends on test passing
  • Runs on ubuntu-latest
  • Builds both Docker images:
    • docker build --build-arg MODE=web -t ai-chatbot:web .
    • docker build --build-arg MODE=api -t ai-chatbot:api .
  • Both must succeed

Triggers

  • push to branches: main, master, copilot/**
  • pull_request targeting: main, master

Security

  • Use permissions: contents: read at the workflow level
  • Do not use GITHUB_TOKEN write permissions unless necessary

Additional requirements

  • Cache pip dependencies using actions/cache or actions/setup-python's built-in caching (cache: 'pip') to speed up runs
  • Use actions/upload-artifact@v4 to upload the coverage report
  • Add pytest-cov to requirements.txt if it's not already there

Summary of changes needed

  1. api.py — Remove the line global _default_bot from _get_or_create_session
  2. .github/workflows/ci.yml — Rewrite with full lint → test matrix → docker-build pipeline
  3. requirements.txt — Add pytest-cov>=4.0.0 if missing

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: joshuvavinith <146979257+joshuvavinith@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix flake8 F824 error in api.py Fix flake8 F824 CI blocker and upgrade CI pipeline Mar 6, 2026
@joshuvavinith joshuvavinith marked this pull request as ready for review March 6, 2026 21:17
@joshuvavinith joshuvavinith merged commit f37219c into main Mar 6, 2026
10 checks passed
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.

2 participants