Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test cases to work on 3.11 #178

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from types import NoneType, UnionType, get_original_bases
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
12 changes: 8 additions & 4 deletions python/tests/generic_alias_1.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently TypeChat only understands generics using 3.12 syntax. It doesn't consider types that extend Generic[...] or generic protocols as adding type parameters. So I think we either find a way to make some of these tests only run in 3.12, or temporarily disable them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the TypeChat code to read type parameters for the classes that extend Generic[...] from the parameters field. With this, the generic_alias_1.py test now outputs type info correctly:

// Entry point is: 'FirstOrSecond'
// TypeScript Schema:

type FirstOrSecond<T> = First<T> | Second<T>

interface Second<T> {
    kind: "second";
    "second_attr": T;
}

interface First<T> {
    kind: "first";
    "first_attr": T;
}

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)

Expand Down
11 changes: 7 additions & 4 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 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
Loading