11"""Loading of attrs classes."""
2+ from enum import Enum
23from ipaddress import IPv4Address , IPv6Address , ip_address
34from typing import Union
45from unittest .mock import Mock
@@ -153,6 +154,27 @@ class ClassWithLiteral:
153154 ) == ClassWithLiteral (4 )
154155
155156
157+ @pytest .mark .skipif (is_py37 , reason = "Not supported on 3.7" )
158+ @pytest .mark .parametrize ("converter_cls" , [Converter , GenConverter ])
159+ def test_structure_literal_enum (converter_cls ):
160+ """Structuring a class with a literal field works."""
161+ from typing import Literal
162+
163+ converter = converter_cls ()
164+
165+ class Foo (Enum ):
166+ FOO = 1
167+ BAR = 2
168+
169+ @define
170+ class ClassWithLiteral :
171+ literal_field : Literal [Foo .FOO ] = Foo .FOO
172+
173+ assert converter .structure (
174+ {"literal_field" : 1 }, ClassWithLiteral
175+ ) == ClassWithLiteral (Foo .FOO )
176+
177+
156178@pytest .mark .skipif (is_py37 , reason = "Not supported on 3.7" )
157179@pytest .mark .parametrize ("converter_cls" , [Converter , GenConverter ])
158180def test_structure_literal_multiple (converter_cls ):
@@ -161,9 +183,17 @@ def test_structure_literal_multiple(converter_cls):
161183
162184 converter = converter_cls ()
163185
186+ class Foo (Enum ):
187+ FOO = 7
188+ FOOFOO = 77
189+
190+ class Bar (int , Enum ):
191+ BAR = 8
192+ BARBAR = 88
193+
164194 @define
165195 class ClassWithLiteral :
166- literal_field : Literal [4 , 5 ] = 4
196+ literal_field : Literal [4 , 5 , Foo . FOO , Bar . BARBAR ] = 4
167197
168198 assert converter .structure (
169199 {"literal_field" : 4 }, ClassWithLiteral
@@ -172,6 +202,14 @@ class ClassWithLiteral:
172202 {"literal_field" : 5 }, ClassWithLiteral
173203 ) == ClassWithLiteral (5 )
174204
205+ assert converter .structure (
206+ {"literal_field" : 7 }, ClassWithLiteral
207+ ) == ClassWithLiteral (Foo .FOO )
208+
209+ cwl = converter .structure ({"literal_field" : 88 }, ClassWithLiteral )
210+ assert cwl == ClassWithLiteral (Bar .BARBAR )
211+ assert isinstance (cwl .literal_field , Bar )
212+
175213
176214@pytest .mark .skipif (is_py37 , reason = "Not supported on 3.7" )
177215@pytest .mark .parametrize ("converter_cls" , [Converter , GenConverter ])
0 commit comments