Skip to content

Commit

Permalink
Python 3.11, Pydantic 2.0, switch away from poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Jul 3, 2023
1 parent 7dee00d commit 2f11a99
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 1,610 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v2
Expand All @@ -21,8 +21,8 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | python3 -
poetry install
pip install --upgrade pip
pip install ".[dev]"
- name: Test with pytest
run: |
poetry run pytest
pytest
6 changes: 3 additions & 3 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
python-version: "3.10"
- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | python3 -
poetry install
pip install --upgrade pip
pip install -e ".[dev]"
- name: Generate coverage report
run: |
poetry run pytest --cov=tyro --cov-report=xml
pytest --cov=tyro --cov-report=xml
- name: Upload to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
# Build documentation
- name: Building documentation
run: |
curl -sSL https://install.python-poetry.org | python3 -
poetry install
poetry run pip install -r docs/requirements.txt
poetry run sphinx-build docs/source docs/build -b dirhtml
pip install --upgrade pip
pip install -e .
pip install -r docs/requirements.txt
sphinx-build docs/source docs/build -b dirhtml
# Deploy
- name: Deploy to GitHub Pages
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
python-version: "3.8"
- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | python3 -
poetry install
pip install --upgrade pip
pip install -e ".[dev]"
- name: Test with mypy
run: |
poetry run mypy --install-types --non-interactive .
mypy --install-types --non-interactive .
8 changes: 5 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ jobs:
python-version: '3.8'
- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | python3 -
poetry install
pip install --upgrade pip
pip install -e ".[dev]"
pip install build twine
- name: Strip unsupported tags in README
run: |
sed -i '/<!-- pypi-strip -->/,/<!-- \/pypi-strip -->/d' README.md
Expand All @@ -30,4 +31,5 @@ jobs:
PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }}
PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
poetry publish --build --username $PYPI_USERNAME --password $PYPI_PASSWORD
python -m build
twine upload --username $PYPI_USERNAME --password $PYPI_PASSWORD dist/*
3 changes: 1 addition & 2 deletions docs/source/examples/03_config_systems/01_base_configs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use the CLI to either override (existing) or fill in (missing) values.
from dataclasses import dataclass
from typing import Callable, Literal, Tuple, Union
from typing import Callable, Literal, Tuple
from torch import nn
from typing_extensions import Annotated
import tyro
Expand Down
8 changes: 4 additions & 4 deletions docs/source/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ pip install tyro
## Development

If you're interested in development, the recommended way to install `tyro` is
via [poetry](https://github.com/python-poetry/poetry).
via `pip`.

```bash
# Clone repository and install.
git clone [email protected]:brentyi/tyro.git
cd tyro
poetry install
python -m pip install -e ".[dev]"

# Run tests.
poetry run pytest
pytest

# Check types.
poetry run mypy --install-types .
mypy --install-types .
```
3 changes: 1 addition & 2 deletions examples/03_config_systems/01_base_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
"""

from dataclasses import dataclass
from typing import Callable, Literal, Tuple, Union
from typing import Callable, Literal, Tuple

from torch import nn
from typing_extensions import Annotated

import tyro

Expand Down
1,529 changes: 0 additions & 1,529 deletions poetry.lock

This file was deleted.

88 changes: 55 additions & 33 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,41 +1,63 @@
[tool.poetry]
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "tyro"
authors = [
{name = "brentyi", email = "[email protected]"},
]
version = "0.5.3"
description = "Strongly typed, zero-effort CLI interfaces"
authors = ["brentyi <[email protected]>"]
include = ["./tyro/**/*"]
readme = "README.md"
repository = "https://github.com/brentyi/tyro"
homepage = "https://github.com/brentyi/tyro"
documentation = "https://brentyi.github.io/tyro/"

[tool.poetry.dependencies]
python = "^3.7"
docstring-parser = "^0.14.1"
typing-extensions = "^4.3.0"
PyYAML = "^6.0"
"backports.cached-property" = { version = "^1.0.2", python = "~3.7" }
colorama = {version = "^0.4.0", platform = "win32"}
frozendict = "^2.3.4"
rich = ">=11.1.0"
shtab = "^1.5.6"

[tool.poetry.group.dev.dependencies]
pytest = "^7.1.2"
pytest-cov = "^3.0.0"
omegaconf = "^2.2.2"
attrs = "^21.4.0"
torch = "^1.10.0"
pyright = "^1.1.264"
mypy = "^0.991"
numpy = ">=1.20.0"
flax = "^0.6.0"
pydantic = "^1.10.2"
coverage = {extras = ["toml"], version = "^6.5.0"}
license = { text="MIT" }
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent"
]
dependencies = [
"docstring-parser>=0.14.1",
"typing-extensions>=4.3.0",
"PyYAML>=6.0",
"backports.cached-property>=1.0.2; python_version<'3.8'",
"colorama>=0.4.0; platform_system=='Windows'",
"frozendict>=2.3.4",
"rich>=11.1.0",
"shtab>=1.5.6"
]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
[project.optional-dependencies]
dev = [
"pytest>=7.1.2",
"pytest-cov>=3.0.0",
"omegaconf>=2.2.2",
"attrs>=21.4.0",
"torch>=1.10.0",
"pyright>=1.1.264",
"mypy>=0.991",
"numpy>=1.20.0",
"flax>=0.6.0",
"pydantic>=1.10.2",
"coverage[toml]>=6.5.0"
]

