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

Migrate from setup.py to Poetry pyproject.toml for packaging and environment management #57

Merged
merged 10 commits into from
Dec 11, 2024
Merged
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
10 changes: 6 additions & 4 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python_version }}
- name: Setup python env
- name: Setup poetry
run: |
pip install pytest
pip install .
pip install poetry
- name: Install poetry env
run: |
poetry install
- name: Run pytest
env:
# set a placeholder OpenAI key (required by tests)
OPENAI_API_KEY: ABCD1234
run: python -m pytest
run: poetry run pytest
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ repos:
hooks:
- id: black
language_version: python3
- repo: https://github.com/python-poetry/poetry
rev: "1.8.3"
hooks:
- id: poetry-check
- repo: https://github.com/tox-dev/pyproject-fmt
rev: "v2.5.0"
hooks:
- id: pyproject-fmt
- repo: https://github.com/rhysd/actionlint
rev: v1.7.3
hooks:
- id: actionlint
20 changes: 16 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This project is governed by our [code of conduct](CODE_OF_CONDUCT.md). By partic

## Development

This project leverages development environments managed by a Python [`setup.py` file.](https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#setup-py)
This project leverages development environments managed by a Python [Poetry `pyproject.toml` file](https://python-poetry.org/docs/).
We use [pytest](https://docs.pytest.org/) for testing and [GitHub Actions](https://docs.github.com/en/actions) for automated tests.
[`pre-commit`](https://pre-commit.com/) is used to help lint or format code.
A [Conda environment](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html) is provided (via the file `environment.yml`) for convenience but is not required for development purposes.
Expand All @@ -22,8 +22,8 @@ A [Conda environment](https://conda.io/projects/conda/en/latest/user-guide/tasks
Perform the following steps to setup a Python development environment.

1. [Install Python](https://www.python.org/downloads/) (we recommend using [`pyenv`](https://github.com/pyenv/pyenv) or similar)
1. Install package, e.g. `pip install .` (from the root directory, referencing the `setup.py`)
1. Install pytest: `pip install pytest`
1. [Install Poetry](https://python-poetry.org/docs/#installation).
1. Install package, e.g. `poetry install`

### Linting

Expand All @@ -45,10 +45,22 @@ Work added to this project is automatically tested using [pytest](https://docs.p
Pytest is installed through the Poetry environment for this project.
We recommend testing your work before opening pull requests with proposed changes.

Some tests require a special environment key to be set: `OPENAI_API_KEY`.
This key is an openai.com API key tied to an account.
See here to [make an OpenAI account](https://openai.com/api/) and [create an API key](https://platform.openai.com/api-keys).
While the `OPENAI_API_KEY` environment variable must be set to a value for the testing suite to complete, any non-whitespace value will do. It doesn't have to be a valid API key, since all tests that actually query the API with the key are skipped by default.

If you want to run tests that query the actual OpenAI API, you must specify a valid API key for `OPENAI_API_KEY` . You can then execute pytest with the `--runcost` option, e.g. `poetry run pytest --runcost`, which will then use the specified key to run tests that query the API. Note that this will cost you money, typically around a cent or two per execution, depending on your choice of model. You can find detailed information about cost for each model per 1M tokens on the [OpenAI API Pricing Page](https://openai.com/api/pricing/) -- our test suite uses `gpt-3.5-turbo`. The tests also take significantly longer than the non-live test suite to complete, so it's recommended to leave them disabled them unless you know you need to test the live API.
You can set this key as follows:

```bash
export OPENAI_API_KEY=ABCD1234
```

You can run pytest on your work using the following example:

```sh
% python -m pytest
% poetry run pytest
```

## Making changes to this repository
Expand Down
Loading