Skip to content

Proposal: Enforce consistent code style with black, isort, and pre-commit #699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .git-blame-ignore-revs
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This should preserve the regular git blame history for commits prior to the automated formatting commit and only show that commit as the git blame for newly added whitespace or removed whitespace changes.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Migrate code style to Black
a463f15ffd9dcc36af4829b78b5e2a81d304f500

# Rerun formatting after merge conflict resolution
b6fac19ce47b0f16211948b907d8b3c55dcee985
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repos:
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.10.1
hooks:
- id: black
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.11
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ Once you've forked the code and created a new branch for your work, you can run

After modifying the source code, you will need to do `poetry run interpreter` again.

**Note**: This project uses [`black`](https://black.readthedocs.io/en/stable/index.html) and [`isort`](https://pypi.org/project/isort/) via a [`pre-commit`](https://pre-commit.com/) hook to ensure consistent code style. If you need to bypass it for some reason, you can `git commit` with the `--no-verify` flag.

### Installing New Packages

If you wish to install new dependencies into the project, please use `poetry add package-name`.

#### Installing Developer Dependencies

If you need to install dependencies specific to development, like testing tools, formatting tools, etc. please use `poetry add package-name --group dev`.

### Known Issues

For some, `poetry install` might hang on some dependencies. As a first step, try to run the following command in your terminal:
Expand Down
11 changes: 6 additions & 5 deletions interpreter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .core.core import Interpreter
import sys

from .core.core import Interpreter

# This is done so when users `import interpreter`,
# they get an instance of interpreter:

Expand All @@ -11,9 +12,9 @@

# But I think it saves a step, removes friction, and looks good.

# ____ ____ __ __
# ____ ____ __ __
# / __ \____ ___ ____ / _/___ / /____ _________ ________ / /____ _____
# / / / / __ \/ _ \/ __ \ / // __ \/ __/ _ \/ ___/ __ \/ ___/ _ \/ __/ _ \/ ___/
# / /_/ / /_/ / __/ / / / _/ // / / / /_/ __/ / / /_/ / / / __/ /_/ __/ /
# \____/ .___/\___/_/ /_/ /___/_/ /_/\__/\___/_/ / .___/_/ \___/\__/\___/_/
# /_/ /_/
# / /_/ / /_/ / __/ / / / _/ // / / / /_/ __/ / / /_/ / / / __/ /_/ __/ /
# \____/ .___/\___/_/ /_/ /___/_/ /_/\__/\___/_/ / .___/_/ \___/\__/\___/_/
# /_/ /_/
41 changes: 27 additions & 14 deletions interpreter/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import argparse
import subprocess
import os
import platform
import pkg_resources
import ooba
import subprocess

import appdirs
from ..utils.get_config import get_config_path
import ooba
import pkg_resources

from ..terminal_interface.conversation_navigator import conversation_navigator
from ..utils.get_config import get_config_path

arguments = [
{
Expand All @@ -15,7 +17,12 @@
"help_text": "prompt / custom instructions for the language model",
"type": str,
},
{"name": "local", "nickname": "l", "help_text": "run the language model locally (experimental)", "type": bool},
{
"name": "local",
"nickname": "l",
"help_text": "run the language model locally (experimental)",
"type": bool,
},
{
"name": "auto_run",
"nickname": "y",
Expand Down Expand Up @@ -76,7 +83,7 @@
"help_text": "optionally enable safety mechanisms like code scanning; valid options are off, ask, and auto",
"type": str,
"choices": ["off", "ask", "auto"],
"default": "off"
"default": "off",
},
{
"name": "gguf_quality",
Expand Down Expand Up @@ -148,10 +155,10 @@ def cli(interpreter):
help="get Open Interpreter's version number",
)
parser.add_argument(
'--change_local_device',
dest='change_local_device',
action='store_true',
help="change the device used for local execution (if GPU fails, will use CPU)"
"--change_local_device",
dest="change_local_device",
action="store_true",
help="change the device used for local execution (if GPU fails, will use CPU)",
)

# TODO: Implement model explorer
Expand Down Expand Up @@ -204,7 +211,9 @@ def cli(interpreter):
setattr(interpreter, attr_name, attr_value)

# if safe_mode and auto_run are enabled, safe_mode disables auto_run
if interpreter.auto_run and (interpreter.safe_mode == "ask" or interpreter.safe_mode == "auto"):
if interpreter.auto_run and (
interpreter.safe_mode == "ask" or interpreter.safe_mode == "auto"
):
setattr(interpreter, "auto_run", False)

# Default to Mistral if --local is on but --model is unset
Expand All @@ -223,7 +232,9 @@ def cli(interpreter):
return

if args.change_local_device:
print("This will uninstall the experimental local LLM interface (Ooba) in order to reinstall it for a new local device. Proceed? (y/n)")
print(
"This will uninstall the experimental local LLM interface (Ooba) in order to reinstall it for a new local device. Proceed? (y/n)"
)
if input().lower() == "n":
return

Expand All @@ -237,11 +248,13 @@ def cli(interpreter):

gpu_choice = input("> ").upper()

while gpu_choice not in 'ABCDN':
while gpu_choice not in "ABCDN":
print("Invalid choice. Please try again.")
gpu_choice = input("> ").upper()

ooba.install(force_reinstall=True, gpu_choice=gpu_choice, verbose=args.debug_mode)
ooba.install(
force_reinstall=True, gpu_choice=gpu_choice, verbose=args.debug_mode
)
return

# Deprecated --fast
Expand Down
5 changes: 2 additions & 3 deletions interpreter/code_interpreters/base_code_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@


class BaseCodeInterpreter:
"""
.run is a generator that yields a dict with attributes: active_line, output
"""

def __init__(self):
pass

def run(self, code):
pass

def terminate(self):
pass
pass
1 change: 1 addition & 0 deletions interpreter/code_interpreters/create_code_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .language_map import language_map


def create_code_interpreter(language):
# Case in-sensitive
language = language.lower()
Expand Down
11 changes: 5 additions & 6 deletions interpreter/code_interpreters/language_map.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from .languages.python import Python
from .languages.shell import Shell
from .languages.javascript import JavaScript
from .languages.html import HTML
from .languages.applescript import AppleScript
from .languages.r import R
from .languages.html import HTML
from .languages.javascript import JavaScript
from .languages.powershell import PowerShell

from .languages.python import Python
from .languages.r import R
from .languages.shell import Shell

language_map = {
"python": Python,
Expand Down
18 changes: 10 additions & 8 deletions interpreter/code_interpreters/languages/applescript.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os

from ..subprocess_code_interpreter import SubprocessCodeInterpreter


class AppleScript(SubprocessCodeInterpreter):
file_extension = "applescript"
proper_name = "AppleScript"

def __init__(self):
super().__init__()
self.start_cmd = os.environ.get('SHELL', '/bin/zsh')
self.start_cmd = os.environ.get("SHELL", "/bin/zsh")

def preprocess_code(self, code):
"""
Expand All @@ -17,33 +19,33 @@ def preprocess_code(self, code):
code = self.add_active_line_indicators(code)

# Escape double quotes
code = code.replace('"', r'\"')
code = code.replace('"', r"\"")

# Wrap in double quotes
code = '"' + code + '"'

# Prepend start command for AppleScript
code = "osascript -e " + code

# Append end of execution indicator
code += '; echo "## end_of_execution ##"'

return code

def add_active_line_indicators(self, code):
"""
Adds log commands to indicate the active line of execution in the AppleScript.
"""
modified_lines = []
lines = code.split('\n')
lines = code.split("\n")

for idx, line in enumerate(lines):
# Add log command to indicate the line number
if line.strip(): # Only add if line is not empty
modified_lines.append(f'log "## active_line {idx + 1} ##"')
modified_lines.append(line)

return '\n'.join(modified_lines)
return "\n".join(modified_lines)

def detect_active_line(self, line):
"""
Expand All @@ -61,4 +63,4 @@ def detect_end_of_execution(self, line):
"""
Detects end of execution marker in the output.
"""
return "## end_of_execution ##" in line
return "## end_of_execution ##" in line
12 changes: 8 additions & 4 deletions interpreter/code_interpreters/languages/html.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import webbrowser
import tempfile
import os
import tempfile
import webbrowser

from ..base_code_interpreter import BaseCodeInterpreter


class HTML(BaseCodeInterpreter):
file_extension = "html"
proper_name = "HTML"
Expand All @@ -16,6 +18,8 @@ def run(self, code):
f.write(code.encode())

# Open the HTML file with the default web browser
webbrowser.open('file://' + os.path.realpath(f.name))
webbrowser.open("file://" + os.path.realpath(f.name))

yield {"output": f"Saved to {os.path.realpath(f.name)} and opened with the user's default web browser."}
yield {
"output": f"Saved to {os.path.realpath(f.name)} and opened with the user's default web browser."
}
14 changes: 8 additions & 6 deletions interpreter/code_interpreters/languages/javascript.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from ..subprocess_code_interpreter import SubprocessCodeInterpreter
import re

from ..subprocess_code_interpreter import SubprocessCodeInterpreter


class JavaScript(SubprocessCodeInterpreter):
file_extension = "js"
proper_name = "JavaScript"

def __init__(self):
super().__init__()
self.start_cmd = "node -i"

def preprocess_code(self, code):
return preprocess_javascript(code)

def line_postprocessor(self, line):
# Node's interactive REPL outputs a billion things
# So we clean it up:
Expand All @@ -20,7 +22,7 @@ def line_postprocessor(self, line):
if line.strip() in ["undefined", 'Type ".help" for more information.']:
return None
# Remove trailing ">"s
line = re.sub(r'^\s*(>\s*)+', '', line)
line = re.sub(r"^\s*(>\s*)+", "", line)
return line

def detect_active_line(self, line):
Expand All @@ -30,7 +32,7 @@ def detect_active_line(self, line):

def detect_end_of_execution(self, line):
return "## end_of_execution ##" in line


def preprocess_javascript(code):
"""
Expand Down Expand Up @@ -61,4 +63,4 @@ def preprocess_javascript(code):
console.log("## end_of_execution ##");
"""

return processed_code
return processed_code
Loading