Skip to content

Commit

Permalink
Merge pull request #2 from Amertz08/test-django3
Browse files Browse the repository at this point in the history
Django3 py>=3.6
  • Loading branch information
Amertz08 authored Feb 13, 2020
2 parents 029aed4 + 53c9e6c commit 8e56740
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ nosetests.xml

#PyCharm
.idea/*
venv
.mypy_cache
30 changes: 23 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,32 @@ matrix:
include:
- python: 3.6
env: TOX_ENV=format
- python: 3.4
env: TOX_ENV=py34
- python: 3.5
env: TOX_ENV=py35
- python: 3.6
env: TOX_ENV=py36
env: TOX_ENV=typing
- python: 3.6
env: TOX_ENV=py36-django2
- python: 3.6
env: TOX_ENV=py36-django21
- python: 3.6
env: TOX_ENV=py36-django22
- python: 3.6
env: TOX_ENV=py36-django3
- python: 3.7
env: TOX_ENV=py37-django2
- python: 3.7
env: TOX_ENV=py37
env: TOX_ENV=py37-django21
- python: 3.7
env: TOX_ENV=py37-django22
- python: 3.7
env: TOX_ENV=py37-django3
- python: 3.8
env: TOX_ENV=py38-django2
- python: 3.8
env: TOX_ENV=py38-django21
- python: 3.8
env: TOX_ENV=py38-django22
- python: 3.8
env: TOX_ENV=py38
env: TOX_ENV=py38-django3

install:
- pip install tox
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
- h3 heading
- X.X.X - YYYY-MM-DD

### 1.5.0 - 2020-02-13

- Dropped support for `python<3.6`
- Type hinting
- `Django3` testing
- Added `drf_ujson.__version__`

### 1.4.1 - 2019-11-05

- Fixed repo url in `setup.py`
Expand Down
1 change: 1 addition & 0 deletions drf_ujson/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__author__ = "y.gavenchuk aka murminathor"
__version__ = (1, 5, 0)
17 changes: 12 additions & 5 deletions drf_ujson/parsers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Any, Mapping, Optional, Type

from django.conf import settings
from rest_framework.exceptions import ParseError
from rest_framework.parsers import BaseParser
from rest_framework.renderers import JSONRenderer
from rest_framework.renderers import JSONRenderer, BaseRenderer
import ujson

__all__ = ["UJSONParser"]
Expand All @@ -12,15 +14,20 @@ class UJSONParser(BaseParser):
Parses JSON-serialized data by ujson parser.
"""

media_type = "application/json"
renderer_class = JSONRenderer
media_type: str = "application/json"
renderer_class: Type[BaseRenderer] = JSONRenderer

# Set to enable usage of higher precision (strtod) function when decoding
# string to double values. Default is to use fast but less precise builtin
# functionality.
precise_float = False
precise_float: bool = False

def parse(self, stream, media_type=None, parser_context=None):
def parse(
self,
stream,
media_type: str = None,
parser_context: Optional[Mapping[str, Any]] = None,
) -> dict:
"""
Parses the incoming bytestream as JSON and returns the resulting data.
"""
Expand Down
16 changes: 12 additions & 4 deletions drf_ujson/renderers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union, Optional, Mapping, Any

from rest_framework.renderers import JSONRenderer
import ujson

Expand All @@ -13,18 +15,24 @@ class UJSONRenderer(JSONRenderer):
"""

# Controls how many decimals to encode for double or decimal values.
double_precision = 9
double_precision: int = 9
# Controls whether forward slashes (/) are escaped.
escape_forward_slashes = False
escape_forward_slashes: bool = False
# Used to enable special encoding of "unsafe" HTML characters into safer
# Unicode sequences.
encode_html_chars = False
encode_html_chars: bool = False

def render(self, data, accepted_media_type=None, renderer_context=None):
def render(
self,
data: Union[dict, None],
accepted_media_type: Optional[str] = None,
renderer_context: Mapping[str, Any] = None,
) -> bytes:

if data is None:
return bytes()

accepted_media_type = accepted_media_type or ""
renderer_context = renderer_context or {}
indent = self.get_indent(accepted_media_type, renderer_context)

Expand Down
7 changes: 7 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[mypy]
ignore_missing_imports = True
plugins = mypy_django_plugin.main, mypy_drf_plugin.main
strict_optional = True

[mypy.plugins.django-stubs]
django_settings_module = tests.settings
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
test = pytest

[tool:pytest]
django_find_project = false
testpaths = tests
python_files = *_tests.py
DJANGO_SETTINGS_MODULE = tests.settings
addopts =
--cov=drf_ujson/
--cov-report=term-missing
26 changes: 21 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,43 @@

from setuptools import setup, find_packages

from drf_ujson import __version__

with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name="drf_ujson2",
version="1.4.1",
python_requires=">=3.4",
version=".".join([str(i) for i in __version__]),
python_requires=">=3.6",
description="Django Rest Framework UJSON Renderer",
keywords="django,djangorestframework,ujson",
long_description=long_description,
long_description_content_type="text/markdown",
author="Gizmag",
author_email="[email protected]",
url="https://github.com/Amertz08/drf-ujson2",
packages=find_packages(exclude=["tests"]),
install_requires=["django", "ujson>=1.35", "djangorestframework"],
extras_require={"dev": ["pytest", "pytest-runner", "pytest-cov", "pytest-mock"]},
extras_require={
"dev": [
"pytest",
"pytest-django",
"pytest-runner",
"pytest-cov",
"pytest-mock",
]
},
classifiers=[
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Framework :: Django :: 2.0",
"Framework :: Django :: 2.1",
"Framework :: Django :: 2.2",
"Framework :: Django :: 3.0",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
],
)
3 changes: 0 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.conf import settings

settings.configure()
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SECRET_KEY = "blah"
21 changes: 16 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
[tox]
envlist =
py34,
py35,
py36,
py37,
py38
py{36,37,38}-django{2,21,22,3}
format
typing

[testenv]
extras = dev
deps =
django2: django~=2.0
django21: django~=2.1
django22: django~=2.2
django3: django~=3.0
commands =
pip list
python setup.py test

[testenv:format]
skipsdist = true
deps =
black
commands =
black . --check

[testenv:typing]
skipsdist = true
deps =
mypy
django-stubs
djangorestframework-stubs
commands = mypy .

0 comments on commit 8e56740

Please sign in to comment.