Skip to content

Commit

Permalink
✨ feat: PdtConverter Pydantic Annotated[T, Feild(...)] 支持
Browse files Browse the repository at this point in the history
  • Loading branch information
luxuncang committed Jun 6, 2024
1 parent fbff2ac commit bd5acbe
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 58 deletions.
38 changes: 17 additions & 21 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ pdm add typegraph3
注册同步转换器并使用:

```python
from typegraph import TypeConverter
from typegraph import PdtConverter

converter = TypeConverter()
converter = PdtConverter()

@converter.register_converter(int, str)
def int_to_str(value: int) -> str:
Expand All @@ -56,9 +56,9 @@ print(result)

```python
import asyncio
from typegraph import TypeConverter
from typegraph import PdtConverter

converter = TypeConverter()
converter = PdtConverter()

@converter.async_register_converter(str, int)
async def str_to_int(value: str) -> int:
Expand Down Expand Up @@ -139,7 +139,7 @@ def test(a: str):

d = {"name": "John", "phone": "123", "address": "123"}

t.convert([d], list[str], debug=True, protocol=True)
t.convert([d], list[str], debug=True)
```

`t.show_mermaid_graph()`
Expand All @@ -152,7 +152,7 @@ dict-->B
Person-->str
```

`t.show_mermaid_graph(protocol=True)`
`t.show_mermaid_graph()`

```mermaid
graph TD;
Expand All @@ -175,9 +175,9 @@ Converting dict[str, str] to <class 'str'> using [<class 'dict'>, <class '__main
#### 同步

```python
from typegraph import TypeConverter
from typegraph import PdtConverter

converter = TypeConverter()
converter = PdtConverter()

@converter.register_converter(str, int)
def str_to_int(value: str) -> int:
Expand All @@ -194,10 +194,10 @@ print(result)
#### 异步

```python
from typegraph import TypeConverter
from typegraph import PdtConverter
import asyncio

converter = TypeConverter()
converter = PdtConverter()

@converter.async_register_converter(str, int)
async def str_to_int(value: str) -> int:
Expand All @@ -219,7 +219,7 @@ asyncio.run(test_async())
提供了单元测试,以确保库的正确功能。运行测试:

```bash
pytest test_switch.py
pdm test
```

测试覆盖了:
Expand All @@ -233,18 +233,14 @@ pytest test_switch.py
您可以可视化类型转换图:

```python
from typegraph import TypeConverter
from typegraph import PdtConverter

t = TypeConverter()
t = PdtConverter()

class Test:
def __init__(self, t):
self.t = t

class TestFloat(float):
...


@t.register_converter(float, Test)
def str_to_Test(input_value):
return Test(input_value)
Expand All @@ -257,7 +253,7 @@ def B_to_float(input_value):
async def float_to_str(input_value):
return str(input_value)

t.show_mermaid_graph(subclass=True)
t.show_mermaid_graph()
```


Expand All @@ -266,7 +262,6 @@ graph TD;
float-->Test
float-->str
Test-->float
TestFloat-.->float
```

图将使用 mermaid 语法显示,您可以在线渲染或在支持的环境中(如 Jupyter Notebooks)进行渲染。
Expand All @@ -275,11 +270,12 @@ TestFloat-.->float

- [X] 子类类型 (SubClass type)
- [X] 联合类型 (Union type)
- [X] 注解类型 (Annotated type)
- [X] 注解类型 (Annotated type) `Pydantic Annotated[T, Feild(...)]`
- [X] 结构类型 (Structural type)
- [X] 协议类型 (Protocol type) (只支持输入类型)
- [X] 协议类型 (Protocol type)
- [X] 字典类型 (TypedDict type)
- [X] 泛型类型 (Generic type)
- [X] 数据类 (Dataclass/BaseModel)

## 许可
此项目使用 MIT 许可证。
Expand Down
42 changes: 19 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ pdm add typegraph3
Register and use a synchronous converter:

```python
from typegraph import TypeConverter
from typegraph import PdtConverter

converter = TypeConverter()
converter = PdtConverter()

@converter.register_converter(int, str)
def int_to_str(value: int) -> str:
Expand All @@ -56,9 +56,9 @@ Register and use an asynchronous converter:

```python
import asyncio
from typegraph import TypeConverter
from typegraph import PdtConverter

converter = TypeConverter()
converter = PdtConverter()

@converter.async_register_converter(str, int)
async def str_to_int(value: str) -> int:
Expand All @@ -77,9 +77,9 @@ asyncio.run(test_async_conversion())
from typing import Protocol, TypedDict, runtime_checkable
from dataclasses import dataclass

from typegraph import TypeConverter
from typegraph import PdtConverter

t = TypeConverter()
t = PdtConverter()

class Person(Protocol):
name: str
Expand Down Expand Up @@ -139,7 +139,7 @@ def test(a: str):

d = {"name": "John", "phone": "123", "address": "123"}

t.convert([d], list[str], debug=True, protocol=True)
t.convert([d], list[str], debug=True)
```

`t.show_mermaid_graph()`
Expand All @@ -152,7 +152,7 @@ dict-->B
Person-->str
```

`t.show_mermaid_graph(protocol=True)`
`t.show_mermaid_graph()`

```mermaid
graph TD;
Expand All @@ -175,9 +175,9 @@ Automatically convert function arguments based on type annotations:
#### Synchronous

```python
from typegraph import TypeConverter
from typegraph import PdtConverter

converter = TypeConverter()
converter = PdtConverter()

@converter.register_converter(str, int)
def str_to_int(value: str) -> int:
Expand All @@ -194,10 +194,10 @@ print(result)
#### Asynchronous

```python
from typegraph import TypeConverter
from typegraph import PdtConverter
import asyncio

converter = TypeConverter()
converter = PdtConverter()

@converter.async_register_converter(str, int)
async def str_to_int(value: str) -> int:
Expand All @@ -219,7 +219,7 @@ asyncio.run(test_async())
Unit tests are provided to ensure the library functions correctly. Run the tests:

```bash
pytest test_switch.py
pdm test
```

Tests cover:
Expand All @@ -233,18 +233,14 @@ Tests cover:
You can visualize the type conversion graph:

```python
from typegraph import TypeConverter
from typegraph import PdtConverter

t = TypeConverter()
t = PdtConverter()

class Test:
def __init__(self, t):
self.t = t

class TestFloat(float):
...


@t.register_converter(float, Test)
def str_to_Test(input_value):
return Test(input_value)
Expand All @@ -257,15 +253,14 @@ def B_to_float(input_value):
async def float_to_str(input_value):
return str(input_value)

t.show_mermaid_graph(subclass=True)
t.show_mermaid_graph()
```

```mermaid
graph TD;
float-->Test
float-->str
Test-->float
TestFloat-.->float
```

The graph will be displayed using mermaid syntax, which can be rendered online or in supported environments like Jupyter Notebooks.
Expand All @@ -274,11 +269,12 @@ The graph will be displayed using mermaid syntax, which can be rendered online o

- [X] Subclass type
- [X] Union type
- [X] Annotated type
- [X] Annotated type `Pydantic Annotated[T, Feild(...)]`
- [X] Structural type
- [X] Protocol type (input types only)
- [X] Protocol type
- [X] TypedDict type
- [X] Generic type
- [X] Dataclass (Dataclass/BaseModel)

## License
This project is licensed under the MIT License.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "typegraph3"
version = "0.1.2rc2"
version = "0.1.2rc3"
description = "Type Auto Switch"
authors = [
{name = "luxuncang", email = "[email protected]"},
Expand Down
Loading

0 comments on commit bd5acbe

Please sign in to comment.