Skip to content

Commit

Permalink
Merge pull request #42 from dougollerenshaw/improve_linting
Browse files Browse the repository at this point in the history
Adding flake8 to ci/cd
  • Loading branch information
dougollerenshaw authored Oct 4, 2024
2 parents 640375b + e5b4950 commit e0183fc
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 80 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 88
extend-ignore = E203, E266, E501, W503
select = B,C,E,F,W,T4,B9
27 changes: 27 additions & 0 deletions .github/workflows/flake8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Flake8 Linter

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 flake8-unused-arguments
- name: Run Flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 ./codeaide --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings
flake8 ./codeaide --count --exit-zero --max-complexity=10 --max-line-length=88 --statistics
27 changes: 26 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-unused-arguments]

- repo: https://github.com/PyCQA/autoflake
rev: v2.1.1
hooks:
- id: autoflake
args: [
--in-place,
--remove-all-unused-imports,
--remove-unused-variables,
--expand-star-imports,
--ignore-init-module-imports,
]
15 changes: 4 additions & 11 deletions codeaide/logic/chat_handler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import json
import os
import sys
import re
import traceback
import logging
from codeaide.utils.api_utils import (
parse_response,
send_api_request,
get_api_client,
save_api_key,
MissingAPIKeyException,
)
from codeaide.utils.constants import (
MAX_RETRIES,
Expand All @@ -19,15 +16,11 @@
INITIAL_MESSAGE,
)
from codeaide.utils.cost_tracker import CostTracker
from codeaide.utils.environment_manager import EnvironmentManager
from codeaide.utils.file_handler import FileHandler
from codeaide.utils.terminal_manager import TerminalManager
from codeaide.utils.general_utils import generate_session_id
from codeaide.utils.logging_config import get_logger, setup_logger
from PyQt5.QtWidgets import QMessageBox, QTextEdit
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QObject, QMetaObject, Qt, Q_ARG, pyqtSlot, pyqtSignal
from codeaide.ui.traceback_dialog import TracebackDialog
from PyQt5.QtCore import QObject, pyqtSignal


class ChatHandler(QObject):
Expand Down Expand Up @@ -354,7 +347,7 @@ def update_conversation_history(self, response):
{"role": "assistant", "content": response.choices[0].message.content}
)
else:
raise ValueError(f"Unsupported provider: {provider}")
raise ValueError(f"Unsupported provider: {self.current_provider}")
self.file_handler.save_chat_history(self.conversation_history)

def create_questions_response(self, text, questions):
Expand Down Expand Up @@ -607,7 +600,7 @@ def send_traceback_to_agent(self, traceback_text):
f"```\n{traceback_text}\n```\n\n"
"Please provide a solution that avoids this error."
)
self.logger.info(f"ChatHandler: Setting input text in chat window")
self.logger.info("ChatHandler: Setting input text in chat window")
self.chat_window.input_text.setPlainText(message)
self.logger.info(f"ChatHandler: Calling on_submit in chat window")
self.logger.info("ChatHandler: Calling on_submit in chat window")
self.chat_window.on_submit()
17 changes: 3 additions & 14 deletions codeaide/ui/chat_window.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import signal
import sys
import traceback
import time
import logging
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import (
Expand All @@ -11,8 +7,6 @@
QMainWindow,
QMessageBox,
QPushButton,
QSizePolicy,
QSpacerItem,
QTextEdit,
QVBoxLayout,
QWidget,
Expand All @@ -35,7 +29,6 @@
USER_MESSAGE_COLOR,
AI_PROVIDERS,
DEFAULT_PROVIDER,
DEFAULT_MODEL,
MODEL_SWITCH_MESSAGE,
)
from codeaide.utils.logging_config import get_logger
Expand Down Expand Up @@ -168,7 +161,7 @@ def on_submit(self):
self.logger.info("ChatWindow: Empty input, returning")
return

self.logger.info(f"ChatWindow: Processing user input")
self.logger.info("ChatWindow: Processing user input")
self.input_text.clear()

if self.waiting_for_api_key:
Expand Down Expand Up @@ -350,22 +343,18 @@ def update_chat_handler(self):
self.logger.info(f"In update_chat_handler, message: {message}")

