pkn 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pkn/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  from .pydantic import *
2
2
 
3
- __version__ = "0.1.1"
3
+ __version__ = "0.1.3"
pkn/pydantic/__init__.py CHANGED
@@ -1 +1,3 @@
1
+ from .generic import *
2
+ from .paths import *
1
3
  from .roots import *
pkn/pydantic/paths.py ADDED
@@ -0,0 +1,41 @@
1
+ from importlib import import_module
2
+ from types import FunctionType, MethodType
3
+ from typing import Annotated, Optional
4
+
5
+ from pydantic import BeforeValidator, PlainSerializer
6
+
7
+ __all__ = (
8
+ "get_import_path",
9
+ "serialize_path_as_string",
10
+ "ImportPath",
11
+ "CallablePath",
12
+ )
13
+
14
+
15
+ def get_import_path(path: str) -> type:
16
+ if isinstance(path, type):
17
+ return path
18
+ elif isinstance(path, (FunctionType, MethodType)):
19
+ return path
20
+ if not isinstance(path, str):
21
+ raise TypeError(path)
22
+ module, call = path.rsplit(".", 1)
23
+ return getattr(import_module(module), call)
24
+
25
+
26
+ def serialize_path_as_string(value: type) -> Optional[str]:
27
+ if value is None:
28
+ return None
29
+ if hasattr(value, "__module__") and hasattr(value, "__qualname__"):
30
+ return f"{value.__module__}.{value.__qualname__}"
31
+ if hasattr(value, "__name__"):
32
+ return f"{value.__module__}.{value.__name__}"
33
+ if hasattr(value, "__class__") and hasattr(value.__class__, "__name__"):
34
+ return f"{value.__class__.__module__}.{value.__class__.__name__}"
35
+ if hasattr(value, "__class__") and hasattr(value.__class__, "__qualname__"):
36
+ return f"{value.__class__.__module__}.{value.__class__.__qualname__}"
37
+ raise TypeError(f"Could not derive module and name for {value}")
38
+
39
+
40
+ ImportPath = Annotated[type, BeforeValidator(get_import_path), PlainSerializer(serialize_path_as_string, when_used="json", return_type=str)]
41
+ CallablePath = Annotated[object, BeforeValidator(get_import_path), PlainSerializer(serialize_path_as_string, when_used="json", return_type=str)]
pkn/pydantic/roots.py CHANGED
@@ -1,12 +1,12 @@
1
- from typing import Dict as DictType, List as ListType
1
+ from typing import Dict as DictType, List as ListType, Optional, Union
2
2
 
3
- from pydantic import BaseModel, RootModel
3
+ from pydantic import BaseModel, Field, RootModel
4
4
 
5
5
  __all__ = ("Dict", "List")
6
6
 
7
7
 
8
8
  class Dict(RootModel):
9
- root: DictType[str, BaseModel]
9
+ root: Optional[DictType[str, Optional[Union[BaseModel, "Dict", "List", int, float, str]]]] = Field(default_factory=dict)
10
10
 
11
11
  def __iter__(self):
12
12
  return iter(self.root)
@@ -46,7 +46,7 @@ class Dict(RootModel):
46
46
 
47
47
 
48
48
  class List(RootModel):
49
- root: ListType[BaseModel]
49
+ root: Optional[ListType[Union[BaseModel, "Dict", "List", int, float, str]]] = Field(default_factory=list)
50
50
 
51
51
  def __iter__(self):
52
52
  return iter(self.root)
@@ -0,0 +1,27 @@
1
+ from pydantic import BaseModel
2
+
3
+ from pkn.pydantic import CallablePath, ImportPath
4
+
5
+
6
+ def foo():
7
+ return "foo"
8
+
9
+
10
+ class MyType(int): ...
11
+
12
+
13
+ class MyModel(BaseModel):
14
+ typ: ImportPath
15
+ foo: CallablePath
16
+
17
+
18
+ def test_get_import_path_inst():
19
+ m = MyModel(typ=MyType, foo=foo)
20
+ assert m.typ == MyType
21
+ assert m.foo == foo
22
+
23
+
24
+ def test_get_import_path_string():
25
+ m = MyModel(typ="pkn.tests.pydantic.test_paths.MyType", foo="pkn.tests.pydantic.test_paths.foo")
26
+ assert m.typ(1) == MyType(1)
27
+ assert m.foo() == foo()
@@ -27,6 +27,10 @@ class TestPydanticRoots:
27
27
  assert "a" not in d
28
28
  assert "b" not in d
29
29
 
30
+ def test_dict_types(self):
31
+ d = Dict({"a": MyModel(a=1, b="b"), "b": "abc", "c": 1, "d": 1.2, "e": List(), "f": Dict(), "g": None})
32
+ assert len(d) == 7
33
+
30
34
  def test_list(self):
