Skip to content

Commit

Permalink
Migrate to use uv instead of poetry (#2288)
Browse files Browse the repository at this point in the history
* Migrate to use uv instead of poetry

Signed-off-by: Bernát Gábor <[email protected]>

* Support dependency groups in TOML on<3.11

Signed-off-by: Bernát Gábor <[email protected]>

* Check codespeed workaround

Signed-off-by: Bernát Gábor <[email protected]>

---------

Signed-off-by: Bernát Gábor <[email protected]>
  • Loading branch information
gaborbernat authored Jan 30, 2025
1 parent dd92cd0 commit 107168c
Show file tree
Hide file tree
Showing 19 changed files with 3,002 additions and 2,327 deletions.
1 change: 1 addition & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
branches: [ "main" ]
schedule:
- cron: "26 1 * * 4"
workflow_dispatch:

jobs:
analyze:
Expand Down
26 changes: 7 additions & 19 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,17 @@ jobs:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
# uv standalone build is not compatible with CodSpeedHQ
# https://github.com/astral-sh/uv/issues/11006
- uses: actions/setup-python@v4
with:
python-version: "3.12"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.7.1
virtualenvs-path: .venv
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-${{ runner.os }}-codspeed-3.12-v1-${{ hashFiles('**/poetry.lock') }}
python-version: "3.13"
- name: Install the latest version of uv
uses: astral-sh/setup-uv@v5
- name: Install dependencies
# if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction

run: uv sync
- name: Run benchmarks
uses: CodSpeedHQ/action@v3
with:
token: ${{ secrets.CODSPEED_TOKEN }}
run: poetry run pytest tests/ --codspeed -n auto
run: .venv/bin/pytest tests/ --codspeed -n auto
22 changes: 6 additions & 16 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,19 @@ on:
push:
branches:
- main
workflow_dispatch:

jobs:
build-deploy:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.8'
architecture: 'x64'

- uses: actions/cache@v4
- name: Install the latest version of uv
uses: astral-sh/setup-uv@v5
with:
path: |
~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements-docs.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install mkdocs
run: |
pip install -r requirements-docs.txt
python-version: 3.13
- name: Install dependencies
run: uv sync
- name: build site
run: mkdocs build --verbose --clean --strict
- name: Deploy
Expand Down
58 changes: 35 additions & 23 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,47 @@ on:
tags:
- '**'

env:
dists-artifact-name: python-package-distributions

jobs:
build-n-publish:
name: Build and publish Python 🐍 distributions 📦 to PyPI
if: "success() && startsWith(github.ref, 'refs/tags/')"
runs-on: ubuntu-24.04
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cache/pypoetry
~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Set up Python 3.8
uses: actions/setup-python@v4
fetch-depth: 0
- name: Install the latest version of uv
uses: astral-sh/setup-uv@v5
- name: Build package
run: uv build --python 3.13 --python-preference only-managed --sdist --wheel . --out-dir dist
- name: Store the distribution packages
uses: actions/upload-artifact@v4
with:
python-version: 3.8
- name: Build publish
run: |
python -m pip install poetry poetry-dynamic-versioning
poetry install
poetry build
- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
name: ${{ env.dists-artifact-name }}
path: dist/*

release:
name: Publish package to PyPI
needs:
- build
runs-on: ubuntu-latest
environment:
name: release
url: https://pypi.org/project/datamodel-code-generator/${{ github.ref_name }}
permissions:
id-token: write
steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: ${{ env.dists-artifact-name }}
path: dist/
- name: Publish to PyPI
uses: pypa/[email protected]
with:
user: __token__
password: ${{ secrets.pypi_password }}
attestations: true

push_to_registry:
name: Push Docker image to Docker Hub
if: "success() && startsWith(github.ref, 'refs/tags/')"
Expand Down
46 changes: 11 additions & 35 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ on:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

jobs:
test:
name: Test on Python ${{ matrix.python-version }} and ${{ matrix.os }} and isort ${{ matrix.isort-version }} and pydantic ${{ matrix.pydantic-version }}
name: Test ${{ matrix.python-version }}@${{ matrix.os }} with isort ${{ matrix.isort-version }}, pydantic ${{ matrix.pydantic-version }}, black ${{ matrix.black-version }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
python-version: ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8"]
os: [ubuntu-24.04, windows-latest, macos-latest]
isort-version: [4.3.21, 5.6.4, default]
black-version: [default]
Expand Down Expand Up @@ -89,53 +90,28 @@ jobs:
shell: bash
env:
OS: ${{ matrix.os }}
PYTHON: ${{ matrix.python-version }}
ISORT: ${{ matrix.isort-version }}
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
- name: Install the latest version of uv
uses: astral-sh/setup-uv@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.7.1
virtualenvs-path: .venv
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
if: matrix.os != 'windows-latest'
with:
path: .venv
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.pydantic-version }}-v1-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction -E http -E graphql
run: uv sync
- name: Install isort ${{ matrix.isort-version }} for Linux/macOS
if: "runner.os != 'Windows' && matrix.isort-version != 'default'"
run: |
poetry run pip install isort==${{ matrix.isort-version }}
run: uv pip install isort==${{ matrix.isort-version }}
- name: Install Pydantic ${{ matrix.pydantic-version }}
if: matrix.pydantic-version != 'default'
run: |
poetry run pip install pydantic=="${{ matrix.pydantic-version }}"
run: uv pip install pydantic=="${{ matrix.pydantic-version }}"
- name: Install Black ${{ matrix.black-version }}
if: matrix.black-version != 'default'
run: |
poetry run pip install black=="${{ matrix.black-version }}"
run: uv pip install black=="${{ matrix.black-version }}"
- name: Lint
if: matrix.pydantic-version == 'default'
run: |
source $VENV
./scripts/lint.sh
run: ./scripts/lint.sh
- name: Unit Test
run: |
source $VENV
./scripts/test.sh
run: ./scripts/test.sh
- name: Upload coverage to Codecov
if: matrix.os == 'ubuntu-24.04'
uses: codecov/codecov-action@v5
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
additional_dependencies:
- tomli
exclude: "^tests/|^CODE_OF_CONDUCT.md"
- repo: https://github.com/python-poetry/poetry
rev: 2.0.1
- repo: https://github.com/tox-dev/pyproject-fmt
rev: v2.5.0
hooks:
- id: poetry-check
- id: pyproject-fmt
2 changes: 1 addition & 1 deletion datamodel_code_generator/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Import(BaseModel):
reference_path: Optional[str] = None

@classmethod
@lru_cache()
@lru_cache
def from_full_path(cls, class_path: str) -> Import:
split_class_path: List[str] = class_path.split('.')
return Import(
Expand Down
2 changes: 1 addition & 1 deletion datamodel_code_generator/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def fall_back_to_nullable(self) -> bool:
return True


@lru_cache()
@lru_cache
def get_template(template_file_path: Path) -> Template:
loader = FileSystemLoader(str(TEMPLATE_DIR / template_file_path.parent))
environment: Environment = Environment(loader=loader)
Expand Down
2 changes: 1 addition & 1 deletion datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def type_has_null(self) -> bool:
return isinstance(self.type, list) and 'null' in self.type


@lru_cache()
@lru_cache
def get_ref_type(ref: str) -> JSONReference:
if ref[0] == '#':
return JSONReference.LOCAL
Expand Down
6 changes: 3 additions & 3 deletions datamodel_code_generator/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def context_variable(
_UNDER_SCORE_2: Pattern[str] = re.compile('([a-z0-9])([A-Z])')


@lru_cache()
@lru_cache
def camel_to_snake(string: str) -> str:
subbed = _UNDER_SCORE_1.sub(r'\1_\2', string)
return _UNDER_SCORE_2.sub(r'\1_\2', subbed).lower()
Expand Down Expand Up @@ -750,15 +750,15 @@ def get_valid_field_name_and_alias(
)


@lru_cache()
@lru_cache
def get_singular_name(name: str, suffix: str = SINGULAR_NAME_SUFFIX) -> str:
singular_name = inflect_engine.singular_noun(name)
if singular_name is False:
singular_name = f'{name}{suffix}'
return singular_name


@lru_cache()
@lru_cache
def snake_to_upper_camel(word: str, delimiter: str = '_') -> str:
prefix = ''
if word.startswith(delimiter):
Expand Down
4 changes: 2 additions & 2 deletions datamodel_code_generator/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def chain_as_tuple(*iterables: Iterable[T]) -> Tuple[T, ...]:
return tuple(chain(*iterables))


@lru_cache()
@lru_cache
def _remove_none_from_type(
type_: str, split_pattern: Pattern[str], delimiter: str
) -> List[str]:
Expand Down Expand Up @@ -207,7 +207,7 @@ def _remove_none_from_union(type_: str, use_union_operator: bool) -> str:
return f'{UNION_PREFIX}{UNION_DELIMITER.join(inner_types)}]'


@lru_cache()
@lru_cache
def get_optional_type(type_: str, use_union_operator: bool) -> str:
type_ = _remove_none_from_union(type_, use_union_operator)

Expand Down
14 changes: 5 additions & 9 deletions datamodel_code_generator/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,13 @@ def load_toml(path: Path) -> Dict[str, Any]: ...
from yaml import SafeLoader

try:
import tomllib

def load_toml(path: Path) -> Dict[str, Any]:
with path.open('rb') as f:
return tomllib.load(f)

from tomllib import load as load_tomllib
except ImportError:
import toml
from tomli import load as load_tomllib

def load_toml(path: Path) -> Dict[str, Any]:
return toml.load(path)
def load_toml(path: Path) -> Dict[str, Any]:
with path.open('rb') as f:
return load_tomllib(f)


SafeLoaderTemp = copy.deepcopy(SafeLoader)
Expand Down
26 changes: 16 additions & 10 deletions docs/development-contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,29 @@ We are waiting for your contributions to `datamodel-code-generator`.
$ git clone [email protected]:<your username>/datamodel-code-generator.git
$ cd datamodel-code-generator

## 2. Install [poetry](https://github.com/python-poetry/poetry)
$ curl -sSL https://install.python-poetry.org | python3 -
## 2. Install [uv](https://docs.astral.sh/uv/getting-started/installation/)
$ curl -LsSf https://astral.sh/uv/install.sh | sh

## 3. Create virtual environment
$ uv venv -p 3.13 --python-preference managed

## 3. Install dependencies
$ poetry install --all-extras
$ uv sync

## 4. Create new branch and rewrite code.
$ git checkout -b new-branch

## 5. Run unittest (you should pass all test and coverage should be 100%)
$ poetry run ./scripts/test.sh
## 5. Activate the virtual environment
$ source .venv/bin/python

## 6. Run unittest (you should pass all test and coverage should be 100%)
$ ./scripts/test.sh

## 6. Format code
$ poetry run ./scripts/format.sh
## 7. Format code
$ ./scripts/format.sh

## 7. Check lint (mypy)
$ poetry run ./scripts/lint.sh
## 8. Check lint (mypy)
$ ./scripts/lint.sh

## 8. Commit and Push...
## 9. Commit and Push...
```
Loading

0 comments on commit 107168c

Please sign in to comment.