Commit 7866ec0f authored by Joel Collins's avatar Joel Collins
Browse files

Static type analysis

parent 3aebb8be
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -60,9 +60,8 @@ docs/_build/
target/

#Big-o files
capture/
record/
*.data
tests/images/out/

#IDE files
.vscode/
+38 −2
Original line number Diff line number Diff line
stages:
  - analysis
  - testing
  - build
  - package
  - deploy
@@ -36,7 +37,24 @@ pylint:
  <<: *poetry-install

  script:
    - poetry run pylint ./openflexure_microscope/
    - poetry run poe pylint

  only:
    - branches
    - merge_requests
    - tags
    - web

# Python type checking with Mypy
mypy:
  stage: analysis
  image: python:3.7
  retry: 1

  <<: *poetry-install

  script:
    - poetry run poe mypy

  only:
    - branches
@@ -49,12 +67,30 @@ black:
  stage: analysis
  image: python:3.7
  retry: 1
  allow_failure: true

  <<: *poetry-install

  script:
    # Run static build script
    - poetry run black --check .
    - poetry run poe black_check

  only:
    - branches
    - merge_requests
    - tags
    - web

# Python unit tests with PyTest
pytest:
  stage: testing
  image: python:3.7
  retry: 1

  <<: *poetry-install

  script:
    - poetry run poe test

  only:
    - branches

.pre-commit-config.yaml

deleted100644 → 0
+0 −11
Original line number Diff line number Diff line
repos:
  - repo: https://github.com/psf/black
    rev: 19.10b0
    hooks:
      - id: black
  - repo: https://github.com/timothycrosley/isort
    rev: 5.4.2
    hooks:
      - id: isort
        additional_dependencies: [toml]
        exclude: ^.*/?setup\.py$
+1 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ node_modules
.gitlab-ci.yml
.gitlab
./dist
tests/
!openflexure_microscope/api/static/dist

# VCS files
+30 −16
Original line number Diff line number Diff line
@@ -22,34 +22,48 @@ This includes installing the server in a mode better suited for active developme
    * `npm install`
    * `npm run build`

## Formatting and linting
## Formatting, linting, and tests

We use 3 main code analysis and formatting libraries in this project. **Please run all of these before submitting a merge request.** 
### Tl;dr

**Before committing**

Run `poetry run poe format`

Auto-formats the code

**Before submitting a merge request/merging**

Run `poetry run poe check`

Formats code, lints, runs static analysis, and runs unit tests.
### Details

We use several code analysis and formatting libraries in this project. **Please run all of these before submitting a merge request.** 

Our CI will check each of these automatically, so ensuring they pass locally will save you time.

* **Black** - Code formatting with minimal configuration.
  * While sometimes it's not perfect, its fine 90% of the time and prevents arguments about formatting. 
  * Automatically formats your code
  * `poetry run black .`
* **Isort** - Import sorting
  * Automatically organises your imports to stop things getting out of hand
  * `poetry run isort .`
  * `poetry run poe black`
* **Pylint** - Static code analysis
  * Analyses your code, failing if issues are detected.
  * We've disabled some less severe warnings, so _if anything fails your merge request will be blocked_
  * `poetry run pylint openflexure_microscope`

### Pre-commit hooks

We support pre-commit hooks to run code formatting before committing to the codebase.
  * `poetry run poe pylint`
* **Mypy** - Type checking
  * Analyses your type hints and annotations to flag up potential bugs
  * Where possible, use type hints in your code. Even if dependencies don't support it, it'll help identify issues.
  * `poetry run poe mypy`
* **Pytest** - Unit testing
  * While unit testing is of limited use due to our dependence on real hardware, some simple isolated functions can (and should) be unit tested.
  * `poetry run poe test`

The simplest way to ensure this works is to install pre-commits into your current Python environment:
Though not in the CI, our `format` script also runs isort:

* `pip3 install pre-commit`
* `pre-commit install`

To run pre-commit analysis manually, you can run `pre-commit run`.
* **Isort** - Import sorting
  * Automatically organises your imports to stop things getting out of hand
  * `poetry run isort .`

## Build-system

Loading