if not success:
self.logger.info(f"In update_chat_handler, not success")
self.logger.info("In update_chat_handler, not success")
if message: # This indicates that an API key is required
self.waiting_for_api_key = True
self.add_to_chat("AI", message)
else:
self.logger.info(f"In update_chat_handler, not success, no message")
self.logger.info("In update_chat_handler, not success, no message")
self.add_to_chat(
"System",
f"Failed to set model {model} for provider {provider}. Please check your API key.",
)
return

new_version = general_utils.increment_version(
current_version, major_or_minor="major", increment=1
)

# Use the constant with format
switch_message = MODEL_SWITCH_MESSAGE.format(
provider=provider,
Expand Down
5 changes: 0 additions & 5 deletions codeaide/ui/code_popup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
QHBoxLayout,
QLabel,
QPushButton,
QTextEdit,
QVBoxLayout,
QWidget,
QPlainTextEdit,
QMessageBox,
QDialog,
)
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

from codeaide.utils import general_utils
from codeaide.utils.constants import (
Expand Down
1 change: 0 additions & 1 deletion codeaide/ui/example_selection_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
)

from codeaide.utils import general_utils
from codeaide.utils.constants import CHAT_WINDOW_BG, CHAT_WINDOW_FG


class ExampleSelectionDialog(QDialog):
Expand Down
3 changes: 1 addition & 2 deletions codeaide/ui/traceback_dialog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from PyQt5.QtWidgets import QMessageBox, QTextEdit
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
import logging


Expand Down Expand Up @@ -38,7 +37,7 @@ def __init__(self, parent, traceback_text):

def exec_(self):
self.logger.info("TracebackDialog: Executing dialog")
result = super().exec_()
super().exec_()
user_choice = "fix" if self.clickedButton() == self.send_button else "ignore"
self.logger.info(f"TracebackDialog: User chose to {user_choice} the traceback")
return self.clickedButton() == self.send_button
6 changes: 1 addition & 5 deletions codeaide/utils/api_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import os
import json
import re
import anthropic
import openai
from decouple import config, AutoConfig
from decouple import AutoConfig
import hjson
from anthropic import APIError
import logging

from codeaide.utils.constants import (
AI_PROVIDERS,
Expand Down
3 changes: 0 additions & 3 deletions codeaide/utils/cost_tracker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from datetime import datetime


class CostTracker:
def __init__(self):
self.cost_log = []
Expand Down
1 change: 0 additions & 1 deletion codeaide/utils/environment_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import subprocess
import sys
import venv
from codeaide.utils.logging_config import get_logger

Expand Down
1 change: 0 additions & 1 deletion codeaide/utils/file_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import shutil
import json
import logging
from codeaide.utils.logging_config import setup_logger, get_logger


Expand Down
1 change: 1 addition & 0 deletions codeaide/utils/general_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from PyQt5.QtGui import QFont, QColor
from datetime import datetime
from codeaide.utils.logging_config import get_logger
import sys

logger = get_logger()

Expand Down
2 changes: 0 additions & 2 deletions codeaide/utils/terminal_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
import os
import subprocess
import sys
import tempfile
import threading
import logging
import queue
import time
import re
from codeaide.utils.environment_manager import EnvironmentManager


Expand Down
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ black
isort
pre-commit
pyqt5
pygments
pygments
flake8
flake8-unused-arguments
autoflake
1 change: 0 additions & 1 deletion tests/integration/test_api_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import pytest
from codeaide.utils.api_utils import get_api_client, send_api_request, parse_response
from codeaide.utils.constants import SYSTEM_PROMPT

ANTHROPIC_MODEL = "claude-3-haiku-20240307"
OPENAI_MODEL = "gpt-3.5-turbo"
Expand Down
4 changes: 1 addition & 3 deletions tests/ui/test_chat_window.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
from unittest.mock import Mock, patch, MagicMock
from unittest.mock import Mock, patch
import pytest
import logging
import os
Expand All @@ -10,7 +9,6 @@

from codeaide.ui.chat_window import ChatWindow
from codeaide.logic.chat_handler import ChatHandler
from codeaide.utils import general_utils
from codeaide.utils.constants import (
AI_PROVIDERS,
DEFAULT_PROVIDER,
Expand Down
Loading

0 comments on commit e0183fc

Please sign in to comment.