Skip to content

Commit abc26a9

Browse files
0.2.7
1 parent b9cc534 commit abc26a9

File tree

10 files changed

+43
-22
lines changed

10 files changed

+43
-22
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v3.4.0
3+
rev: v4.4.0
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer
77
- id: trailing-whitespace
88

99
- repo: https://github.com/pycqa/isort
10-
rev: 5.8.0
10+
rev: v5.11.3
1111
hooks:
1212
- id: isort
1313
name: isort (python)
1414

15-
- repo: https://gitlab.com/PyCQA/flake8
16-
rev: '3.9.2'
15+
- repo: https://github.com/PyCQA/flake8
16+
rev: '6.0.0'
1717
hooks:
1818
- id: flake8

docs/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Packages required to build the documentation
22
sphinx >= 5.3, < 6
3-
sphinx-autodoc-typehints >= 1.17, < 2
4-
sphinx_rtd_theme >= 1, < 2
3+
sphinx-autodoc-typehints >= 1.20.1, < 2
4+
sphinx_rtd_theme >= 1.1.1, < 2

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ sub.cancel()
157157
```
158158

159159
# Changelog
160+
#### 0.2.7 (09.01.2023)
161+
- Fixed default generation for data types that inherit from python base types
162+
160163
#### 0.2.6 (21.12.2022)
161164
- Fixed an issue where the default yaml file would not be created properly when using aliases in container
162165

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
-r requirements_setup.txt
33

44
# testing dependencies
5-
pytest >= 7.1, < 8
6-
pre-commit >= 2, < 3
5+
pytest >= 7.2, < 8
6+
pre-commit >= 2.21, < 3

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def load_req() -> typing.List[str]:
6262
"Programming Language :: Python :: 3.8",
6363
"Programming Language :: Python :: 3.9",
6464
"Programming Language :: Python :: 3.10",
65+
"Programming Language :: Python :: 3.11",
6566
"Programming Language :: Python :: 3 :: Only",
6667
"Topic :: Software Development :: Libraries"
6768
],

src/easyconfig/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.6'
1+
__version__ = '0.2.7'

src/easyconfig/yaml/from_model.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from enum import Enum
22

33
from pydantic import BaseModel
4-
from pydantic.fields import ModelField
54

65
from easyconfig.__const__ import ARG_NAME_IN_FILE, MISSING
76
from easyconfig.yaml import CommentedMap, CommentedSeq
@@ -10,13 +9,19 @@
109

1110

1211
def _get_yaml_value(obj, parent_model: BaseModel, skip_none=True):
12+
if obj is None:
13+
return None
14+
1315
# Sometimes enum is used with int/str
1416
if isinstance(obj, Enum):
1517
return _get_yaml_value(obj.value, parent_model=parent_model, skip_none=skip_none)
1618

1719
# yaml native datatypes
18-
if isinstance(obj, (int, float, str, bool, bytes, NoneType)):
19-
return obj
20+
# Pydantic defines several validators that inherit from the python base type
21+
# Yaml can't represent those, so we cast them back to the native data type.
22+
for data_type in (int, float, str, bool, bytes):
23+
if isinstance(obj, data_type):
24+
return data_type(obj)
2025

2126
if isinstance(obj, BaseModel):
2227
return cmap_from_model(obj, skip_none=skip_none)
@@ -43,7 +48,7 @@ def _get_yaml_value(obj, parent_model: BaseModel, skip_none=True):
4348

4449
def cmap_from_model(model: BaseModel, skip_none=True) -> CommentedMap:
4550
cmap = CommentedMap()
46-
for obj_key, field in model.__fields__.items(): # type: str, ModelField
51+
for obj_key, field in model.__fields__.items():
4752
value = getattr(model, obj_key, MISSING)
4853
if value is MISSING or (skip_none and value is None):
4954
continue

tests/helper/my_path.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ class Path(_path_type):
1111
def __init__(self, *args, does_exist: bool = True, initial_value: Optional[str] = None, **kwargs):
1212
super(Path, self).__init__()
1313

14-
# Pathlib init
15-
drv, root, parts = self._parse_args(args)
16-
self._drv = drv
17-
self._root = root
18-
self._parts = parts
19-
self._init()
14+
if hasattr(Path, '_from_parts'):
15+
Path._from_parts(args)
16+
else:
17+
# Pathlib init
18+
drv, root, parts = self._parse_args(args)
19+
self._drv = drv
20+
self._root = root
21+
self._parts = parts
22+
self._init()
2023

2124
# Own Path implementation
2225
self.does_exist: bool = does_exist

tests/yaml/test_model_to_yaml.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from enum import Enum
33
from typing import List, Optional
44

5-
from pydantic import BaseModel, Field
5+
from pydantic import AnyHttpUrl, BaseModel, ByteSize, Field
66

77
from easyconfig.__const__ import ARG_NAME_IN_FILE
88
from easyconfig.yaml.from_model import cmap_from_model
@@ -17,6 +17,14 @@ class SimpleModel(BaseModel):
1717
assert dump_yaml(cmap_from_model(SimpleModel())) == 'a: 5\nb: 6\n'
1818

1919

20+
def test_base_overrides():
21+
class SimpleModel(BaseModel):
22+
size: ByteSize
23+
url: AnyHttpUrl = 'http://test.de'
24+
25+
assert dump_yaml(cmap_from_model(SimpleModel(size=ByteSize(1024)))) == 'size: 1024\nurl: http://test.de\n'
26+
27+
2028
def test_simple_model_complex_types():
2129
class SimpleModel(BaseModel):
2230
a: datetime = datetime(2000, 1, 1)

tox.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ envlist =
1010
[gh-actions]
1111
python =
1212
3.8: py38
13-
3.9: py39, flake, docs
14-
3.10: py10
13+
3.9: py39
14+
3.10: py10, flake, docs
15+
3.11: py11
1516

1617
[testenv]
1718
deps =

0 commit comments

Comments
 (0)