|
| 1 | +import textwrap |
| 2 | + |
| 3 | +from typing import Callable, Literal |
| 4 | + |
| 5 | +from typemap.type_eval import eval_typing |
| 6 | +from typemap.typing import ( |
| 7 | + NewProtocol, |
| 8 | + Iter, |
| 9 | + Attrs, |
| 10 | + GetType, |
| 11 | + GetName, |
| 12 | + Member, |
| 13 | + Param, |
| 14 | + StrConcat, |
| 15 | +) |
| 16 | + |
| 17 | +from . import format_helper |
| 18 | + |
| 19 | + |
| 20 | +class Schema: |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +class Type: |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +class Expression: |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +# hmmmm... recursion with this sort of thing will be funny... |
| 33 | +# how will we handle the decorators or __init_subclass__ or what have you |
| 34 | + |
| 35 | + |
| 36 | +class Property: |
| 37 | + name: str |
| 38 | + required: bool |
| 39 | + multi: bool |
| 40 | + typ: Type |
| 41 | + expr: Expression | None |
| 42 | + |
| 43 | + |
| 44 | +type Schemaify[T] = NewProtocol[ |
| 45 | + *[p for p in Iter[Attrs[T]]], |
| 46 | + *[ |
| 47 | + Member[ |
| 48 | + StrConcat[Literal["get_"], GetName[p]], |
| 49 | + Callable[ |
| 50 | + [ |
| 51 | + Param[Literal["self"], Schemaify[T]], |
| 52 | + Param[Literal["schema"], Schema, Literal["keyword"]], |
| 53 | + ], |
| 54 | + GetType[p], |
| 55 | + ], |
| 56 | + Literal["ClassVar"], |
| 57 | + ] |
| 58 | + for p in Iter[Attrs[T]] |
| 59 | + ], |
| 60 | +] |
| 61 | + |
| 62 | + |
| 63 | +def test_schema_like_1(): |
| 64 | + tgt = eval_typing(Schemaify[Property]) |
| 65 | + fmt = format_helper.format_class(tgt) |
| 66 | + |
| 67 | + assert fmt == textwrap.dedent("""\ |
| 68 | + class Schemaify[tests.test_schemalike.Property]: |
| 69 | + name: str |
| 70 | + required: bool |
| 71 | + multi: bool |
| 72 | + typ: tests.test_schemalike.Type |
| 73 | + expr: tests.test_schemalike.Expression | None |
| 74 | + def get_name(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> str: ... |
| 75 | + def get_required(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> bool: ... |
| 76 | + def get_multi(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> bool: ... |
| 77 | + def get_typ(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> tests.test_schemalike.Type: ... |
| 78 | + def get_expr(self: Schemaify[tests.test_schemalike.Property], *, schema: tests.test_schemalike.Schema) -> tests.test_schemalike.Expression | None: ... |
| 79 | + """) |
0 commit comments