Skip to content

Commit 49b6f33

Browse files
committed
fix: Python 3.9 compatibility
UnionType does not exist in Python 3.9
1 parent 392c972 commit 49b6f33

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

doc/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
[0.2.4] - Unreleased
5+
--------------------
6+
7+
Fixed
8+
^^^^^
9+
- Python 3.9 compatibility.
10+
411
[0.2.3] - 2024-11-01
512
--------------------
613

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ exclude_lines = [
6565
"@pytest.mark.skip",
6666
"pragma: no cover",
6767
"raise NotImplementedError",
68+
"except ImportError",
6869
]
6970

7071
[tool.ruff.lint]

scim2_models/base.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from enum import Enum
33
from enum import auto
44
from inspect import isclass
5-
from types import UnionType
65
from typing import Annotated
76
from typing import Any
87
from typing import Generic
@@ -33,6 +32,14 @@
3332
from scim2_models.utils import normalize_attribute_name
3433
from scim2_models.utils import to_camel
3534

35+
try:
36+
from types import UnionType
37+
38+
UNION_TYPES = [Union, UnionType]
39+
except ImportError:
40+
# Python 3.9 has no UnionType
41+
UNION_TYPES = [Union]
42+
3643
ReferenceTypes = TypeVar("ReferenceTypes")
3744
URIReference = NewType("URIReference", str)
3845
ExternalReference = NewType("ExternalReference", str)
@@ -438,7 +445,7 @@ def get_field_root_type(cls, attribute_name: str) -> type:
438445
attribute_type = cls.model_fields[attribute_name].annotation
439446

440447
# extract 'x' from 'Optional[x]'
441-
if get_origin(attribute_type) in (Union, UnionType):
448+
if get_origin(attribute_type) in UNION_TYPES:
442449
attribute_type = get_args(attribute_type)[0]
443450

444451
# extract 'x' from 'List[x]'

0 commit comments

Comments
 (0)