Unverified Commit 071a8846 authored by Pierre Smeyers's avatar Pierre Smeyers Committed by GitLab
Browse files

Merge branch 'feat/pyright' into 'master'

feat(pyright): add basedpyright job for type checking and configuration options

Closes #97

See merge request to-be-continuous/python!172
parents 1089c07b 601e706f
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -401,6 +401,27 @@ In addition to a textual report in the console, this job produces the following
| `$PYTHON_PROJECT_DIR/reports/py-mypy.codeclimate.json` | [Code Climate](https://github.com/soul-catcher/mypy-gitlab-code-quality) | [GitLab integration](https://docs.gitlab.com/ci/yaml/artifacts_reports/#artifactsreportscodequality) |
| `$PYTHON_PROJECT_DIR/reports/py-mypy.console.txt` | [mypy console output](https://mypy.readthedocs.io/) | [SonarQube integration](https://docs.sonarsource.com/sonarqube-server/latest/analyzing-source-code/importing-external-issues/external-analyzer-reports/) |

#### `py-basedpyright` job

This job is **disabled by default** and performs type checking based on [basedpyright](https://docs.basedpyright.com/) (a fork of Microsoft's pyright).
It is activated by setting `$PYRIGHT_ENABLED` to `true`.

It is bound to the `build` stage, and uses the following variables:

| Input / Variable         | Description                        | Default value     |
| ------------------------ | ---------------------------------- | ----------------- |
| `pyright-enabled` / `PYRIGHT_ENABLED` | Set to `true` to enable the `basedpyright` job                  | _none_ (disabled) |
| `pyright-args` / `PYRIGHT_ARGS` | Additional [basedpyright CLI options](https://docs.basedpyright.com/latest/configuration/command-line/) | _none_           |
| `pyright-level` / `PYRIGHT_LEVEL` | The minimum message level that will cause the job to fail (one of: `error`, `warning`) | `error` |

Basedpyright can be configured using a `pyproject.toml` file or a standalone `pyrightconfig.json` file. See the [configuration documentation](https://docs.basedpyright.com/latest/configuration/config-files/) for more details.

In addition to a textual report in the console, this job produces the following reports, kept for one day:

| Report         | Format                                                                       | Usage             |
| -------------- | ---------------------------------------------------------------------------- | ----------------- |
| `$PYTHON_PROJECT_DIR/reports/py-basedpyright.codeclimate.json` | [Code Climate](https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types) | [GitLab integration](https://docs.gitlab.com/ci/yaml/artifacts_reports/#artifactsreportscodequality) |

### SonarQube analysis

If you're using the SonarQube template to analyse your Python code, here is a sample `sonar-project.properties` file:
+20 −0
Original line number Diff line number Diff line
@@ -323,6 +323,26 @@
          "advanced": true
        }
      ]
    },
    {
      "id": "pyright",
      "name": "basedpyright",
      "description": "Type checking based on [basedpyright](https://docs.basedpyright.com/) (fork of Microsoft's pyright).",
      "enable_with": "PYRIGHT_ENABLED",
      "variables": [
        {
          "name": "PYRIGHT_ARGS",
          "description": "Additional [basedpyright CLI options](https://docs.basedpyright.com/latest/configuration/command-line/)",
          "advanced": true
        },
        {
          "name": "PYRIGHT_LEVEL",
          "description": "The minimum message level that will cause the job to fail",
          "type": "enum",
          "values": ["error", "warning"],
          "default": "error"
        }
      ]
    }
  ],
  "variants": [
+39 −0
Original line number Diff line number Diff line
@@ -203,6 +203,19 @@ spec:
    mypy-files:
      description: Files or directories to analyse
      default: ''
    pyright-enabled:
      description: Enable pyright
      type: boolean
      default: false
    pyright-args:
      description: Additional [basedpyright CLI options](https://docs.basedpyright.com/latest/configuration/command-line/)
      default: ""
    pyright-level:
      description: The minimum message level that will cause the job to fail
      options:
      - error
      - warning
      default: error
---
# default workflow rules: Merge Request pipelines
.tbc-workflow-rules:
@@ -349,6 +362,9 @@ variables:
  MYPY_ENABLED: $[[ inputs.mypy-enabled ]]
  MYPY_ARGS: $[[ inputs.mypy-args ]]
  MYPY_FILES: $[[ inputs.mypy-files ]]
  PYRIGHT_ENABLED: $[[ inputs.pyright-enabled ]]
  PYRIGHT_ARGS: $[[ inputs.pyright-args ]]
  PYRIGHT_LEVEL: $[[ inputs.pyright-level ]]


.python-scripts: &python-scripts |
@@ -1373,6 +1389,29 @@ py-mypy:
      when: never
    - !reference [.test-policy, rules]

py-basedpyright:
  stage: build
  extends: .python-base
  script:
    - mkdir -p -m 777 reports
    - install_requirements
    - _pip install basedpyright
    - _run basedpyright --level ${PYRIGHT_LEVEL} --gitlabcodequality reports/py-basedpyright.codeclimate.json ${PYRIGHT_ARGS}
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    expire_in: 1 day
    when: always
    reports:
      codequality: $PYTHON_PROJECT_DIR/reports/py-basedpyright.codeclimate.json
    paths:
      - "$PYTHON_PROJECT_DIR/reports/py-basedpyright.*"
  rules:
    # exclude if $PYRIGHT_ENABLED not set
    - if: '$PYRIGHT_ENABLED != "true"'
      when: never
    - !reference [.test-policy, rules]


###############################################################################################
#                                      test stage                                             #
###############################################################################################