Skip to content

Commit

Permalink
Merge pull request #345 from roedoejet/dev.hatch
Browse files Browse the repository at this point in the history
Switch to `hatch` for build and (dynamic) versioning
  • Loading branch information
dhdaines committed Apr 12, 2024
2 parents b772bd6 + 4e23d76 commit 6333947
Show file tree
Hide file tree
Showing 29 changed files with 474 additions and 175 deletions.
7 changes: 0 additions & 7 deletions .coveragerc

This file was deleted.

7 changes: 7 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# NOTE: This is actually ignored by flake8 in pre-commit for some
# reason but it will get used in editors. How to make flake8 read it?
# Who knows? flake8 is a mystery.

[flake8]
max-line-length = 120
extend-ignore = E203,E704
5 changes: 1 addition & 4 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .
- name: Install documentation dependencies
run: |
pip install -r docs/requirements.txt
pip install -e .[docs]
- name: Setup doc deploy
run: |
git config user.name 'github-actions[bot]'
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/matrix-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements/requirements.test.txt
pip install -e .
pip install -e .[test,api]
- name: Run tests
run: python run_tests.py dev
5 changes: 1 addition & 4 deletions .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .
- name: Install documentation dependencies
run: |
pip install -r docs/requirements.txt
pip install -e .[docs]
- name: Setup doc deploy
run: |
git config user.name 'github-actions[bot]'
Expand Down
17 changes: 12 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ on:
- push
- workflow_call

# Since we don't checkout the full history, set a default version so
# certain tests (pep440, update_schema) will still function. NOTE:
# This **must** match the Major.Minor version of the JSON schema file
# in g2p/mappings/.schema!!!
env:
SETUPTOOLS_SCM_PRETEND_VERSION: "2.0"

jobs:
test-all-on-linux:
# This is our main test job, exercising everything and uploading to coverage
Expand All @@ -17,9 +24,8 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements/requirements.test.txt
pip install -e .
pip install -e .[test]
pip install gunicorn
pip install pip-licenses
if pip-licenses | grep -v 'Artistic License' | grep -v LGPL | grep GNU; then echo 'Please avoid introducing *GPL dependencies'; false; fi
pip install coverage
Expand Down Expand Up @@ -81,15 +87,16 @@ jobs:
if: ${{ !contains(github.event.head_commit.message, '#no-ci') }}
steps:
- uses: actions/checkout@v4
with:
fetch-tags: true
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.7"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements/requirements.test.txt
pip install -e .
pip install -e .[test]
- name: Run tests on Windows
run: python run_tests.py dev
- name: Make sure the CLI outputs utf8 on Windows
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ docs/_build/

# mkdocs build
site
g2p/_version.py
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
exclude: \.svg$
- repo: local
# Using local repos because these won't work for me from remote repo -EJ
# They're also more convenient because we install them via requirements.dev.txt
# They're also more convenient because we install them via optional deps
# and they are then available on the command line as well as in pre-commit.
hooks:
- id: isort
Expand All @@ -32,6 +32,7 @@ repos:
stages: [commit]
- repo: https://github.com/pycqa/flake8
# do flake8 last to avoid duplicate reports
rev: 3.8.3
rev: 5.0.4
hooks:
- id: flake8
- id: flake8
args: ["--max-line-length=120", "--extend-ignore=E203,E704"]
8 changes: 0 additions & 8 deletions .pylintrc

This file was deleted.

58 changes: 48 additions & 10 deletions Contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ them in.

## TL;DR

Please considering running these commands in each of your sandboxes to enable our
pre-commit hooks and commitlint:
Please consider using the `dev` environment with `hatch` to do
development, which enables some checking of Git commits and messages:

```sh
pip install -r requirements/requirements.dev.txt
hatch -e dev shell
```

If you have a pre-existing virtual environment (sandbox), you can also
install the required packages with `pip`:

