Skip to content

Commit

Permalink
feat: support type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
adhtruong committed Jan 17, 2024
1 parent 8f96365 commit 9589176
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
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 9589176

Please sign in to comment.