Skip to content

Commit

Permalink
Merge pull request #5 from idiap/fixes
Browse files Browse the repository at this point in the history
Fix argument parsing, improve test coverage
  • Loading branch information
eginhard authored Nov 20, 2024
2 parents 6877ab3 + ef27e89 commit 65a58f7
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 16 deletions.
10 changes: 10 additions & 0 deletions .github/actions/setup-uv/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Setup uv
runs:
using: 'composite'
steps:
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: "0.5.1"
enable-cache: true
cache-dependency-glob: "**/pyproject.toml"
8 changes: 2 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: "0.4.27"
enable-cache: true
cache-dependency-glob: "**/pyproject.toml"
- name: Setup uv
uses: ./.github/actions/setup-uv
- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}
- name: Lint check
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/pypi-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ jobs:
if [[ "v$version" != "$tag" ]]; then
exit 1
fi
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: "0.4.27"
enable-cache: true
cache-dependency-glob: "**/pyproject.toml"
- name: Setup uv
uses: ./.github/actions/setup-uv
- name: Set up Python
run: uv python install
- name: Build sdist and wheel
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 👩‍✈️ Coqpit

[![PyPI - License](https://img.shields.io/pypi/l/coqpit-config)](https://github.com/idiap/coqui-ai-coqpit/blob/main/LICENSE.txt)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/coqpit-config)
[![PyPI - Version](https://img.shields.io/pypi/v/coqpit-config)](https://pypi.org/project/coqpit-config)
[![CI](https://github.com/idiap/coqui-ai-coqpit/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/idiap/coqui-ai-coqpit/actions/workflows/main.yml)

Simple, light-weight and no dependency config handling through python data
Expand Down
3 changes: 2 additions & 1 deletion coqpit/coqpit.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ def parse_known_args(
Returns:
List of unknown parameters.
"""
unknown: list[str] = []
if not args:
# If args was not specified, parse from sys.argv
parser = self.init_argparse(instance=self, arg_prefix=arg_prefix, relaxed_parser=relaxed_parser)
Expand All @@ -948,7 +949,7 @@ def parse_known_args(
parser = self.init_argparse(instance=self, arg_prefix=arg_prefix, relaxed_parser=relaxed_parser)
args, unknown = parser.parse_known_args(args)

self.parse_args(args)
self.parse_args(args, arg_prefix=arg_prefix)
return unknown

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "coqpit-config"
version = "0.1.0"
version = "0.1.1"
description = "Simple (maybe too simple), light-weight config management through python data-classes."
readme = "README.md"
requires-python = ">=3.9"
Expand Down
39 changes: 37 additions & 2 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional
from typing import Optional, Union

from coqpit.coqpit import Coqpit, _deserialize_list
import pytest

from coqpit.coqpit import Coqpit, _deserialize_list, _deserialize_primitive_types


@dataclass
Expand Down Expand Up @@ -66,3 +68,36 @@ def test_deserialize_list() -> None:
assert _deserialize_list([1, 2, 3], list[int]) == [1, 2, 3]
assert _deserialize_list([1, 2, 3], list[float]) == [1.0, 2.0, 3.0]
assert _deserialize_list([1, 2, 3], list[str]) == ["1", "2", "3"]


# TODO: `type: ignore` can probably be removed when switching to Python 3.10
# Union syntax (e.g. str | int)
def test_deserialize_primitive_type() -> None:
cases = (
(True, bool, True),
(False, bool, False),
("a", str, "a"),
("3", str, "3"),
(3, int, 3),
(3, float, 3.0),
(3, str, "3"),
(3.0, str, "3.0"),
(3, bool, True),
("a", Union[str, None], "a"),
("3", Union[str, None], "3"),
(3, Union[int, None], 3),
(3, Union[float, None], 3.0),
(None, Union[str, None], None),
(None, Union[int, None], None),
(None, Union[float, None], None),
(None, Union[str, None], None),
(float("inf"), float, float("inf")),
(float("inf"), int, float("inf")),
(float("-inf"), float, float("-inf")),
(float("-inf"), int, float("-inf")),
)
for value, field_type, expected in cases:
assert _deserialize_primitive_types(value, field_type) == expected # type: ignore[arg-type]

with pytest.raises(TypeError):
_deserialize_primitive_types(3, Coqpit)
44 changes: 44 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import Union

from coqpit.coqpit import _is_optional_field, _is_union, _is_union_and_not_simple_optional

# TODO: `type: ignore` can probably be removed when switching to Python 3.10
# Union syntax (e.g. str | int)


def test_is_union() -> None:
cases = (
(Union[str, int], True),
(Union[str, None], True),
(int, False),
(list[int], False),
(list[Union[str, int]], False),
)
for item, expected in cases:
assert _is_union(item) == expected # type: ignore[arg-type]


def test_is_union_and_not_simple_optional() -> None:
cases = (
(Union[str, int], True),
(Union[str, None], False),
(Union[list[int], None], False),
(int, False),
(list[int], False),
(list[Union[str, int]], False),
)
for item, expected in cases:
assert _is_union_and_not_simple_optional(item) == expected # type: ignore[arg-type]


def test_is_optional_field() -> None:
cases = (
(Union[str, int], False),
(Union[str, None], True),
(Union[list[int], None], True),
(int, False),
(list[int], False),
(list[Union[str, int]], False),
)
for item, expected in cases:
assert _is_optional_field(item) == expected # type: ignore[arg-type]

0 comments on commit 65a58f7

Please sign in to comment.