File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -1764,9 +1764,7 @@ def value_to_type(
17641764 elif key_type is type (None ):
17651765 key = {"null" : None }[key ]
17661766
1767- # Can't call isinstance if key_type is a newtype
1768- is_newtype = getattr (key_type , "__supertype__" , None )
1769- if is_newtype or not isinstance (key , key_type ):
1767+ if not isinstance (key_type , type ) or not isinstance (key , key_type ):
17701768 key = value_to_type (key_type , key , custom_converters )
17711769 except Exception as err :
17721770 raise TypeError (
Original file line number Diff line number Diff line change @@ -671,3 +671,29 @@ async def test_json_type_converter():
671671 assert [addr , addr ] == (
672672 await custom_conv .decode ([list_payload ], [List [ipaddress .IPv4Address ]])
673673 )[0 ]
674+
675+
676+ def test_value_to_type_literal_key ():
677+ # The type for the dictionary's *key*:
678+ KeyHint = Literal [
679+ "Key1" ,
680+ "Key2" ,
681+ ]
682+
683+ # The type for the dictionary's *value* (the inner dict):
684+ InnerKeyHint = Literal [
685+ "Inner1" ,
686+ "Inner2" ,
687+ ]
688+ InnerValueHint = str | int | float | None
689+ ValueHint = dict [InnerKeyHint , InnerValueHint ]
690+
691+ # The full type hint for the mapping:
692+ hint_with_bug = dict [KeyHint , ValueHint ]
693+
694+ # A value that uses one of the literal keys:
695+ value_to_convert = {"Key1" : {"Inner1" : 123.45 , "Inner2" : 10 }}
696+ custom_converters : Sequence [JSONTypeConverter ] = []
697+
698+ # Function executes without error
699+ value_to_type (hint_with_bug , value_to_convert , custom_converters )
You can’t perform that action at this time.
0 commit comments