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

Add pre-commit #82

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
182 changes: 182 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
repos:
# Prevent commits to the main branch.
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: no-commit-to-branch
name: not committing to branch `main`
entry: no-commit-to-branch
args:
- --branch=main
language: python
pass_filenames: false
always_run: true
# Remove all unused imports for Python files.
- repo: https://github.com/hadialqattan/pycln
rev: v2.1.3
hooks:
- id: pycln
name: remove all unused imports
description: A formatter for finding and removing unused import statements.
entry: pycln
args:
- --all
language: python
language_version: python3
types:
- python
# Make sure all imports are absolute in Python files.
- repo: https://github.com/MarcoGorelli/absolufy-imports
rev: v0.3.1
hooks:
- id: absolufy-imports
name: make all imports absolute
description: Automatically convert relative imports to absolute
entry: absolufy-imports
language: python
types:
- python
minimum_pre_commit_version: 0.15.0
# Remove any commented out code.
- repo: https://github.com/asottile/dead
rev: v1.5.0
hooks:
- id: dead
name: detect dead code
description: dead simple python dead code detection
entry: dead
pass_filenames: false
language: python
language_version: python3
types:
- python
# Sort the imports using isort in Python files.
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
name: sort imports using isort
description: sort imports for all python files
entry: isort
args:
- --profile=black
- --verbose
- --overwrite-in-place
language: python
types_or:
- cython
- pyi
- python
# Format Python files.
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
name: format python modules and stub files
description: black - the uncompromising Python code formatter
entry: black
args:
- --verbose
language: python
require_serial: true
minimum_pre_commit_version: 2.9.2
types_or:
- python
# Lint Python files.
- repo: https://github.com/charliermarsh/ruff-pre-commit
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this and similar repositories trustworthy enough? This is a lot of repositories that I don't know anything about.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That is a very good question. We could run bash scripts for the different linting/typing/formatting tools if a dev has them installed. Let me experiment with this idea more.

rev: v0.0.242
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be set up via an environment file instead of separately here?

hooks:
- id: ruff
name: lint python modules
description: Run 'ruff' for extremely fast Python linting
entry: ruff
args:
- --verbose
- --diff
- --respect-gitignore
- --line-length=88
Copy link
Contributor

@mattpap mattpap Mar 18, 2023

Choose a reason for hiding this comment

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

This is pretty outdated limit and inconsistent with bokeh. We set this up higher in bokeh and bokehjs (~165) to avoid unnecessary line splitting. The same applies to setting up other tools, e.g. prettier.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We definitely want to mimic what Bokeh does, and I'll update this to be 165

