Commit graph

89 commits

Author SHA1 Message Date
Dave Shawley
42bc4c9292
Add support for encoding namedtuples as tuples 2021-11-08 10:52:24 -05:00
Dave Shawley
f5725ac6dd
Add support for encoding array.array instances 2021-11-08 10:51:59 -05:00
Dave Shawley
b15f131df1
Add support for encoding pathlib.Path 2021-11-04 07:15:34 -04:00
Dave Shawley
c2c2408786
Remove usage of annotations future.
Though we weren't using the affected feature, I am going to avoid
``from __future__ import annotations`` since it is likely going to be
replaced with a different approach.  The only two things that we were
getting out of it is using `dict[]` instead of `typing.Dict[]` and using
`typing.Literal` in Python 3.7.  Neither is compelling IMO.

https://discuss.python.org/t/type-annotations-pep-649-and-pep-563/
2021-11-03 06:53:07 -04:00
Dave Shawley
680f4d8a45
Add type_info.JsonDumpable. 2021-11-03 06:53:06 -04:00
Dave Shawley
e01bc1889d
Add support for encoding ipaddress.IPv4/6Address 2021-11-03 06:53:04 -04:00
Dave Shawley
1fb8dff9e2
Add support for encoding dataclasses. 2021-11-03 06:52:33 -04:00
Dave Shawley
be5eb94cbf
Add support for encoding decimal.Decimal as float.
This could be lossy on the JSON side since the precision is up to the
reader as well.  I implemented the encoding as a cast to `float` so
there will almost certainly be some loss of precision.
2021-11-03 06:49:40 -04:00
Dave Shawley
65d6693d62
Rename some type aliases.
I found the aliases of the form `typing.Supports*` in the Standard
Library today.  Decided to rename `HasSettings` and `DefinesIsoFormat`
to match.
2021-10-28 08:42:57 -04:00
Dave Shawley
675ffbdf98
Change form encoder to default to stringify.
This matches what urlencode would do and is will remove surprises where
a value is supported by the other transcoders but not the form encoder.
2021-10-15 07:18:01 -04:00
Dave Shawley
7f03f29175
Accept encoding options in FormUrlEncodingOptions. 2021-10-14 07:58:04 -04:00
Dave Shawley
198e73b6ef
Implement form encoding of sequence values.
This is off by default to match the `doseq` parameter of
urllib.parse.urlencode.
2021-10-14 07:54:32 -04:00
Dave Shawley
1fe22df719
Gracefully handle transcoder encoding failures.
Since the form encoder refuses to handle nested sequences, the content
mixin explicitly handles this case instead of letting the unhandled
exception bubble up.
2021-10-14 07:12:08 -04:00
Dave Shawley
2df3aade3c
Improve documentation for FormUrlEncodedTranscoder. 2021-10-14 07:12:08 -04:00
Dave Shawley
ee66c5cadb
Optimize the URL encoding inner loop.
I rearranged the _encode method branching a bit.  This resulted in a
little more than a 12% gain.  Still slower than JSON or msgpack but it's
in python instead of C.
2021-10-14 07:12:08 -04:00
Dave Shawley
25ec09972c
Add uuid support to FormUrlEncodedTranscoder. 2021-10-14 07:12:07 -04:00
Dave Shawley
4fd3864c04
Rejected serialization of non-pair sequences.
This required a bit of change in _encode to detect non-pair sequences
that are also not strings or byte strings.
2021-10-14 07:12:07 -04:00
Dave Shawley
e99d41a8b4
Refuse to serialize None, True, False.
If someone explicitly removes the literal mappings, then refuse to
serialize None, True, and False instead of guessing.  Note that I
explicitly included True and False in the branch logic since bool is a
subclass of int but str(True) != str(int(True)) :/
2021-10-14 07:12:07 -04:00
Dave Shawley
3aa08b7435
Implement serialization of primitive values.
This isn't covered by the serialization specification but is accounted
for in the deserialization side.
2021-10-14 07:12:07 -04:00
Dave Shawley
716bff0d7f
Add application/x-www-formurlencoded transcoder.
I ended up not using urllib.parse functions since they do not implement
the specification.  The only difference is that the specification
requires that "~" is encoded.

