Skip to content

Commit

Permalink
Resolve #241 Support python >= 3.6
Browse files Browse the repository at this point in the history
Notes:
In py36 the typing module is flattening baseclasses in
unions eg Union[int, bool] -> (int), there is no official
workaround but from all my samples I 've only encountered this case
in a few tests cases in the w3c test suite.
  • Loading branch information
tefra committed Sep 1, 2020
1 parent 6d4c8f2 commit dd294cf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cache: pip

jobs:
include:
- python: 3.6
env: TOXENV=py36
- python: 3.7
env: TOXENV=py37
- python: 3.8
Expand Down
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ classifiers =
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Expand All @@ -37,7 +38,7 @@ install_requires =
lxml
requests
toposort
python_requires = >=3.7
python_requires = >=3.6
include_package_data = True

[options.entry_points]
Expand All @@ -52,6 +53,7 @@ dev =
pytest-benchmark
pytest-cov
tox
dataclasses;python_version<"3.7"
docs =
sphinx
sphinx-autobuild
Expand Down
22 changes: 22 additions & 0 deletions tests/formats/dataclass/test_context.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import sys
from dataclasses import dataclass
from dataclasses import make_dataclass
from dataclasses import replace
from typing import get_type_hints
from typing import Iterator
from typing import List
from typing import Union
from unittest import mock
from unittest import TestCase

import pytest

from tests.fixtures.books import BookForm
from tests.fixtures.books import Books
from tests.fixtures.defxmlschema.chapter03prod import Product
Expand All @@ -19,6 +23,7 @@
from xsdata.formats.dataclass.models.elements import XmlAttribute
from xsdata.formats.dataclass.models.elements import XmlElement
from xsdata.formats.dataclass.models.elements import XmlMeta
from xsdata.formats.dataclass.models.elements import XmlText
from xsdata.formats.dataclass.models.elements import XmlWildcard
from xsdata.utils import text

Expand Down Expand Up @@ -202,6 +207,23 @@ def test_get_type_hints(self):
self.assertFalse(var.dataclass)
self.assertIsNone(var.clazz)

@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python >= 3.7")
def test_get_type_hints_with_union_types(self):
@dataclass
class Example:
bool: bool
int: int
union: Union[int, bool]

result = list(self.ctx.get_type_hints(Example, None))
expected = [
XmlText(name="bool", qname="bool", types=[bool]),
XmlText(name="int", qname="int", types=[int]),
XmlText(name="union", qname="union", types=[bool, int]),
]

self.assertEqual(expected, result)

def test_get_type_hints_with_dataclass_list(self):
result = list(self.ctx.get_type_hints(Books, None))

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py37,py38,py39
envlist = py36,py37,py38,py39
skip_missing_interpreters = true

[testenv]
Expand Down
4 changes: 2 additions & 2 deletions xsdata/formats/dataclass/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ def is_derived(cls, obj: Any, clazz: Type) -> bool:

@staticmethod
def is_element_list(type_hint: Any, is_tokens: bool) -> bool:
if getattr(type_hint, "__origin__", None) is list:
if getattr(type_hint, "__origin__", None) in (list, List):
if not is_tokens:
return True

type_hint = type_hint.__args__[0]
if getattr(type_hint, "__origin__", None) is list:
if getattr(type_hint, "__origin__", None) in (list, List):
return True

return False

0 comments on commit dd294cf

Please sign in to comment.