[project.urls]
"GitHub" = "https://github.com/brentyi/tyro"

[tool.setuptools.packages.find]
exclude = [
"tests*"
]

[tool.setuptools.package-data]
tyro = ["py.typed"]

[tool.isort]
profile = "black"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class DefaultInstanceSubparser:


def test_subparser_in_nested_with_metadata() -> None:
@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class A:
a: int

Expand Down Expand Up @@ -202,7 +202,7 @@ class Parent:


def test_subparser_in_nested_with_metadata_generic() -> None:
@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class A:
a: int

Expand Down Expand Up @@ -258,7 +258,7 @@ class Parent:


def test_subparser_in_nested_with_metadata_generic_alt() -> None:
@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class A:
a: int

Expand Down
4 changes: 2 additions & 2 deletions tests/test_generics_and_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def test_pculbertson() -> None:
# https://github.com/brentyi/tyro/issues/7
from typing import Union

@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class TypeA:
data: int

Expand All @@ -392,7 +392,7 @@ class Wrapper:
def test_annotated() -> None:
# https://github.com/brentyi/tyro/issues/7

@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class TypeA:
data: int

Expand Down
6 changes: 3 additions & 3 deletions tests/test_nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Nested:


def test_nested_default() -> None:
@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class B:
y: int = 1

Expand Down Expand Up @@ -869,7 +869,7 @@ class Wrapper:


def test_nested_in_subparser_override_with_default() -> None:
@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class Mnist:
binary: bool = False
"""Set to load binary version of MNIST dataset."""
Expand All @@ -888,7 +888,7 @@ class ImageNet:
}
)

@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class DatasetContainer:
dataset: Selector = Mnist() # type: ignore

Expand Down
4 changes: 3 additions & 1 deletion tyro/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ def _cli_impl(
dummy_field = cast(
dataclasses.Field,
dataclasses.field(
default=default if default is not None else dataclasses.MISSING
default_factory=lambda: default
if default is not None
else dataclasses.MISSING
),
)
f = dataclasses.make_dataclass(
Expand Down
50 changes: 36 additions & 14 deletions tyro/_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,21 +459,43 @@ def _field_list_from_pydantic(

# Handle pydantic models.
field_list = []
for pd_field in cls.__fields__.values(): # type: ignore
helptext = pd_field.field_info.description
if helptext is None:
helptext = _docstrings.get_field_docstring(cls, pd_field.name)

field_list.append(
FieldDefinition.make(
name=pd_field.name,
typ=pd_field.outer_type_,
default=(
MISSING_NONPROP if pd_field.required else pd_field.get_default()
),
helptext=helptext,
pydantic_version = float(getattr(pydantic, "__version__", "1.0"))
if pydantic_version < 2.0: # pragma: no cover
# Pydantic 1.xx.
for pd_field in cls.__fields__.values(): # type: ignore
helptext = pd_field.field_info.description
if helptext is None:
helptext = _docstrings.get_field_docstring(cls, pd_field.name)

field_list.append(
FieldDefinition.make(
name=pd_field.name,
typ=pd_field.outer_type_,
default=(
MISSING_NONPROP if pd_field.required else pd_field.get_default()
),
helptext=helptext,
)
)
else:
# Pydantic 2.xx.
for name, pd_field in cls.model_fields.items(): # type: ignore
helptext = pd_field.description
if helptext is None:
helptext = _docstrings.get_field_docstring(cls, name)

field_list.append(
FieldDefinition.make(
name=name,
typ=pd_field.annotation,
default=(
MISSING_NONPROP
if pd_field.is_required()
else pd_field.get_default(call_default_factory=True)
),
helptext=helptext,
)
)
)
return field_list


Expand Down
1 change: 1 addition & 0 deletions tyro/_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def narrow_container_types(
typ: TypeOrCallable, default_instance: Any
) -> TypeOrCallable:
"""TypeForm narrowing for containers. Infers types of container contents."""
print(typ, default_instance)
if typ is list and isinstance(default_instance, list):
typ = List.__getitem__(Union.__getitem__(tuple(map(type, default_instance)))) # type: ignore
elif typ is set and isinstance(default_instance, set):
Expand Down

0 comments on commit 2f11a99

Please sign in to comment.