Skip to content
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

setting up linters and black formatter #15

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
],
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
},
// Install extensions
Expand Down Expand Up @@ -135,7 +138,8 @@
//Promptflow
"ms-toolsai.vscode-ai",
"ms-toolsai.jupyter",
"prompt-flow.prompt-flow"
"prompt-flow.prompt-flow",
"ms-python.black-formatter"
]
}
},
Expand Down
2 changes: 2 additions & 0 deletions .devcontainer/on-create.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ echo_status() { local msg=$1; echo "[$(date -u +%Y-%m-%dT%H:%M:%S%Z)] $msg" | te

echo_status "on-create start"
pip install --no-cache-dir ipython ipykernel
pip install "black[jupyter]"
pip install isort

# only run apt upgrade on pre-build
sudo apt-get update
Expand Down
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 = 110
# ignore space before `:` since black adds space
extend-ignore = E203
21 changes: 21 additions & 0 deletions docs/developer_experience/python_development.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Python Development

To ensure a level of code quality and best practices, this repo includes Python linters and formatter configuration.
Before commiting changes to the code in this repo, make sure you execute the following steps:

```bash
# navigate to root of repo
cd /workspaces/botify

# format code - this is a pre-requisite for the linting step
make -f python.mk format

# run linters
make -f python.mk lint
```

## Evaluating and fixing linter errors

There are 5 steps to the linting process and in most cases, when one of the steps fails it won't progress any further. This means that after you fix all the errors, you should run the linters again because the additional steps will likely catch additional problems.

In some cases, you need to override a certain rule because it may be too impractical to fix. Depending on the linter that is generating the error there are different ways of doing this through config files or code annotations. This should only be used in special cases where the cost of fixing largely surpasses the benefit (e.g: test classes).
759 changes: 759 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
[tool.poetry]
name = "Botify"
authors = []
description = "Generative AI chatbot that leverages Azure Cloud Services, Microsoft Open-Source, and other Open-Source technologies to create a question/answer bot."
version = "1.0.0"
package-mode = false

[tool.poetry.dependencies]
python = "^3.10"

[tool.poetry.group.dev.dependencies]
pytest = "*"
black = {extras = ["jupyter"], version = "*"}
isort = "*"
mypy = "*"
flake8 = "*"
pylint = "*"
pytest-cov = "*"

[tool.black]
line_length = 110

[tool.isort]
# vertical hanging indent
multi_line_output = 3

# Includes a trailing comma on multi line imports that include parentheses
include_trailing_comma = true

line_length = 110

[tool.mypy]
# no optional errors, since the list of optional errors may change
strict = true

# visually nicer output in error messages
pretty = true

# suppresses error messages about imports that cannot be resolved.
ignore_missing_imports = true

# warns about casting an expression to its inferred type.
warn_redundant_casts = true

# shows a warning when encountering any code inferred to be unreachable or redundant.
warn_unreachable = true

# allows variables to be redefined with an arbitrary type.
allow_redefinition = true

# disallows defining functions without type annotations or with incomplete type annotations.
disallow_untyped_defs = true

# this flag tells mypy that top-level packages will be based in either the current directory, or a member of the MYPYPATH.
explicit_package_bases = true

[tool.pylint.'MAIN']
# Discover python modules and packages in the file system subtree.
recursive = true

# [tool.pylint.'MESSAGES CONTROL']
disable = "C0103"

[tool.pylint.'BASIC']
# Include a hint for the correct naming format with invalid-name.
include-naming-hint = true

# Regular expression matching correct variable names.
variable-rgx = "^[a-z][a-z0-9]*(([_a-z0-9]+)*)?$"

# Regular expression matching correct argument names.
argument-rgx = "^[a-z][a-z0-9]*(([_a-z0-9]+)*)?$"

# Defaults to ('i', 'j', 'k', 'ex', 'Run', '_')
good-names = ""

[tool.pylint.'FORMAT']
max-line-length = 110

[tool.pylint.'DESIGN']
# Maximum number of arguments for method
max-args=10

# Maximum number of attributes for a class (see R0902).
max-attributes=20

[tool.coverage.run]
omit = [".*", "*/site-packages/*", "tests/*"]

[tool.coverage.report]
omit = ["tests/*"]
fail_under = 70
16 changes: 16 additions & 0 deletions python.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Makefile for Poetry-managed Python projects

REPO_ROOT_PATH = $(shell git rev-parse --show-toplevel 2> /dev/null)

.PHONY: format lint

format:
poetry run black --config "$(REPO_ROOT_PATH)/pyproject.toml" .
poetry run isort --config-root $(REPO_ROOT_PATH) --resolve-all-configs .

lint:
poetry run black --config "$(REPO_ROOT_PATH)/pyproject.toml" . --check
poetry run flake8 --config $(REPO_ROOT_PATH)/.flake8 .
poetry run isort --config-root $(REPO_ROOT_PATH) --resolve-all-configs --check-only --diff .
poetry run pylint --rcfile $(REPO_ROOT_PATH)/pyproject.toml .
poetry run mypy --config-file $(REPO_ROOT_PATH)/pyproject.toml .