Skip to content

Commit

Permalink
Merge branch 'master' into fix-autocompletion-for-annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
george-zubrienko authored Jun 11, 2023
2 parents 4d2420b + df05a13 commit 2f58d9c
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 143 deletions.
41 changes: 40 additions & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ on:
push:
branches:
- master
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
pull_request:
branches:
- master
workflow_dispatch:

jobs:
test_and_publish:
test:
runs-on: ubuntu-latest
strategy:
max-parallel: 5
Expand All @@ -34,3 +36,40 @@ jobs:
- name: Test with pytest
run: |
pytest
publish:
needs: [test]
if: startsWith(github.ref, 'refs/tags')
environment: publish
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.x"
- name: Install pypa/build
run: >-
python3 -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
run: >-
python3 -m
build
--sdist
--wheel
--outdir dist/
.
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
- name: Publish distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1


259 changes: 138 additions & 121 deletions Pipfile.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,15 @@ from dataclasses_json import dataclass_json, config
from datetime import date
from marshmallow import fields

dataclasses_json.cfg.global_config.encoders[date] = date.isoformat
dataclasses_json.cfg.global_config.decoders[date] = date.fromisoformat

@dataclass_json
@dataclass
class DataClassWithIsoDatetime:
created_at: date = field(
metadata=config(
encoder= date.isoformat,
decoder= date.fromisoformat,
mm_field= fields.DateTime(format='iso')
))
created_at: date
modified_at: date
accessed_at: date
```

As you can see, you can **override** or **extend** the default codecs by providing a "hook" via a
Expand Down
2 changes: 0 additions & 2 deletions dataclasses_json/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
_undefined_parameter_action_safe)

A = TypeVar('A', bound="DataClassJsonMixin")
B = TypeVar('B')
C = TypeVar('C')
Fields = List[Tuple[str, Any]]


Expand Down
7 changes: 6 additions & 1 deletion dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ def _user_overrides_or_exts(cls):

def _encode_json_type(value, default=_ExtendedEncoder().default):
if isinstance(value, Json.__args__): # type: ignore
return value
if isinstance(value, list):
return [_encode_json_type(i) for i in value]
elif isinstance(value, dict):
return {k: _encode_json_type(v) for k, v in value.items()}
else:
return value
return default(value)


Expand Down
4 changes: 2 additions & 2 deletions publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ git tag "$1"
git push --tags
gh release create "$1"
portray on_github_pages
python setup.py sdist bdist_wheel
twine upload dist/*
# python setup.py sdist bdist_wheel
# twine upload dist/*
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='dataclasses-json',
version='0.5.7',
version='0.5.8',
packages=find_packages(exclude=('tests*',)),
package_data={"dataclasses_json": ["py.typed"]},
author='lidatong',
Expand All @@ -25,7 +25,7 @@
python_requires='>=3.6',
extras_require={
'dev': [
'pytest>=6.2.3',
'pytest>=7.2.0',
'ipython',
'mypy>=0.710',
'hypothesis',
Expand All @@ -39,5 +39,3 @@
scripts=['publish.py']
)



22 changes: 16 additions & 6 deletions tests/test_enum.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
from enum import Enum
from typing import Dict, List
from typing import Dict, List, Optional
import pytest

from dataclasses import dataclass
from dataclasses import dataclass, field

from dataclasses_json import dataclass_json

Expand All @@ -27,19 +27,25 @@ class MyStrEnum(str, Enum):
class DataWithEnum:
name: str
my_enum: MyEnum = MyEnum.STR3
enum_list: List[MyEnum] = field(default_factory=list)
nested: Dict[str, List[MyEnum]] = field(default_factory=dict)


d1 = DataWithEnum('name1', MyEnum.STR1)
d1_json = '{"name": "name1", "my_enum": "str1"}'
d1_json = '{"name": "name1", "my_enum": "str1", "enum_list": [], "nested": {}}'

# Make sure the enum is set to the default value defined by MyEnum
d2_using_default_value = DataWithEnum('name2')
d2_json = '{"name": "name2", "my_enum": "str3"}'
d2_json = '{"name": "name2", "my_enum": "str3", "enum_list": [], "nested": {}}'

d3_int = DataWithEnum('name1', MyEnum.INT1)
d3_int_json = '{"name": "name1", "my_enum": 1}'
d3_int_json = '{"name": "name1", "my_enum": 1, "enum_list": [], "nested": {}}'

d4_float = DataWithEnum('name1', MyEnum.FLOAT1)
d4_float_json = '{"name": "name1", "my_enum": 1.23}'
d4_float_json = '{"name": "name1", "my_enum": 1.23, "enum_list": [], "nested": {}}'

d5_list = DataWithEnum('name1', MyEnum.STR1, [MyEnum.STR2, MyEnum.STR3], nested={'enum_val': [MyEnum.STR1]})
d5_list_json = '{"name": "name1", "my_enum": "str1", "enum_list": ["str2", "str3"], "nested": {"enum_val": ["str1"]}}'


@dataclass_json
Expand Down Expand Up @@ -82,6 +88,10 @@ def test_data_with_enum_default_value(self):
def test_collection_with_enum(self):
assert container.to_json() == container_json

def test_enum_with_list(self):
assert d5_list.to_json() == d5_list_json, f'Actual: {d5_list.to_json()}, Expected: {d5_list_json}'
assert d5_list.to_dict(encode_json=True) == json.loads(d5_list_json), f'Actual: {d5_list.to_dict()}, Expected: {json.loads(d5_list_json)}'


class TestDecoder:
def test_data_with_enum(self):
Expand Down

0 comments on commit 2f58d9c

Please sign in to comment.