From 1ce00f928a459dd1f6f5b58eb2fb7b712b8c919d Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Mon, 18 Dec 2023 00:47:04 +1000 Subject: [PATCH] `TypeForm` --- basedtyping/__init__.py | 21 ++++++++++++++++++++- tests/test_typeform.py | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/test_typeform.py diff --git a/basedtyping/__init__.py b/basedtyping/__init__.py index 1a06b72..1333f63 100644 --- a/basedtyping/__init__.py +++ b/basedtyping/__init__.py @@ -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, @@ -64,6 +65,7 @@ def override(arg, /): "issubform", "Untyped", "Intersection", + "TypeForm", ) if not TYPE_CHECKING: @@ -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 +""") diff --git a/tests/test_typeform.py b/tests/test_typeform.py new file mode 100644 index 0000000..7f79b8a --- /dev/null +++ b/tests/test_typeform.py @@ -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__}]" + )