31
35
  lst = List([MyModel(a=1, b="b"), MyModel(a=2, b="c")])
32
36
  assert len(lst) == 2
@@ -37,3 +41,7 @@ class TestPydanticRoots:
37
41
  del lst[0]
38
42
  assert len(lst) == 1
39
43
  assert lst[0] == MyModel(a=2, b="c")
44
+
45
+ def test_list_types(self):
46
+ d = List([MyModel(a=1, b="b"), "abc", 1, 1.2, List(), Dict(), None])
47
+ assert len(d) == 7
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pkn
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: My Pocket Knife
5
5
  Project-URL: Repository, https://github.com/1kbgz/pkn
6
6
  Project-URL: Homepage, https://github.com/1kbgz/pkn
@@ -0,0 +1,19 @@
1
+ pkn/__init__.py,sha256=tmrbhNq_zFJpjNhdW2g9_SkGXeOQ-iBLSjmwdkXMiXk,47
2
+ pkn/infra/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ pkn/infra/at.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ pkn/infra/common.py,sha256=Ymsc6F60EUJoCxGNUbj2xQ4OwsVh5IwcOXFnn_mRGYE,744
5
+ pkn/infra/journalctl.py,sha256=ZbmEAcqJG3IoLUTy8Uwkx341ga0t-56SXl4Wa-ls1DY,287
6
+ pkn/infra/launchctl.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ pkn/infra/reboot.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ pkn/infra/systemctl.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ pkn/pydantic/__init__.py,sha256=KQVHgirlymnu4lpOW_MxxfchnE2c2A_f9xGdS4UxAIc,65
10
+ pkn/pydantic/generic.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ pkn/pydantic/paths.py,sha256=aQ5kfYVjtVQrSbE9wJ0cCMqWFNM81V8ivTkLa6kLypU,1590
12
+ pkn/pydantic/roots.py,sha256=2jt5fbGGMDu7kEi6EYci21FFkLwxtEse1ldrAehTYyw,2575
13
+ pkn/tests/test_all.py,sha256=7jewXmZqf4xpkDjN1yc17oxTCHQIlxqWqgmm_l96kSI,60
14
+ pkn/tests/pydantic/test_paths.py,sha256=3w8tZgSgVnDMXUGG86OvSnPhtmimHutnGrQJy5hoqqQ,529
15
+ pkn/tests/pydantic/test_roots.py,sha256=uNfzA5jqIDK8WVD-yIJdwTJOdec7HwmCwI677rbi86I,1377
16
+ pkn-0.1.3.dist-info/METADATA,sha256=owb6aZ7lvYCCsQxPM5msraOknguWukRQNppH4rXq_98,2036
17
+ pkn-0.1.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
18
+ pkn-0.1.3.dist-info/licenses/LICENSE,sha256=88kGYCLkpYJl18wjFE_ggvtjqnPAm2zhiIgX12fG_ug,11345
19
+ pkn-0.1.3.dist-info/RECORD,,
@@ -1,17 +0,0 @@
1
- pkn/__init__.py,sha256=LDeokQfuV7SMJPfgn26xXrP5MYrsLrhhutyNp1D1XA8,47
2
- pkn/infra/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- pkn/infra/at.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- pkn/infra/common.py,sha256=Ymsc6F60EUJoCxGNUbj2xQ4OwsVh5IwcOXFnn_mRGYE,744
5
- pkn/infra/journalctl.py,sha256=ZbmEAcqJG3IoLUTy8Uwkx341ga0t-56SXl4Wa-ls1DY,287
6
- pkn/infra/launchctl.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
- pkn/infra/reboot.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- pkn/infra/systemctl.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- pkn/pydantic/__init__.py,sha256=M-lKT4BNEjAX0098TvC4U8CVnDTn4smN-fkMK9haopM,21
10
- pkn/pydantic/generic.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
- pkn/pydantic/roots.py,sha256=CWB6yrk75KB1MQnrfapG-C1M3IZE97HgQxN0ttuUIgE,2381
12
- pkn/tests/test_all.py,sha256=7jewXmZqf4xpkDjN1yc17oxTCHQIlxqWqgmm_l96kSI,60
13
- pkn/tests/pydantic/test_roots.py,sha256=aX8TU00DTurUX7kal4LXeJKGhzF2TQiCs7lCEsYL-eo,1070
14
- pkn-0.1.1.dist-info/METADATA,sha256=3gAiQmI1u5KgSLqmrT6dxIW5FLOeHEYxC1mi8B0NP4s,2036
15
- pkn-0.1.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
- pkn-0.1.1.dist-info/licenses/LICENSE,sha256=88kGYCLkpYJl18wjFE_ggvtjqnPAm2zhiIgX12fG_ug,11345
17
- pkn-0.1.1.dist-info/RECORD,,
File without changes