language: python
types_or:
- python
- pyi
require_serial: true
minimum_pre_commit_version: 2.9.2
# Apply consistent formatting to the Python file `setup.cfg`.
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.2.0
hooks:
- id: setup-cfg-fmt
name: standardize the `setup.cfg` file
description: apply a consistent format to `setup.cfg` files
entry: setup-cfg-fmt
language: python
language_version: python3
files: (^|/)setup\.cfg$
# Check the following file types can be parsed.
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-json
name: check json files are parseable
description: checks json files for parseable syntax.
entry: check-json
language: python
types:
- json
- id: check-yaml
name: check yaml files are parseable
description: checks yaml files for parseable syntax.
entry: check-yaml
exclude: |
(?x)^(
conda.recipe/meta.yaml
)$
language: python
types:
- yaml
- id: check-toml
name: check toml files are parseable
description: checks toml files for parseable syntax.
entry: check-toml
language: python
types:
- toml
# Standardize markdown files.
- repo: https://github.com/executablebooks/mdformat
rev: 0.7.16
hooks:
- id: mdformat
name: format markdown using mdformat
description: CommonMark compliant Markdown formatter
entry: mdformat
args:
- --wrap=88
language: python
types:
- markdown
minimum_pre_commit_version: 1.0.0
additional_dependencies:
- mdformat-black
- mdformat-myst
# Run prettier on [Java|Type]Script files.
- repo: local
hooks:
- id: prettier
name: run prettier
description: run prettier on any javascript or typescript files
entry: bash -c "cd ipywidgets_bokeh; prettier --check --config=.prettierrc src/*"
language: system
types:
- javascript
pass_filenames: false
# Run eslint on [Java|Type]Script files.
- repo: local
hooks:
- id: eslint
name: run eslint
description: run eslint on any javascript or typescript files
entry: bash -c "cd ipywidgets_bokeh; eslint src/*"
language: system
types:
- javascript
pass_filenames: false
207 changes: 207 additions & 0 deletions ipywidgets_bokeh/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
module.exports = {
extends: [],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["tsconfig.json"],
tsconfigRootDir: ".",
sourceType: "module",
},
plugins: ["@typescript-eslint", "deprecation"],
rules: {
"@typescript-eslint/ban-types": [
"error",
{
types: {
Function: false,
object: false,
"{}": false,
},
},
],
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/member-delimiter-style": [
"error",
{
multiline: {
delimiter: "none",
requireLast: true,
},
singleline: {
delimiter: "comma",
requireLast: false,
},
},
],
"@typescript-eslint/semi": ["error", "never"],
"@typescript-eslint/type-annotation-spacing": ["error"],
"@typescript-eslint/no-unnecessary-condition": [
"warn",
{
allowConstantLoopConditions: true,
},
],
"@typescript-eslint/strict-boolean-expressions": [
"error",
{
allowAny: true,
},
],
"@typescript-eslint/no-unnecessary-type-assertion": ["error"],
"@typescript-eslint/no-unnecessary-type-constraint": ["error"],
"no-self-assign": [
"error",
{
props: false,
},
],
"brace-style": [
"error",
"1tbs",
{
allowSingleLine: true,
},
],
"comma-dangle": ["off"],
"@typescript-eslint/comma-dangle": [
"error",
{
arrays: "always-multiline",
objects: "always-multiline",
imports: "always-multiline",
exports: "always-multiline",
functions: "always-multiline",
enums: "always-multiline",
generics: "always-multiline",
tuples: "always-multiline",
},
],
"comma-spacing": [
"error",
{
before: false,
after: true,
},
],
"dot-notation": "error",
"eol-last": ["error", "always"],
indent: "off",
"@typescript-eslint/indent": [
"error",
2,
{
SwitchCase: 1,
outerIIFEBody: 1,
ArrayExpression: "first",
ObjectExpression: "first",
ImportDeclaration: "first",
VariableDeclarator: "first",
CallExpression: {
arguments: 1,
},
FunctionDeclaration: {
body: 1,
parameters: "off",
},
FunctionExpression: {
body: 1,
parameters: "off",
},
ignoredNodes: ["ConditionalExpression"],
},
],
"no-debugger": "error",
"no-floating-decimal": ["error"],
"no-multiple-empty-lines": [
"error",
{
max: 1,
maxBOF: 0,
maxEOF: 0,
},
],
"no-new-wrappers": "error",
"no-template-curly-in-string": "error",
"no-throw-literal": "error",
"no-trailing-spaces": ["error"],
"no-var": "error",
"object-shorthand": "error",
"prefer-const": [
"error",
{
destructuring: "all",
},
],
"prefer-exponentiation-operator": "error",
"quote-props": ["error", "as-needed"],
"object-curly-spacing": ["error", "never"],
"space-before-blocks": ["error", "always"],
"space-before-function-paren": [
"error",
{
anonymous: "never",
named: "never",
asyncArrow: "always",
},
],
"space-in-parens": ["error", "never"],
"keyword-spacing": [
"error",
{
before: true,
after: true,
},
],
"func-call-spacing": ["error", "never"],
"no-whitespace-before-property": ["error"],
"block-spacing": ["error", "always"],
"key-spacing": [
"error",
{
beforeColon: false,
afterColon: true,
mode: "minimum",
},
],
"space-unary-ops": [
"error",
{
words: true,
nonwords: false,
overrides: {},
},
],
"guard-for-in": ["error"],
quotes: [
"error",
"double",
{
avoidEscape: true,
allowTemplateLiterals: false,
},
],
"prefer-template": ["error"],
"generator-star-spacing": [
"error",
{
before: false,
after: true,
anonymous: {
before: false,
after: true,
},
method: {
before: true,
after: false,
},
},
],
"yield-star-spacing": [
"error",
{
before: false,
after: true,
},
],
"deprecation/deprecation": ["error"],
},
}
7 changes: 7 additions & 0 deletions ipywidgets_bokeh/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 88,
"tabWidth": 2,
"bracketSpacing": false,
"semi": false,
"trailingComma": "all"
}
Loading