Skip to content

Commit

Permalink
SpecialForm
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinIsland committed Jan 4, 2024
1 parent 4a459f2 commit 9d12aea
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions basedtyping/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Untyped",
"Intersection",
"TypeForm",
"SpecialForm",
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -555,4 +556,30 @@ def __getitem__(self, parameters: object | tuple[object]) -> _BasedGenericAlias:
def f[T](t: TypeForm[T]) -> T: ...
reveal_type(f(int | str)) # int | str
reveal_type(f(int)) # int
""")

class _SpecialFormForm(_BasedSpecialForm, _root=True): # type: ignore[misc]
def __init__(self, doc: str):
self._name = "SpecialForm"
self._doc = self.__doc__ = doc

def __getitem__(self, parameters: object | tuple[object]) -> _BasedGenericAlias:
if not isinstance(parameters, tuple):
parameters = (parameters,)

return _BasedGenericAlias(self, parameters) # type: ignore[arg-type]


SpecialForm = _SpecialForm(doc="""\
A type that can be used to represent a ``SpecialForm``.
For example:
reveal_type(int) # type[int]
reveal_type(int | str) # SpecialForm[int | str]
def f[T](t: SpecialForm[T]) -> T: ...
reveal_type(f(int | str)) # int | str
reveal_type(f(int)) # error
""")
15 changes: 15 additions & 0 deletions tests/test_specialform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations

from basedtyping import SpecialForm


class A:
x: int


class B:
y: int


def test_typeform():
assert str(SpecialForm[Union[A, B]]) == f"basedtyping.TypeForm[Union[{A.__module__}.{A.__qualname__}, {B.__module__}.{B.__qualname__}]]"

0 comments on commit 9d12aea

Please sign in to comment.