From df2f5e01a5872de382fe16a336e036dc589717a9 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Wed, 19 Apr 2023 15:28:19 +0200 Subject: [PATCH] Fix handling of -q and -s, add pre-commit checks. --- .flake8 | 2 +- .pre-commit-config.yaml | 11 +++++++++++ chatmastermind/main.py | 22 ++++++++++------------ chatmastermind/storage.py | 2 +- mypy.ini | 7 +++++++ 5 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 mypy.ini diff --git a/.flake8 b/.flake8 index f1d12eb..e5af6e1 100644 --- a/.flake8 +++ b/.flake8 @@ -1,6 +1,6 @@ [flake8] exclude = .git,__pycache__,.venv -per-file-ignores = __init__.py:F401 +per-file-ignores = __init__.py:F401, tests/test_ai.py:E501 max-line-length = 140 max-complexity = 10 select = B,C,E,F,W,T4,B9 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..fd7a9c2 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,11 @@ +repos: + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 'v1.1.1' + hooks: + - id: mypy + args: [--config-file=mypy.ini, --install-types, --non-interactive] + + - repo: https://github.com/pycqa/flake8 + rev: 6.0.0 + hooks: + - id: flake8 diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 1c937d4..0c08bcf 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -9,6 +9,7 @@ import argparse from .utils import terminal_width, pp, process_tags, display_chat from .storage import save_answers, create_chat, get_tags from .api_client import ai, openai_api_key +from itertools import zip_longest def run_print_command(args: argparse.Namespace, config: dict) -> None: @@ -32,23 +33,20 @@ def process_and_display_chat(args: argparse.Namespace, question_list = args.question if args.question is not None else [] source_list = args.source if args.source is not None else [] - for question, source in zip(question_list, source_list): - with open(source) as r: - question_parts.append(f"{question}\n\n```\n{r.read().strip()}\n```") - - if len(question_list) > len(source_list): - for question in question_list[len(source_list):]: + for question, source in zip_longest(question_list, source_list, fillvalue=None): + if question is not None and source is not None: + with open(source) as r: + question_parts.append(f"{question}\n\n```\n{r.read().strip()}\n```") + elif question is not None: question_parts.append(question) - else: - for source in source_list[len(question_list):]: + elif source is not None: with open(source) as r: question_parts.append(f"```\n{r.read().strip()}\n```") - question = '\n\n'.join(question_parts) - - chat = create_chat(question, tags, extags, config) + full_question = '\n\n'.join(question_parts) + chat = create_chat(full_question, tags, extags, config) display_chat(chat, dump, args.only_source_code) - return chat, question, tags + return chat, full_question, tags def handle_question(args: argparse.Namespace, diff --git a/chatmastermind/storage.py b/chatmastermind/storage.py index fb7bd8d..0f21bba 100644 --- a/chatmastermind/storage.py +++ b/chatmastermind/storage.py @@ -51,7 +51,7 @@ def create_chat(question: Optional[str], extags: Optional[List[str]], config: Dict[str, Any] ) -> List[Dict[str, str]]: - chat = [] + chat: List[Dict[str, str]] = [] append_message(chat, 'system', config['system'].strip()) for file in sorted(pathlib.Path(config['db']).iterdir()): if file.suffix == '.yaml': diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..b99c5a5 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,7 @@ +[mypy] +files = chatmastermind, tests +ignore_missing_imports = True +strict_optional = True +warn_unused_ignores = False +warn_redundant_casts = True +warn_unused_configs = True