Skip to content

Commit

Permalink
TypeForm
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinIsland committed Dec 17, 2023
1 parent 4689c3c commit 1ce00f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
21 changes: 20 additions & 1 deletion basedtyping/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""The main ``basedtyping`` module. the types/functions defined here can be used at both type-time and at runtime."""

from __future__ import annotations

import typing
import contextlib
import sys
import typing
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -64,6 +65,7 @@ def override(arg, /):
"issubform",
"Untyped",
"Intersection",
"TypeForm",
)

if not TYPE_CHECKING:
Expand Down Expand Up @@ -525,3 +527,20 @@ def Intersection(self, parameters):
Intersection = _BasedSpecialForm("Intersection", doc="")
else:
Intersection: _SpecialForm


class _TypeFormSpecialForm(_BasedSpecialForm, _root=True):
def __init__(self, doc: str):
self._name = "TypeForm"
self._doc = self.__doc__ = doc

def __getitem__(self, parameters):
if not isinstance(parameters, tuple):
parameters = (parameters,)

return typing._GenericAlias(self, parameters)


TypeForm = _TypeFormSpecialForm(doc="""\
hi
""")
19 changes: 19 additions & 0 deletions tests/test_typeform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations

from basedtyping import TypeForm


class A:
x: int


class B:
y: int


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

0 comments on commit 1ce00f9

Please sign in to comment.