Skip to content

Commit

Permalink
Merge pull request #4 from adamboche/autoapi
Browse files Browse the repository at this point in the history
Autoapi
  • Loading branch information
adamboche committed Jun 8, 2019
2 parents 54dcd5d + 47bf9f1 commit ed07cb6
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.1.3
current_version = 0.1.4
commit = True
tag = True

Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ Overview
:alt: PyPI Package latest release
:target: https://pypi.org/pypi/marshmallow-union

.. |commits-since| image:: https://img.shields.io/github/commits-since/adamboche/python-marshmallow-union/v0.1.3.svg
.. |commits-since| image:: https://img.shields.io/github/commits-since/adamboche/python-marshmallow-union/v0.1.4.svg
:alt: Commits since latest release
:target: https://github.com/adamboche/python-marshmallow-union/compare/v0.1.3...master
:target: https://github.com/adamboche/python-marshmallow-union/compare/v0.1.4...master

.. |wheel| image:: https://img.shields.io/pypi/wheel/marshmallow-union.svg
:alt: PyPI Wheel
Expand Down
28 changes: 25 additions & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,61 @@
#
# pip-compile --output-file=dev-requirements.txt dev-requirements.in
#
alabaster==0.7.12 # via sphinx
ansicolors==1.0.2 # via cuvner
appdirs==1.4.3 # via black
argh==0.26.2 # via watchdog
astroid==2.2.5 # via pylint
atomicwrites==1.3.0 # via pytest
attrs==19.1.0 # via black, pytest
babel==2.7.0 # via sphinx
black==19.3b0
bump2version==0.5.10
certifi==2019.3.9 # via requests
chardet==3.0.4 # via requests
click==7.0 # via black, cuvner
coverage==4.5.3
cuvner==18.0.1
docutils==0.14 # via sphinx
filelock==3.0.12 # via tox
idna==2.8 # via requests
imagesize==1.1.0 # via sphinx
importlib-metadata==0.17 # via pluggy, pytest
incremental==17.5.0 # via cuvner
isort==4.3.20 # via pylint
jinja2==2.10.1 # via sphinx
lazy-object-proxy==1.4.1 # via astroid
markupsafe==1.1.1 # via jinja2
mccabe==0.6.1 # via pylint
more-itertools==7.0.0 # via pytest
packaging==19.0 # via pytest
packaging==19.0 # via pytest, sphinx
pathtools==0.1.2 # via watchdog
pex==1.6.7
pluggy==0.12.0 # via pytest, tox
py==1.8.0 # via pytest, tox
pygments==2.4.2 # via cuvner
pygments==2.4.2 # via cuvner, sphinx
pylint==2.3.1
pyparsing==2.4.0 # via packaging
pytest==4.6.2
pytz==2019.1 # via babel
pyyaml==5.1.1 # via watchdog
requests==2.22.0 # via sphinx
six==1.12.0 # via astroid, cuvner, packaging, pytest, tox
snowballstemmer==1.2.1 # via sphinx
sphinx-autodoc-typehints==1.6.0
sphinx-rtd-theme==0.4.3
sphinx==2.1.0 # via sphinx-autodoc-typehints, sphinx-rtd-theme
sphinxcontrib-applehelp==1.0.1 # via sphinx
sphinxcontrib-devhelp==1.0.1 # via sphinx
sphinxcontrib-htmlhelp==1.0.2 # via sphinx
sphinxcontrib-jsmath==1.0.1 # via sphinx
sphinxcontrib-qthelp==1.0.2 # via sphinx
sphinxcontrib-serializinghtml==1.1.3 # via sphinx
toml==0.10.0 # via black, tox
tox==3.12.1
typed-ast==1.4.0 # via astroid
unidiff==0.5.5 # via cuvner
urllib3==1.25.3 # via requests
versioneer==0.18
virtualenv==16.6.0 # via tox
watchdog==0.9.0 # via cuvner
Expand All @@ -45,4 +67,4 @@ wrapt==1.11.1 # via astroid
zipp==0.5.1 # via importlib-metadata

# The following packages are considered to be unsafe in a requirements file:
# setuptools==41.0.1 # via tox
# setuptools==41.0.1 # via sphinx, tox
8 changes: 6 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
"sphinx_autodoc_typehints", # Must come *after* sphinx.ext.napoleon.
"sphinx.ext.autosummary",
"sphinx.ext.coverage",
"sphinx.ext.doctest",
Expand All @@ -26,7 +28,7 @@
year = "2019"
author = "akb"
copyright = "{0}, {1}".format(year, author)
version = release = "version = release = 0.1.3"
version = release = "version = release = 0.1.4"

pygments_style = "trac"
templates_path = ["."]
Expand All @@ -48,4 +50,6 @@

napoleon_use_ivar = True
napoleon_use_rtype = False
napoleon_use_param = False
napoleon_use_param = True

autoapi_dirs = ["../src/marshmallow_union"]
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sphinx>=1.3
sphinx-rtd-theme
sphinx-autodoc-typehints
-e .
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def read(*names, **kwargs):

setup(
name="marshmallow-union",
version="0.1.3",
version="version=0.1.4",
description="Union fields for marshmallow.",
long_description="%s\n%s"
% (
Expand Down
54 changes: 52 additions & 2 deletions src/marshmallow_union/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
from ._core import Union
import typing as t

__version__ = "__version__ = 0.1.3"
import marshmallow
import marshmallow.exceptions


class Union(marshmallow.fields.Field):
"""Field that accepts any one of multiple fields.
Each argument will be tried until one succeeds.
Args:
fields: The list of candidate fields to try.
reverse_serialize_candidates: Whether to try the candidates in reverse order when
serializing.
"""

def __init__(
self,
fields: t.List[marshmallow.fields.Field],
reverse_serialize_candidates: bool = False,
*args,
**kwargs
):
self._candidate_fields = fields
self._reverse_serialize_candidates = reverse_serialize_candidates
super().__init__(*args, **kwargs)

def _serialize(self, value, attr, obj, **kwargs):
errors = []

fields = self._candidate_fields
if self._reverse_serialize_candidates:
fields = reversed(fields)

for candidate_field in fields:
try:
return candidate_field._serialize(value, attr, obj, **kwargs)
except marshmallow.exceptions.ValidationError as e:
errors.append(e.messages)
raise marshmallow.exceptions.ValidationError(message=errors, field_name=attr)

def _deserialize(self, value, attr, obj, **kwargs):
errors = []
for candidate_field in self._candidate_fields:
try:
return candidate_field._deserialize(value, attr, obj, **kwargs)
except marshmallow.exceptions.ValidationError as e:
errors.append(e.messages)
raise marshmallow.exceptions.ValidationError(message=errors, field_name=attr)


__version__ = "__version__ = 0.1.4"
50 changes: 0 additions & 50 deletions src/marshmallow_union/_core.py

This file was deleted.

3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
; a generative tox configuration, see: https://tox.readthedocs.io/en/latest/config.html#generative-envlist

[tox]

envlist =
clean,
check,
Expand Down Expand Up @@ -65,7 +66,7 @@ deps =

[testenv:docs]
deps =
-r{toxinidir}/docs/requirements.txt
-r{toxinidir}/docs/requirements.txt
commands =
sphinx-apidoc -o docs/reference -f src
sphinx-build {posargs:-E} -b html docs dist/docs
Expand Down

0 comments on commit ed07cb6

Please sign in to comment.