Skip to content

Commit

Permalink
Update test cases to work on 3.11 (#178)
Browse files Browse the repository at this point in the history
* Update test cases to work on 3.11

* Read type parameter information from __parameters__ field for classes that inherit from Generic[T]

---------

Co-authored-by: Daniel Rosenwasser <[email protected]>
  • Loading branch information
hillary-mutisya and DanielRosenwasser authored Feb 20, 2024
1 parent d1d8db3 commit 9b92a5c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from types import NoneType, UnionType
import inspect
import typing
import typing_extensions
from dataclasses import MISSING, Field, dataclass
from types import NoneType, UnionType
Expand All @@ -20,8 +20,7 @@
Protocol,
Required,
TypeAlias,
TypeAliasType,
TypedDict,
TypeAliasType,
TypeGuard,
TypeVar,
Union,
Expand Down Expand Up @@ -99,7 +98,7 @@ def is_python_type_or_alias(origin: object) -> TypeGuard[type | TypeAliasType]:
]
)

_KNOWN_SPECIAL_BASES: frozenset[Any] = frozenset([TypedDict, Protocol])
_KNOWN_SPECIAL_BASES: frozenset[Any] = frozenset([typing.TypedDict, typing_extensions.TypedDict, Protocol])


@dataclass
Expand Down Expand Up @@ -294,6 +293,8 @@ def declare_type(py_type: object):

if hasattr(py_type, "__type_params__"):
type_params = [TypeParameterDeclarationNode(type_param.__name__) for type_param in py_type.__type_params__] # type: ignore
elif hasattr(py_type, "__parameters__"):
type_params = [TypeParameterDeclarationNode(type_param.__name__) for type_param in py_type.__parameters__]
else:
type_params = None

Expand Down
2 changes: 1 addition & 1 deletion python/tests/coffeeshop.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Cart(TypedDict):

result = python_type_to_typescript_schema(Cart)

print("// Entry point is: '{result.typescript_type_reference}'")
print(f"// Entry point is: '{result.typescript_type_reference}'")
print("// TypeScript Schema:\n")
print(result.typescript_schema_str)
if result.errors:
Expand Down
2 changes: 1 addition & 1 deletion python/tests/coffeeshop_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Cart(TypedDict):

result = python_type_to_typescript_schema(Cart)

print("// Entry point is: '{result.typescript_type_reference}'")
print(f"// Entry point is: '{result.typescript_type_reference}'")
print("// TypeScript Schema:\n")
print(result.typescript_schema_str)
if result.errors:
Expand Down
14 changes: 9 additions & 5 deletions python/tests/generic_alias_1.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from typing import Literal, TypedDict
from typing import Literal, TypedDict, TypeVar, Generic
from typing_extensions import TypeAliasType

from typechat import python_type_to_typescript_schema

class First[T](TypedDict):
T = TypeVar("T", covariant=True)


class First(Generic[T], TypedDict):
kind: Literal["first"]
first_attr: T


class Second[T](TypedDict):
class Second(Generic[T], TypedDict):
kind: Literal["second"]
second_attr: T


type FirstOrSecond[T] = First[T] | Second[T]
FirstOrSecond = TypeAliasType("FirstOrSecond", First[T] | Second[T], type_params=(T,))

result = python_type_to_typescript_schema(FirstOrSecond)

print("// Entry point is: '{result.typescript_type_reference}'")
print(f"// Entry point is: '{result.typescript_type_reference}'")
print("// TypeScript Schema:\n")
print(result.typescript_schema_str)
if result.errors:
Expand Down
13 changes: 8 additions & 5 deletions python/tests/generic_alias_2.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from typing import Literal, TypedDict
from typing import Literal, TypedDict, Generic, TypeVar

from typechat import python_type_to_typescript_schema
from typing_extensions import TypeAliasType

T = TypeVar("T", covariant=True)

class First[T](TypedDict):

class First(Generic[T], TypedDict):
kind: Literal["first"]
first_attr: T


class Second[T](TypedDict):
class Second(Generic[T], TypedDict):
kind: Literal["second"]
second_attr: T


type FirstOrSecond[T] = First[T] | Second[T]
FirstOrSecond = TypeAliasType("FirstOrSecond", First[T] | Second[T], type_params=(T,))


class Nested(TypedDict):
Expand All @@ -22,7 +25,7 @@ class Nested(TypedDict):

result = python_type_to_typescript_schema(Nested)

print("// Entry point is: '{result.typescript_type_reference}'")
print(f"// Entry point is: '{result.typescript_type_reference}'")
print("// TypeScript Schema:\n")
print(result.typescript_schema_str)
if result.errors:
Expand Down
18 changes: 9 additions & 9 deletions python/tests/hello_world.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from typing import Annotated, Literal, NotRequired, Optional, Required, Self, TypedDict

from typing import Annotated, Literal, NotRequired, Optional, Required, Self, TypedDict, TypeVar, Generic
from typing_extensions import TypeAliasType
from typechat import python_type_to_typescript_schema

T = TypeVar("T", covariant=True)


class C[T](TypedDict):
class C(Generic[T], TypedDict):
"This is a generic class named C."
x: NotRequired[T]
c: "C[int | float | None]"


type IndirectC = C[int]
IndirectC = TypeAliasType("IndirectC", C[int])


class D(C[str], total=False):
Expand All @@ -22,10 +24,8 @@ class D(C[str], total=False):

multiple_metadata: Annotated[str, None, str, "This comes from later metadata.", int]

nonclass = TypedDict("NonClass", {
"a": int,
"my-dict": dict[str, int]
})

nonclass = TypedDict("NonClass", {"a": int, "my-dict": dict[str, int]})


class E(C[str]):
Expand All @@ -34,7 +34,7 @@ class E(C[str]):
next: Self | None


type D_or_E = D | E
D_or_E = TypeAliasType("D_or_E", D | E)


result = python_type_to_typescript_schema(D_or_E)
Expand Down

0 comments on commit 9b92a5c

Please sign in to comment.