Skip to content

Commit

Permalink
feat: support type alias (#487)
Browse files Browse the repository at this point in the history
Co-authored-by: guacs <126393040+guacs@users.noreply.github.com>
  • Loading branch information
adhtruong and guacs authored Jan 19, 2024
1 parent 2b57706 commit 94ad561
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 26 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: "3.12"

- name: Install Pre-Commit
run: python -m pip install pre-commit && pre-commit install
Expand All @@ -34,7 +34,7 @@ jobs:
strategy:
fail-fast: true
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
pydantic-version: ["1.10", "2.0"]
sqla-version: ["1.4", "2"]
exclude:
Expand Down Expand Up @@ -78,15 +78,15 @@ jobs:
run: echo "PYTHONPATH=$PWD" >> $GITHUB_ENV

- name: Test
if: matrix.python-version != '3.11' || matrix.pydantic-version != '2.0' || matrix.sqla-version != '2'
if: matrix.python-version != '3.12' || matrix.pydantic-version != '2.0' || matrix.sqla-version != '2'
run: pdm run pytest tests

- name: Test with Coverage
if: matrix.python-version == '3.11' && matrix.pydantic-version == '2.0' && matrix.sqla-version == '2'
if: matrix.python-version == '3.12' && matrix.pydantic-version == '2.0' && matrix.sqla-version == '2'
run: pdm run pytest tests docs/examples --cov=. --cov-report=xml

- uses: actions/upload-artifact@v4
if: matrix.python-version == '3.11'
if: matrix.python-version == '3.12'
with:
name: coverage-xml
path: coverage.xml
Expand All @@ -103,12 +103,12 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: "3.12"

- uses: pdm-project/setup-pdm@v3
name: Set up PDM
with:
python-version: "3.11"
python-version: "3.12"
allow-python-prereleases: true
cache: true

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ jobs:

- uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: "3.12"

- uses: pdm-project/setup-pdm@v3
name: Set up PDM
with:
python-version: "3.11"
python-version: "3.12"
allow-python-prereleases: true
cache: true

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:

- uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: "3.12"

- uses: pdm-project/setup-pdm@v3
name: Set up PDM
with:
python-version: "3.11"
python-version: "3.12"
allow-python-prereleases: true
cache: true

Expand All @@ -42,12 +42,12 @@ jobs:

- uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: "3.12"

- uses: pdm-project/setup-pdm@v3
name: Set up PDM
with:
python-version: "3.11"
python-version: "3.12"
allow-python-prereleases: true
cache: true

Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: "3.11"
python: "3.12"
repos:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v3.0.0
Expand Down
11 changes: 9 additions & 2 deletions polyfactory/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
except ImportError:
NoneType = type(None) # type: ignore[misc,assignment]

from typing_extensions import get_args, get_origin
from typing_extensions import TypeAliasType, get_args, get_origin

from polyfactory.constants import TYPE_MAPPING
from polyfactory.utils.predicates import is_annotated, is_new_type, is_optional, is_safe_subclass, is_union
Expand Down Expand Up @@ -65,13 +65,20 @@ def unwrap_annotation(annotation: Any, random: Random) -> Any:
:returns: The unwrapped annotation.
"""
while is_optional(annotation) or is_new_type(annotation) or is_annotated(annotation):
while (
is_optional(annotation)
or is_new_type(annotation)
or is_annotated(annotation)
or isinstance(annotation, TypeAliasType)
):
if is_new_type(annotation):
annotation = unwrap_new_type(annotation)
elif is_optional(annotation):
annotation = unwrap_optional(annotation)
elif is_annotated(annotation):
annotation = unwrap_annotated(annotation, random=random)[0]
elif isinstance(annotation, TypeAliasType):
annotation = annotation.__value__

return annotation

Expand Down
10 changes: 1 addition & 9 deletions polyfactory/utils/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
from inspect import isclass
from typing import Any, Literal, NewType, Optional, TypeVar, Union, get_args

from typing_extensions import (
Annotated,
NotRequired,
ParamSpec,
Required,
TypeGuard,
_AnnotatedAlias,
get_origin,
)
from typing_extensions import Annotated, NotRequired, ParamSpec, Required, TypeGuard, _AnnotatedAlias, get_origin

from polyfactory.constants import TYPE_MAPPING

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Testing :: Unit",
Expand Down
27 changes: 26 additions & 1 deletion tests/test_new_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
from datetime import date
from decimal import Decimal
from typing import Any, Dict, List, NewType, Optional, Tuple, Union
from types import ModuleType
from typing import Any, Callable, Dict, List, NewType, Optional, Tuple, Union

import pytest
from pydantic import (
Expand All @@ -18,6 +20,7 @@
constr,
)

from polyfactory.factories.dataclass_factory import DataclassFactory
from polyfactory.factories.pydantic_factory import ModelFactory


Expand Down Expand Up @@ -137,3 +140,25 @@ class MyFactory(ModelFactory):

assert isinstance(result.conpositive_float_field, float)
assert result.conpositive_float_field > 0


@pytest.mark.skipif(sys.version_info < (3, 12), reason="3.12 only syntax")
def test_type_alias(create_module: Callable[[str], ModuleType]) -> None:
module = create_module(
"""
from typing import Literal
from dataclasses import dataclass
type LiteralAlias = Literal["a", "b"]
@dataclass
class A:
field: LiteralAlias
""",
)

factory = DataclassFactory.create_factory(module.A) # type: ignore[var-annotated]
result = factory.build()
assert result.field in {"a", "b"}

0 comments on commit 94ad561

Please sign in to comment.