NB - this commit is incomplete since it does not handle calling the
transcoder on simple objects.  mypy will quite correctly fail.
2021-10-14 07:12:01 -04:00
Dave Shawley
2047fe8d16
Increase test coverage to 100% 2021-10-04 07:30:42 -04:00
Dave Shawley
9ff5ca3781
Fail gracefully when transcoder is missing.
If there is no transcoder for the default content type, sending a
response would fail with a "500 Internal Server Error" due to an
*unhandled* KeyError.  This commit catches the KeyError and produces
an explicit "500 Internal Server Error" with an appropriate error
message being logged.
2021-10-03 15:52:48 -04:00
Dave Shawley
d605acb5b7
Deprecate not configuring a default content type. 2021-10-03 15:52:48 -04:00
Dave Shawley
4dd46eda5b
Return 406 Not Acceptable appropriately.
If there is no default content type and nothing matches in the Accept
header, previous versions would fail with an Internal Server Error
because of an unhandled exception.  Instead we should be returning
a "406 Not Acceptable".
2021-10-03 15:52:47 -04:00
Dave Shawley
36244966e4
Subtly prefer add_transcoder in docs. 2021-10-03 15:52:47 -04:00
Dave Shawley
4a0ca5e390
Documentation updates for typing.
The changes to `api.rst` are particularly important since that is where
I describe the externally available type annotations.
2021-10-03 15:52:47 -04:00
Dave Shawley
acc0a1db14
Add type annotations.
The next commit is a pile of documentation updates that I didn't want
cluttering up this commit.
2021-10-03 15:52:43 -04:00
Dave Shawley
de43997df5
Reformat using yapf.
https://github.com/sprockets/sprockets.mixins.mediatype/issues/35
2021-09-19 09:58:08 -04:00
Dave Shawley
7472c399fb
Lint ignore some of the weird stuff in __init__.
Adjusted flake8 & coverage ignores for the namespace package cruft and
the import guards in the __init__ files.
2021-09-16 09:54:16 -04:00
Dave Shawley
e9e5f25925
Move metadata from setup.py into setup.cfg. 2021-09-16 09:49:42 -04:00
Dave Shawley
f6f84bdd2c 3.0.4: Reject invalid Content-Type headers. 2020-11-02 09:21:08 -05:00
Dave Shawley
21c6cb73fe Invalid content-type --> 400 Bad Request.
ietfparse raises a `ValueError` from `parse_content_type` which was
uncaught and resulted in an internal server error.
2020-10-29 13:50:33 -04:00
Dave Shawley
5755792959 3.0.3: Preparing for Python 3.9. 2020-09-14 08:10:20 -04:00
Andrew Rabert
f7683262f8 Use non-deprecated collections.abc
See https://docs.python.org/3.9/whatsnew/3.9.html#you-should-check-for-deprecationwarning-in-your-code
2020-07-14 10:04:30 -04:00
Dave Shawley
3dadd00c64 Metadata bumps for 3.0.2. 2020-05-04 16:55:22 -04:00
amberheilman
0b54131f65
Change log level on decode failure log
Don't print tracebacks when failing to decode the body.
2020-05-04 16:46:51 -04:00
Gavin M. Roy
03aeb58f8d Ignore flake8 errors since it has to pass now 2019-03-05 09:41:19 -05:00
Gavin M. Roy
7da331464b Support Tornado 6, remove setuptools_scm 2019-03-05 09:32:00 -05:00
Andrew Rabert
1a300dea34 Rename to match package name 2018-12-04 23:24:06 -05:00
Andrew Rabert
4a52d18f41 Remove deprecated method 2018-12-04 23:08:31 -05:00
Andrew Rabert
c54501a7f4 Remove redundant web.url 2018-12-04 23:08:05 -05:00
Andrew Rabert
5c7cebc6b7 Address flake8 concerns 2018-11-30 13:09:03 -05:00
Andrew Rabert
30c87225c0 Support ietfparse 1.5.1
ietfparse 1.5.1 broke sprockets.mixins.media_type as it introduced the
splitting of content suffix from content subtype. This commit fixes
that.
2018-11-30 11:58:43 -05:00
Andrew Rabert
2fe2480dbf Fix docs 2018-11-28 18:22:39 -05:00
Andrew Rabert
00b87a43cf Use setuptools_scm
This allows us to remove the ImportError hack from __init__.py.
It also simplifies how the package is versioned - deriving it solely
from the repository tag.
Both version_info and __version__ have been removed with this change.
Given how these are a unofficial-standard at best, I've found it safer to
always use pkg_resources to determine the version when I need to.
2018-11-28 13:15:54 -05:00
Andrew Rabert
a28633aef5 Address flake8 complaints 2018-11-28 11:50:43 -05:00
Andrew Rabert
fd3f8b3008 Python 3 updates
- Simplify super()
- Remove object inheritance
- Simplify package namespacing
- Remove BinaryWrapper
- Remove python 2 code paths
2018-11-28 11:50:40 -05:00
Andrew Rabert
b2e980d04d Update docs
- Update and isoldate doc dependencies
- Simplify sphinx config
- Treat warnings as errors
- Remove redundant self.finish() from examples
2018-11-28 11:28:18 -05:00
Andrew Rabert
4f187f80c1 Release version 2.2.2 2018-08-07 15:52:59 -04:00
Dave Shawley
5b74e20542 Metadata updates for 2.2.1. 2018-04-12 07:13:42 -04:00