Skip to content

Commit 9180c76

Browse files
author
Rémy Lavainne
committed
Add is_union_annotation() to helpers.py
1 parent 63c940b commit 9180c76

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/pyckson/helpers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
from enum import Enum
44

5-
from typing import List, Set, Dict
5+
from typing import List, Set, Dict, Union
66

77
try:
88
from typing import _ForwardRef as ForwardRef
@@ -127,6 +127,12 @@ def is_typing_dict_annotation(annotation):
127127
return False
128128

129129

130+
def is_union_annotation(annotation) -> bool:
131+
if hasattr(annotation, '__origin__'):
132+
return annotation.__origin__ == Union
133+
return False
134+
135+
130136
def using(attr):
131137
def class_decorator(cls):
132138
set_cls_attr(cls, PYCKSON_RULE_ATTR, attr)

tests/test_helpers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from typing import Union
12
from unittest import TestCase
23

3-
from pyckson.helpers import camel_case_name
4+
from pyckson.helpers import camel_case_name, is_union_annotation
45

56

67
class CamelCaseNameTest(TestCase):
@@ -9,3 +10,11 @@ def test_should_do_nothing_on_simple_names(self):
910

1011
def test_should_camel_case_when_there_is_an_underscore(self):
1112
self.assertEqual(camel_case_name('foo_bar'), 'fooBar')
13+
14+
15+
def test_should_detect_union_annotation():
16+
annotation = Union[str, int]
17+
18+
is_union = is_union_annotation(annotation)
19+
20+
assert is_union

0 commit comments

Comments
 (0)