mirror of
https://github.com/openSUSE/osc.git
synced 2026-01-04 14:32:01 +01:00
Support new 'X | None' union syntax in field types
This commit is contained in:
@@ -39,6 +39,15 @@ if sys.version_info < (3, 8):
|
||||
else:
|
||||
from typing import get_origin
|
||||
|
||||
|
||||
# types.UnionType was added in Python 3.10
|
||||
if sys.version_info < (3, 10):
|
||||
class UnionType:
|
||||
pass
|
||||
else:
|
||||
from types import UnionType
|
||||
|
||||
|
||||
import urllib3.response
|
||||
|
||||
from . import xml
|
||||
@@ -165,7 +174,7 @@ class Field(property):
|
||||
@property
|
||||
def is_optional(self):
|
||||
origin_type = get_origin(self.type) or self.type
|
||||
return origin_type == Union and len(self.type.__args__) == 2 and type(None) in self.type.__args__
|
||||
return origin_type in (Union, UnionType) and type(None) in self.type.__args__
|
||||
|
||||
@property
|
||||
def is_model(self):
|
||||
@@ -193,7 +202,7 @@ class Field(property):
|
||||
origin_type = get_origin(expected_type) or expected_type
|
||||
|
||||
# unwrap Union
|
||||
if origin_type == Union:
|
||||
if origin_type in (Union, UnionType):
|
||||
if value is None and type(None) in expected_type.__args__:
|
||||
valid_type = True
|
||||
continue
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import sys
|
||||
import unittest
|
||||
from typing import Set
|
||||
|
||||
@@ -24,6 +25,16 @@ class TestNotSet(unittest.TestCase):
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
@unittest.skipIf(sys.version_info[:2] < (3, 10), "added in python 3.10")
|
||||
def test_union_or(self):
|
||||
class TestModel(BaseModel):
|
||||
text: str | None = Field()
|
||||
|
||||
m = TestModel()
|
||||
self.assertEqual(m.dict(), {"text": None})
|
||||
|
||||
self.assertRaises(TypeError, setattr, m.text, 123)
|
||||
|
||||
def test_dict(self):
|
||||
class TestSubmodel(BaseModel):
|
||||
text: str = Field(default="default")
|
||||
|
||||
Reference in New Issue
Block a user