@@ -67,6 +67,7 @@ class Defaults:
6767
6868Document  =  Union [bool , bytes , datetime .datetime , Mapping , None , Sequence , str ]
6969YAMLStyle  =  Literal ["" , "'" , '"' , "|" , ">" ]
70+ YAMLVersion  =  Union [tuple [int , int ], None ]
7071
7172
7273@dataclass (frozen = True ) 
@@ -111,6 +112,7 @@ class YAMLOptions(FormatOptions):
111112    indent : int  =  Defaults .YAML_INDENT 
112113    style : YAMLStyle  =  Defaults .YAML_STYLE 
113114    style_newline : YAMLStyle  |  None  =  None 
115+     version : YAMLVersion  =  (1 , 2 )
114116    width : int  =  Defaults .WIDTH 
115117
116118
@@ -124,6 +126,7 @@ class YAMLOptions(FormatOptions):
124126    "Document" ,
125127    "TooManyValuesError" ,
126128    "YAMLStyle" ,
129+     "YAMLVersion" ,
127130    # Format dataclasses. 
128131    "FormatOptions" ,
129132    "CBOROptions" ,
@@ -142,16 +145,36 @@ class YAMLOptions(FormatOptions):
142145]
143146
144147
145- INPUT_FORMATS  =  ["cbor" , "json" , "msgpack" , "toml" , "yaml" ]
146- OUTPUT_FORMATS  =  ["cbor" , "json" , "msgpack" , "python" , "toml" , "yaml" ]
147- OUTPUT_FORMATS_ARGV0  =  ["cbor" , "json" , "msgpack" , "py" , "toml" , "yaml" ]
148+ INPUT_FORMATS  =  ["cbor" , "json" , "msgpack" , "toml" , "yaml" , "yaml-1.1" , "yaml-1.2" ]
149+ OUTPUT_FORMATS  =  [
150+     "cbor" ,
151+     "json" ,
152+     "msgpack" ,
153+     "python" ,
154+     "toml" ,
155+     "yaml" ,
156+     "yaml-1.1" ,
157+     "yaml-1.2" ,
158+ ]
159+ OUTPUT_FORMATS_ARGV0  =  [
160+     "cbor" ,
161+     "json" ,
162+     "msgpack" ,
163+     "py" ,
164+     "toml" ,
165+     "yaml" ,
166+     "yaml-1.1" ,
167+     "yaml-1.2" ,
168+ ]
148169OPTIONS_CLASSES  =  {
149170    "cbor" : CBOROptions ,
150171    "json" : JSONOptions ,
151172    "msgpack" : MsgPackOptions ,
152173    "python" : PythonOptions ,
153174    "toml" : TOMLOptions ,
154175    "yaml" : YAMLOptions ,
176+     "yaml-1.1" : YAMLOptions ,
177+     "yaml-1.2" : YAMLOptions ,
155178}
156179UTF_8  =  "utf-8" 
157180
@@ -436,6 +459,17 @@ def output_width(value: str) -> int:
436459    return  args 
437460
438461
462+ def  _yaml_version (format : str ) ->  YAMLVersion :
463+     match  format :
464+         case  "yaml-1.1" :
465+             return  (1 , 1 )
466+ 
467+         case  "yaml-1.2" :
468+             return  (1 , 2 )
469+ 
470+     return  None 
471+ 
472+ 
439473# === Parser/serializer wrappers === 
440474
441475
@@ -532,9 +566,11 @@ def _decode_toml(input_data: bytes) -> Document:
532566        raise  ValueError (msg )
533567
534568
535- def  _decode_yaml (input_data : bytes ) ->  Document :
569+ def  _decode_yaml (input_data : bytes ,  version :  YAMLVersion ) ->  Document :
536570    try :
537571        yaml  =  ruamel .yaml .YAML (pure = True , typ = "safe" )
572+         yaml .version  =  version 
573+ 
538574        doc  =  yaml .load (input_data )
539575
540576        return  cast ("Document" , doc )
@@ -545,19 +581,25 @@ def _decode_yaml(input_data: bytes) -> Document:
545581
546582
547583def  decode (input_format : str , input_data : bytes ) ->  Document :
548-     decoder  =  {
549-         "cbor" : _decode_cbor ,
550-         "json" : _decode_json ,
551-         "msgpack" : _decode_msgpack ,
552-         "toml" : _decode_toml ,
553-         "yaml" : _decode_yaml ,
554-     }
555- 
556-     if  input_format  not  in decoder :
557-         msg  =  f"Unknown input format: { input_format }  
558-         raise  ValueError (msg )
584+     match  input_format :
585+         case  "cbor" :
586+             return  _decode_cbor (input_data )
559587
560-     return  decoder [input_format ](input_data )
588+         case  "json" :
589+             return  _decode_json (input_data )
590+ 
591+         case  "msgpack" :
592+             return  _decode_msgpack (input_data )
593+ 
594+         case  "toml" :
595+             return  _decode_toml (input_data )
596+ 
597+         case  "yaml"  |  "yaml-1.1"  |  "yaml-1.2" :
598+             return  _decode_yaml (input_data , version = _yaml_version (input_format ))
599+ 
600+         case  _:
601+             msg  =  f"Unknown input format: { input_format }  
602+             raise  ValueError (msg )
561603
562604
563605class  TooManyValuesError (BaseException ):
@@ -776,13 +818,14 @@ def _encode_yaml(
776818    indent : int  |  None ,
777819    style : YAMLStyle ,
778820    style_newline : YAMLStyle  |  None ,
821+     version : YAMLVersion ,
779822    width : int ,
780823) ->  str :
781824    yaml  =  ruamel .yaml .YAML (pure = True )
782825    yaml .default_flow_style  =  False 
783- 
784826    yaml .default_style  =  style   # type: ignore 
785827    yaml .indent  =  indent 
828+     yaml .version  =  version 
786829    yaml .width  =  width 
787830
788831    def  represent_none (self , data ):
@@ -848,11 +891,12 @@ def format_options(
848891                stringify = stringify ,
849892            )
850893
851-         case  "yaml" :
894+         case  "yaml"   |   "yaml-1.1"   |   "yaml-1.2" :
852895            return  YAMLOptions (
853896                indent = Defaults .YAML_INDENT  if  indent  is  None  else  indent ,
854897                style = yaml_style ,
855898                style_newline = yaml_style_newline ,
899+                 version = _yaml_version (output_format ),
856900                width = width ,
857901            )
858902
@@ -925,7 +969,7 @@ def encode(
925969                stringify = options .stringify ,
926970            ).encode (UTF_8 )
927971
928-         case  "yaml" :
972+         case  "yaml"   |   "yaml-1.1"   |   "yaml-1.2" :
929973            if  not  isinstance (options , YAMLOptions ):
930974                msg  =  "expected 'options' argument to have class 'YAMLOptions'" 
931975                raise  TypeError (msg )
@@ -935,6 +979,7 @@ def encode(
935979                indent = options .indent ,
936980                style = options .style ,
937981                style_newline = options .style_newline ,
982+                 version = options .version ,
938983                width = options .width ,
939984            ).encode (UTF_8 )
940985
0 commit comments