```sh
pip install -e .[dev]
pre-commit install
gitlint install-hook
```
Expand Down Expand Up @@ -53,15 +60,30 @@ All the pre-commit hooks are executed using a tool called
[pre-commit](https://pre-commit.com/). Once you enable pre-commit, it will run all the
hooks each time you try to commit anything in this repo.

We've listed all the developper dependencies for the project in
[requirements/requirements.dev.txt](requirements/requirements.dev.txt) to make them easy to install:
We've added all the developer dependencies for the project to the
`dev` environment to make them easy to install with `hatch`. In
addition, `pre-commit` and `gitlint` hooks will be installed on
creation of this environment. Note that you will have to use the
`dev` environment when committing since pre-commit is installed there.
You can either start a shell:

```sh
pip install -r requirements/requirements.dev.txt
pre-commit install
hatch -e dev shell
```

Note that you have to run the second command in every g2p sandbox you create.
Or run commands in the environment:

```sh
hatch -e dev run git commit -m 'chore: foo bar baz'
```

If you have a pre-existing virtual environment (sandbox), you can also
install the required packages with `pip`:

```sh
pip install -e .[dev]
pre-commit install
```

## commitlint

Expand Down Expand Up @@ -115,10 +137,26 @@ These rules are inspired by these commit formatting guides:
We run commitlint on each commit message that you write by enabling the commit-msg hook in
Git.

Run this command in your g2p sandbox to install and enable the commit-msg hook:
The commit-msg hook is enabled on creation of the `dev` environment
with `hatch`. Note that you will have to use the `dev` environment
when committing since pre-commit is installed there. You can either
start a shell:

```sh
hatch -e dev shell
```

Or run commands in the environment:

```sh
hatch -e dev run git commit -m 'chore: foo bar baz'
```

If you have a pre-existing virtual environment (sandbox), you can also
install the required packages with `pip`:

```sh
pip install -r requirements/requirements.dev.txt
pip install -e .[dev]
gitlint install-hook
```

Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,29 @@ The initial version of this package was developed by [Patrick Littell](https://g
The best thing to do is install with pip `pip install g2p`.
This command will install the latest release published on [PyPI g2p releases](https://pypi.org/project/g2p/).

You can also clone the repo and pip install it locally:
You can also use [hatch](https://hatch.pypa.io/latest/) (see [hatch
installation instructions](https://hatch.pypa.io/latest/install/)) to
set up an isolated local development environment, which may be useful
if you wish to contribute new mappings:

```sh
$ git clone https://github.com/roedoejet/g2p.git
$ cd g2p
$ hatch shell
```

You can also simply install an "editable" version with `pip` (but it
is recommended to do this in a [virtual
environment](https://docs.python.org/3/library/venv.html) or a [conda
environment](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)):

```sh
$ git clone https://github.com/roedoejet/g2p.git
$ cd g2p
$ pip install -e .
```


## Usage

The easiest way to create a transducer is to use the `g2p.make_g2p` function.
Expand Down Expand Up @@ -251,7 +266,7 @@ mappings:

4. Add a mapping file. Look at the other mappings for examples, or visit the [g2p studio](https://g2p-studio.herokuapp.com) to practise your mappings.
Mappings are defined in either a CSV or json file. See [writing mapping files](#writing-mapping-files) for more info.
5. After installing your local version (`pip3 install -e .`), update with `g2p update`
5. Start a development shell with `hatch shell` (or install an editable version with `pip install -e .`) then update with `g2p update`
6. Add some tests in `g2p/testspublic/data/<YourIsoCode>.psv`. Each line in the file will run a test with the following structure: `<in_lang>|<out_lang>|<input_string>|<expected_output>`
7. Run `python3 run_tests.py langs` to make sure your tests pass.
8. Make sure you have [checked all the boxes](https://github.com/roedoejet/g2p/blob/main/.github/pull_request_template.md) and make a [pull request]((https://github.com/roedoejet/g2p/pulls)!
Expand Down
7 changes: 7 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ cd g2p

pip install -e .
```

To install in an isolated environment (recommended for development)
you may use [hatch](https://hatch.pypa.io/latest/):

```
hatch shell
```
5 changes: 0 additions & 5 deletions docs/requirements.txt

This file was deleted.

1 change: 0 additions & 1 deletion g2p/_version.py

This file was deleted.

17 changes: 15 additions & 2 deletions g2p/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import jsonschema
import yaml
from click.testing import CliRunner
from tqdm import tqdm

from g2p._version import VERSION
from g2p.app import APP
from g2p.cli import (
convert,
doctor,
Expand All @@ -39,7 +39,7 @@ class CliTest(TestCase):
"""Test suite for the g2p Command Line Interface"""

def setUp(self):
self.runner = APP.test_cli_runner()
self.runner = CliRunner()

def test_update(self):
result = self.runner.invoke(update)
Expand Down Expand Up @@ -97,6 +97,19 @@ def test_update(self):
result = self.runner.invoke(update, ["-i", bad_langs_dir, "-o", tmpdir])
self.assertEqual(result.exit_code, 0)

def test_schema_ci_version(self):
"""Make sure that the version (possibly a fake version - see
.github/workflows/tests.yml) matches the one in the schema."""
MAJOR_MINOR_VERSION = ".".join(VERSION.split(".")[:2])
self.assertTrue(
(
Path(__file__).parent.parent
/ "mappings"
/ ".schema"
/ f"g2p-config-schema-{MAJOR_MINOR_VERSION}.json"
).exists()
)

def test_update_schema(self):
result = self.runner.invoke(update_schema)
self.assertNotEqual(result.exit_code, 0)
Expand Down
5 changes: 3 additions & 2 deletions g2p/tests/test_doctor_expensive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from unittest import TestCase, main

from g2p.app import APP
from click.testing import CliRunner

from g2p.cli import doctor
from g2p.log import LOGGER
from g2p.mappings.langs.utils import check_ipa_known_segs
Expand All @@ -23,7 +24,7 @@ def test_doctor_cli(self):
# TODO: assert something more useful here...
# This test simulates calling "g2p doctor" on the command line with no arguments,
# which runs doctor on all mappings.
runner = APP.test_cli_runner()
runner = CliRunner()
with self.assertLogs(LOGGER, level="WARNING") as cm:
result = runner.invoke(doctor)
self.assertEqual(result.exit_code, 0)
Expand Down
4 changes: 2 additions & 2 deletions g2p/tests/test_studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
Test suite for the g2p-studio web app.
Requirements: python 3.8 and requirements/requirements.test.txt
Requirements: python 3.8 and `test` dependencies
Before running this test suite, launch the g2p-studio server:
minimal dev mode:
Expand All @@ -27,7 +27,7 @@
# flake8: noqa: C901
from unittest import IsolatedAsyncioTestCase, main

from playwright.async_api import async_playwright
from playwright.async_api import async_playwright # type: ignore

from g2p.app import APP, SOCKETIO
from g2p.log import LOGGER
Expand Down
5 changes: 0 additions & 5 deletions g2p/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
from unittest import TestCase, main

import yaml
from pep440 import is_canonical

from g2p import get_arpabet_langs
from g2p._version import VERSION
from g2p.exceptions import IncorrectFileType, RecursionError
from g2p.log import LOGGER
from g2p.mappings import Mapping, utils
Expand Down Expand Up @@ -299,9 +297,6 @@ def test_get_arpabet_langs(self):
self.assertIs(LANGS2, LANGS)
self.assertIs(LANG_NAMES2, LANG_NAMES)

def test_version_is_pep440_compliant(self):
self.assertTrue(is_canonical(VERSION))


if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions g2p/tests/test_z_local_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
from unittest import TestCase, main

import yaml
from click.testing import CliRunner

from g2p import exceptions
from g2p.app import APP
from g2p.cli import convert, generate_mapping
from g2p.mappings import Mapping
from g2p.mappings.utils import normalize
Expand All @@ -32,7 +32,7 @@

class LocalConfigTest(TestCase):
def setUp(self):
self.runner = APP.test_cli_runner()
self.runner = CliRunner()
self.mappings_dir = Path(PUBLIC_DIR) / "mappings"

def test_local_config(self):
Expand Down
Loading

0 comments on commit 6333947

Please sign in to comment.