text
stringlengths
12
786k
let ctor_args iter = function | Tctor_tuple typs -> List . iter ~ f ( : iter . type_expr iter ) typs | Tctor_record fields -> List . iter ~ f ( : iter . field_decl iter ) fields
let ctor_decl iter { ctor_ident ; ctor_args ; ctor_ret ; ctor_loc ; ctor_ctor } = iter . location iter ctor_loc ; ident iter ctor_ident ; iter . ctor_args iter ctor_args ; Option . iter ~ f ( : iter . type_expr iter ) ctor_ret ; ignore ctor_ctor
let type_decl iter { tdec_ident ; tdec_params ; tdec_desc ; tdec_loc ; tdec_tdec } = iter . location iter tdec_loc ; ident iter tdec_ident ; List . iter ~ f ( : iter . type_expr iter ) tdec_params ; iter . type_decl_desc iter tdec_desc ; ignore tdec_tdec
let type_decl_desc iter = function | Tdec_abstract -> ( ) | Tdec_alias typ -> iter . type_expr iter typ | Tdec_record fields -> List . iter ~ f ( : iter . field_decl iter ) fields | Tdec_variant ctors -> List . iter ~ f ( : iter . ctor_decl iter ) ctors | Tdec_open -> ( ) | Tdec_extend ( name , ctors ) -> path iter name ; List . iter ~ f ( : iter . ctor_decl iter ) ctors
let literal ( _iter : iterator ) ( _ : literal ) = ( )
let pattern iter { pat_desc ; pat_loc ; pat_type } = iter . location iter pat_loc ; iter . type0_expr iter pat_type ; iter . pattern_desc iter pat_desc
let pattern_desc iter = function | Tpat_any -> ( ) | Tpat_variable name -> ident iter name | Tpat_constraint ( pat , typ ) -> iter . type_expr iter typ ; iter . pattern iter pat | Tpat_tuple pats -> List . iter ~ f ( : iter . pattern iter ) pats | Tpat_or ( p1 , p2 ) -> iter . pattern iter p1 ; iter . pattern iter p2 | Tpat_literal l -> iter . literal iter l | Tpat_record fields -> List . iter fields ~ f ( : fun ( name , pat ) -> path iter name ; iter . pattern iter pat ) | Tpat_ctor ( name , arg ) -> path iter name ; Option . iter ~ f ( : iter . pattern iter ) arg | Tpat_row_ctor ( name , args ) -> ident iter name ; List . iter ~ f ( : iter . pattern iter ) args
let expression iter { exp_desc ; exp_loc ; exp_type } = iter . location iter exp_loc ; iter . type0_expr iter exp_type ; iter . expression_desc iter exp_desc
let expression_desc iter = function | Texp_apply ( e , args ) -> iter . expression iter e ; List . iter args ~ f ( : fun ( _explicit , _label , e ) -> iter . expression iter e ) | Texp_variable name -> path iter name | Texp_literal l -> iter . literal iter l | Texp_fun ( _label , p , e , _explicit ) -> iter . pattern iter p ; iter . expression iter e | Texp_newtype ( name , e ) -> ident iter name ; iter . expression iter e | Texp_seq ( e1 , e2 ) -> iter . expression iter e1 ; iter . expression iter e2 | Texp_let ( p , e1 , e2 ) -> iter . pattern iter p ; iter . expression iter e1 ; iter . expression iter e2 | Texp_instance ( name , e1 , e2 ) -> ident iter name ; iter . expression iter e1 ; iter . expression iter e2 | Texp_constraint ( e , typ ) -> iter . type_expr iter typ ; iter . expression iter e | Texp_tuple es -> List . iter ~ f ( : iter . expression iter ) es | Texp_match ( e , cases ) -> iter . expression iter e ; List . iter cases ~ f ( : fun ( p , e ) -> iter . pattern iter p ; iter . expression iter e ) | Texp_field ( e , name ) -> path iter name ; iter . expression iter e | Texp_record ( bindings , default ) -> Option . iter ~ f ( : iter . expression iter ) default ; List . iter bindings ~ f ( : fun ( name , e ) -> path iter name ; iter . expression iter e ) | Texp_ctor ( name , arg ) -> path iter name ; Option . iter ~ f ( : iter . expression iter ) arg | Texp_row_ctor ( name , args ) -> ident iter name ; List . iter ~ f ( : iter . expression iter ) args | Texp_unifiable { expression ; name ; id = _ } -> ident iter name ; Option . iter ~ f ( : iter . expression iter ) expression | Texp_if ( e1 , e2 , e3 ) -> iter . expression iter e1 ; iter . expression iter e2 ; Option . iter ~ f ( : iter . expression iter ) e3 | Texp_read ( conv , conv_args , e ) -> iter . convert iter conv ; List . iter conv_args ~ f ( : fun ( _lbl , e ) -> iter . expression iter e ) ; iter . expression iter e | Texp_prover ( conv , conv_args , e ) -> iter . convert iter conv ; List . iter conv_args ~ f ( : fun ( _lbl , e ) -> iter . expression iter e ) ; iter . expression iter e | Texp_convert conv -> iter . convert iter conv
let convert_body iter { conv_body_desc ; conv_body_loc ; conv_body_type } = iter . location iter conv_body_loc ; iter . type0_expr iter conv_body_type ; iter . convert_body_desc iter conv_body_desc
let convert_body_desc iter = function | Tconv_record fields -> List . iter fields ~ f ( : fun ( field , conv ) -> path iter field ; iter . convert_body iter conv ) | Tconv_ctor ( name , args ) -> path iter name ; List . iter args ~ f ( : fun ( _label , conv ) -> iter . convert_body iter conv ) | Tconv_tuple convs -> List . iter ~ f ( : iter . convert_body iter ) convs | Tconv_arrow ( conv1 , conv2 ) -> iter . convert_body iter conv1 ; iter . convert_body iter conv2 | Tconv_identity | Tconv_opaque -> ( )
let convert iter { conv_desc ; conv_loc ; conv_type } = iter . location iter conv_loc ; iter . type0_expr iter conv_type ; iter . convert_desc iter conv_desc
let convert_desc iter = function | Tconv_fun ( name , body ) -> ident iter name ; iter . convert iter body | Tconv_body body -> iter . convert_body iter body
let type_conv iter = function | Ttconv_with ( _mode , decl ) -> iter . type_decl iter decl | Ttconv_to typ -> iter . type_expr iter typ
let signature iter = List . iter ~ f ( : iter . signature_item iter )
let signature_item iter { sig_desc ; sig_loc } = iter . location iter sig_loc ; iter . signature_desc iter sig_desc
let signature_desc iter = function | Tsig_value ( name , typ ) | Tsig_instance ( name , typ ) -> ident iter name ; iter . type_expr iter typ | Tsig_type decl -> iter . type_decl iter decl | Tsig_convtype ( decl , tconv , convname , typ ) -> iter . type_decl iter decl ; type_conv iter tconv ; ident iter convname ; iter . type_expr iter typ | Tsig_rectype decls -> List . iter ~ f ( : iter . type_decl iter ) decls | Tsig_module ( name , msig ) | Tsig_modtype ( name , msig ) -> ident iter name ; iter . module_sig iter msig | Tsig_open name -> path iter name | Tsig_typeext ( typ , ctors ) -> iter . variant iter typ ; List . iter ~ f ( : iter . ctor_decl iter ) ctors | Tsig_request ( typ , ctor ) -> iter . type_expr iter typ ; iter . ctor_decl iter ctor | Tsig_multiple sigs -> iter . signature iter sigs | Tsig_prover sigs -> iter . signature iter sigs | Tsig_convert ( name , typ ) -> ident iter name ; iter . type_expr iter typ
let module_sig iter { msig_desc ; msig_loc } = iter . location iter msig_loc ; iter . module_sig_desc iter msig_desc
let module_sig_desc iter = function | Tmty_sig sigs -> iter . signature iter sigs | Tmty_name name -> path iter name | Tmty_alias name -> path iter name | Tmty_abstract -> ( ) | Tmty_functor ( name , fsig , msig ) -> str iter name ; iter . module_sig iter fsig ; iter . module_sig iter msig
let statements iter = List . iter ~ f ( : iter . statement iter )
let statement iter { stmt_desc ; stmt_loc } = iter . location iter stmt_loc ; iter . statement_desc iter stmt_desc
let statement_desc iter = function | Tstmt_value ( p , e ) -> iter . pattern iter p ; iter . expression iter e | Tstmt_instance ( name , e ) -> ident iter name ; iter . expression iter e | Tstmt_type decl -> iter . type_decl iter decl | Tstmt_convtype ( decl , tconv , convname , conv ) -> iter . type_decl iter decl ; type_conv iter tconv ; ident iter convname ; iter . convert iter conv | Tstmt_rectype decls -> List . iter ~ f ( : iter . type_decl iter ) decls | Tstmt_module ( name , me ) -> ident iter name ; iter . module_expr iter me | Tstmt_modtype ( name , mty ) -> ident iter name ; iter . module_sig iter mty | Tstmt_open name -> path iter name | Tstmt_open_instance name -> path iter name | Tstmt_typeext ( typ , ctors ) -> iter . variant iter typ ; List . iter ~ f ( : iter . ctor_decl iter ) ctors | Tstmt_request ( typ , ctor , handler ) -> iter . type_expr iter typ ; iter . ctor_decl iter ctor ; Option . iter handler ~ f ( : fun ( p , e ) -> Option . iter ~ f ( : iter . pattern iter ) p ; iter . expression iter e ) | Tstmt_multiple stmts -> iter . statements iter stmts | Tstmt_prover stmts -> iter . statements iter stmts | Tstmt_convert ( name , typ , conv ) -> ident iter name ; iter . type_expr iter typ ; iter . convert iter conv
let module_expr iter { mod_desc ; mod_loc } = iter . location iter mod_loc ; iter . module_desc iter mod_desc
let module_desc iter = function | Tmod_struct stmts -> iter . statements iter stmts | Tmod_name name -> path iter name | Tmod_functor ( name , fsig , me ) -> str iter name ; iter . module_sig iter fsig ; iter . module_expr iter me
let location ( _iter : iterator ) ( _ : Location . t ) = ( )
let longident iter = function | Longident . Lident _ -> ( ) | Ldot ( l , _ ) -> iter . longident iter l | Lapply ( l1 , l2 ) -> iter . longident iter l1 ; iter . longident iter l2
let path iter = function | Path . Pident ident -> iter . ident iter ident | Path . Pdot ( path , _ , _ ) | Path . Pocamldot ( path , _ , _ , _ ) -> iter . path iter path | Path . Papply ( path1 , path2 ) -> iter . path iter path1 ; iter . path iter path2
let ident ( _iter : iterator ) ( _ : Ident . t ) = ( )
let type0_decl ( _iter : iterator ) ( _ : Type0 . type_decl ) = ( )
let type0_expr ( _iter : iterator ) ( _ : Type0 . type_expr ) = ( )
let default_iterator = { type_expr ; type_desc ; variant ; row_tag ; field_decl ; ctor_args ; ctor_decl ; type_decl ; type_decl_desc ; literal ; pattern ; pattern_desc ; expression ; expression_desc ; convert_body ; convert_body_desc ; convert ; convert_desc ; signature_item ; signature ; signature_desc ; module_sig ; module_sig_desc ; statement ; statements ; statement_desc ; module_expr ; module_desc ; location ; longident ; ident ; path ; type0_decl ; type0_expr }
type mapper = { type_expr : mapper -> type_expr -> type_expr ; type_desc : mapper -> type_desc -> type_desc ; variant : mapper -> variant -> variant ; row_tag : mapper -> row_tag -> row_tag ; field_decl : mapper -> field_decl -> field_decl ; ctor_args : mapper -> ctor_args -> ctor_args ; ctor_decl : mapper -> ctor_decl -> ctor_decl ; type_decl : mapper -> type_decl -> type_decl ; type_decl_desc : mapper -> type_decl_desc -> type_decl_desc ; literal : mapper -> literal -> literal ; pattern : mapper -> pattern -> pattern ; pattern_desc : mapper -> pattern_desc -> pattern_desc ; expression : mapper -> expression -> expression ; expression_desc : mapper -> expression_desc -> expression_desc ; convert : mapper -> convert -> convert ; convert_desc : mapper -> convert_desc -> convert_desc ; convert_body : mapper -> convert_body -> convert_body ; convert_body_desc : mapper -> convert_body_desc -> convert_body_desc ; signature_item : mapper -> signature_item -> signature_item ; signature : mapper -> signature -> signature ; signature_desc : mapper -> signature_desc -> signature_desc ; module_sig : mapper -> module_sig -> module_sig ; module_sig_desc : mapper -> module_sig_desc -> module_sig_desc ; statement : mapper -> statement -> statement ; statements : mapper -> statements -> statements ; statement_desc : mapper -> statement_desc -> statement_desc ; module_expr : mapper -> module_expr -> module_expr ; module_desc : mapper -> module_desc -> module_desc ; location : mapper -> Location . t -> Location . t ; longident : mapper -> Longident . t -> Longident . t ; ident : mapper -> Ident . t -> Ident . t ; path : mapper -> Path . t -> Path . t ; type0 : Type0_map . mapper }
let with_backtrack_replace f = let snap = Type1 . Snapshot . create ( ) in let ret = f ( ) in Type1 . backtrack_replace snap ; ret
let lid mapper { Location . txt ; loc } = { Location . txt = mapper . longident mapper txt ; loc = mapper . location mapper loc }
let str mapper ( { Location . txt ; loc } : str ) = { Location . txt ; loc = mapper . location mapper loc }
let ident mapper ( { Location . txt ; loc } : Ident . t Location . loc ) = { Location . txt = mapper . ident mapper txt ; loc = mapper . location mapper loc }
let path mapper ( { Location . txt ; loc } : Path . t Location . loc ) = { Location . txt = mapper . path mapper txt ; loc = mapper . location mapper loc }
let type_expr mapper { type_desc ; type_loc ; type_type } = let type_loc = mapper . location mapper type_loc in let type_desc = mapper . type_desc mapper type_desc in let type_type = with_backtrack_replace ( fun ( ) -> mapper . type0 . type_expr mapper . type0 type_type ) in { type_desc ; type_loc ; type_type }
let type_desc mapper typ = match typ with | Ttyp_var name -> Ttyp_var ( Option . map ~ f ( : str mapper ) name ) | Ttyp_tuple typs -> Ttyp_tuple ( List . map ~ f ( : mapper . type_expr mapper ) typs ) | Ttyp_arrow ( typ1 , typ2 , explicit , label ) -> Ttyp_arrow ( mapper . type_expr mapper typ1 , mapper . type_expr mapper typ2 , explicit , label ) | Ttyp_ctor variant -> Ttyp_ctor ( mapper . variant mapper variant ) | Ttyp_poly ( vars , typ ) -> Ttyp_poly ( List . map ~ f ( : mapper . type_expr mapper ) vars , mapper . type_expr mapper typ ) | Ttyp_prover typ -> Ttyp_prover ( mapper . type_expr mapper typ ) | Ttyp_conv ( typ1 , typ2 ) -> Ttyp_conv ( mapper . type_expr mapper typ1 , mapper . type_expr mapper typ2 ) | Ttyp_opaque typ -> Ttyp_opaque ( mapper . type_expr mapper typ ) | Ttyp_alias ( typ , name ) -> Ttyp_alias ( mapper . type_expr mapper typ , str mapper name ) | Ttyp_row ( tags , closed , min_tags ) -> Ttyp_row ( List . map ~ f ( : mapper . row_tag mapper ) tags , closed , Option . map ~ f ( : List . map ~ f ( : ident mapper ) ) min_tags ) | Ttyp_row_subtract ( typ , tags ) -> Ttyp_row_subtract ( mapper . type_expr mapper typ , List . map ~ f ( : ident mapper ) tags )
let variant mapper { var_ident ; var_params } = { var_ident = path mapper var_ident ; var_params = List . map ~ f ( : mapper . type_expr mapper ) var_params }
let row_tag mapper { rtag_ident ; rtag_arg ; rtag_loc } = { rtag_ident = ident mapper rtag_ident ; rtag_arg = List . map ~ f ( : mapper . type_expr mapper ) rtag_arg ; rtag_loc = mapper . location mapper rtag_loc }
let field_decl mapper { fld_ident ; fld_type ; fld_loc ; fld_fld } = { fld_loc = mapper . location mapper fld_loc ; fld_ident = ident mapper fld_ident ; fld_type = mapper . type_expr mapper fld_type ; fld_fld = with_backtrack_replace ( fun ( ) -> mapper . type0 . field_decl mapper . type0 fld_fld ) }
let ctor_args mapper = function | Tctor_tuple typs -> Tctor_tuple ( List . map ~ f ( : mapper . type_expr mapper ) typs ) | Tctor_record fields -> Tctor_record ( List . map ~ f ( : mapper . field_decl mapper ) fields )
let ctor_decl mapper { ctor_ident ; ctor_args ; ctor_ret ; ctor_loc ; ctor_ctor } = { ctor_loc = mapper . location mapper ctor_loc ; ctor_ident = ident mapper ctor_ident ; ctor_args = mapper . ctor_args mapper ctor_args ; ctor_ret = Option . map ~ f ( : mapper . type_expr mapper ) ctor_ret ; ctor_ctor = with_backtrack_replace ( fun ( ) -> mapper . type0 . ctor_decl mapper . type0 ctor_ctor ) }
let type_decl mapper { tdec_ident ; tdec_params ; tdec_desc ; tdec_loc ; tdec_tdec } = { tdec_loc = mapper . location mapper tdec_loc ; tdec_ident = ident mapper tdec_ident ; tdec_params = List . map ~ f ( : mapper . type_expr mapper ) tdec_params ; tdec_desc = mapper . type_decl_desc mapper tdec_desc ; tdec_tdec = with_backtrack_replace ( fun ( ) -> mapper . type0 . type_decl mapper . type0 tdec_tdec ) }
let type_decl_desc mapper = function | Tdec_abstract -> Tdec_abstract | Tdec_alias typ -> Tdec_alias ( mapper . type_expr mapper typ ) | Tdec_record fields -> Tdec_record ( List . map ~ f ( : mapper . field_decl mapper ) fields ) | Tdec_variant ctors -> Tdec_variant ( List . map ~ f ( : mapper . ctor_decl mapper ) ctors ) | Tdec_open -> Tdec_open | Tdec_extend ( name , ctors ) -> Tdec_extend ( path mapper name , List . map ~ f ( : mapper . ctor_decl mapper ) ctors )
let literal ( _iter : mapper ) ( l : literal ) = l
let pattern mapper { pat_desc ; pat_loc ; pat_type } = { pat_loc = mapper . location mapper pat_loc ; pat_desc = mapper . pattern_desc mapper pat_desc ; pat_type = with_backtrack_replace ( fun ( ) -> mapper . type0 . type_expr mapper . type0 pat_type ) }
let pattern_desc mapper = function | Tpat_any -> Tpat_any | Tpat_variable name -> Tpat_variable ( ident mapper name ) | Tpat_constraint ( pat , typ ) -> Tpat_constraint ( mapper . pattern mapper pat , mapper . type_expr mapper typ ) | Tpat_tuple pats -> Tpat_tuple ( List . map ~ f ( : mapper . pattern mapper ) pats ) | Tpat_or ( p1 , p2 ) -> Tpat_or ( mapper . pattern mapper p1 , mapper . pattern mapper p2 ) | Tpat_literal l -> Tpat_literal ( mapper . literal mapper l ) | Tpat_record fields -> Tpat_record ( List . map fields ~ f ( : fun ( name , pat ) -> ( path mapper name , mapper . pattern mapper pat ) ) ) | Tpat_ctor ( name , arg ) -> Tpat_ctor ( path mapper name , Option . map ~ f ( : mapper . pattern mapper ) arg ) | Tpat_row_ctor ( name , args ) -> Tpat_row_ctor ( ident mapper name , List . map ~ f ( : mapper . pattern mapper ) args )
let convert_body mapper { conv_body_desc ; conv_body_loc ; conv_body_type } = { conv_body_loc = mapper . location mapper conv_body_loc ; conv_body_desc = mapper . convert_body_desc mapper conv_body_desc ; conv_body_type = mapper . type0 . type_expr mapper . type0 conv_body_type }
let convert_body_desc mapper = function | Tconv_record fields -> Tconv_record ( List . map fields ~ f ( : fun ( name , conv ) -> ( path mapper name , mapper . convert_body mapper conv ) ) ) | Tconv_ctor ( name , args ) -> Tconv_ctor ( path mapper name , List . map args ~ f ( : fun ( label , conv ) -> ( label , mapper . convert_body mapper conv ) ) ) | Tconv_tuple convs -> Tconv_tuple ( List . map ~ f ( : mapper . convert_body mapper ) convs ) | Tconv_arrow ( conv1 , conv2 ) -> Tconv_arrow ( mapper . convert_body mapper conv1 , mapper . convert_body mapper conv2 ) | Tconv_identity -> Tconv_identity | Tconv_opaque -> Tconv_opaque
let convert mapper { conv_desc ; conv_loc ; conv_type } = { conv_loc = mapper . location mapper conv_loc ; conv_desc = mapper . convert_desc mapper conv_desc ; conv_type = mapper . type0 . type_expr mapper . type0 conv_type }
let convert_desc mapper = function | Tconv_fun ( name , conv ) -> Tconv_fun ( ident mapper name , mapper . convert mapper conv ) | Tconv_body conv -> Tconv_body ( mapper . convert_body mapper conv )
let expression mapper { exp_desc ; exp_loc ; exp_type } = { exp_loc = mapper . location mapper exp_loc ; exp_desc = mapper . expression_desc mapper exp_desc ; exp_type = with_backtrack_replace ( fun ( ) -> mapper . type0 . type_expr mapper . type0 exp_type ) }
let expression_desc mapper = function | Texp_apply ( e , args ) -> Texp_apply ( mapper . expression mapper e , List . map args ~ f ( : fun ( explicit , label , e ) -> ( explicit , label , mapper . expression mapper e ) ) ) | Texp_variable name -> Texp_variable ( path mapper name ) | Texp_literal l -> Texp_literal ( mapper . literal mapper l ) | Texp_fun ( label , p , e , explicit ) -> Texp_fun ( label , mapper . pattern mapper p , mapper . expression mapper e , explicit ) | Texp_newtype ( name , e ) -> Texp_newtype ( ident mapper name , mapper . expression mapper e ) | Texp_seq ( e1 , e2 ) -> Texp_seq ( mapper . expression mapper e1 , mapper . expression mapper e2 ) | Texp_let ( p , e1 , e2 ) -> Texp_let ( mapper . pattern mapper p , mapper . expression mapper e1 , mapper . expression mapper e2 ) | Texp_instance ( name , e1 , e2 ) -> Texp_instance ( ident mapper name , mapper . expression mapper e1 , mapper . expression mapper e2 ) | Texp_constraint ( e , typ ) -> Texp_constraint ( mapper . expression mapper e , mapper . type_expr mapper typ ) | Texp_tuple es -> Texp_tuple ( List . map ~ f ( : mapper . expression mapper ) es ) | Texp_match ( e , cases ) -> Texp_match ( mapper . expression mapper e , List . map cases ~ f ( : fun ( p , e ) -> ( mapper . pattern mapper p , mapper . expression mapper e ) ) ) | Texp_field ( e , name ) -> Texp_field ( mapper . expression mapper e , path mapper name ) | Texp_record ( bindings , default ) -> Texp_record ( List . map bindings ~ f ( : fun ( name , e ) -> ( path mapper name , mapper . expression mapper e ) ) , Option . map ~ f ( : mapper . expression mapper ) default ) | Texp_ctor ( name , arg ) -> Texp_ctor ( path mapper name , Option . map ~ f ( : mapper . expression mapper ) arg ) | Texp_row_ctor ( name , args ) -> Texp_row_ctor ( ident mapper name , List . map ~ f ( : mapper . expression mapper ) args ) | Texp_unifiable { expression ; name ; id } -> Texp_unifiable { id ; name = ident mapper name ; expression = Option . map ~ f ( : mapper . expression mapper ) expression } | Texp_if ( e1 , e2 , e3 ) -> Texp_if ( mapper . expression mapper e1 , mapper . expression mapper e2 , Option . map ~ f ( : mapper . expression mapper ) e3 ) | Texp_read ( conv , conv_args , e ) -> Texp_read ( mapper . convert mapper conv , List . map conv_args ~ f ( : fun ( label , e ) -> ( label , mapper . expression mapper e ) ) , mapper . expression mapper e ) | Texp_prover ( conv , conv_args , e ) -> Texp_prover ( mapper . convert mapper conv , List . map conv_args ~ f ( : fun ( label , e ) -> ( label , mapper . expression mapper e ) ) , mapper . expression mapper e ) | Texp_convert conv -> Texp_convert ( mapper . convert mapper conv )
let type_conv mapper = function | Ttconv_with ( mode , decl ) -> Ttconv_with ( mode , mapper . type_decl mapper decl ) | Ttconv_to typ -> Ttconv_to ( mapper . type_expr mapper typ )
let signature mapper = List . map ~ f ( : mapper . signature_item mapper )
let signature_item mapper { sig_desc ; sig_loc } = { sig_loc = mapper . location mapper sig_loc ; sig_desc = mapper . signature_desc mapper sig_desc }
let signature_desc mapper = function | Tsig_value ( name , typ ) -> Tsig_value ( ident mapper name , mapper . type_expr mapper typ ) | Tsig_instance ( name , typ ) -> Tsig_instance ( ident mapper name , mapper . type_expr mapper typ ) | Tsig_type decl -> Tsig_type ( mapper . type_decl mapper decl ) | Tsig_convtype ( decl , tconv , convname , typ ) -> Tsig_convtype ( mapper . type_decl mapper decl , type_conv mapper tconv , ident mapper convname , mapper . type_expr mapper typ ) | Tsig_rectype decls -> Tsig_rectype ( List . map ~ f ( : mapper . type_decl mapper ) decls ) | Tsig_module ( name , msig ) -> Tsig_module ( ident mapper name , mapper . module_sig mapper msig ) | Tsig_modtype ( name , msig ) -> Tsig_modtype ( ident mapper name , mapper . module_sig mapper msig ) | Tsig_open name -> Tsig_open ( path mapper name ) | Tsig_typeext ( typ , ctors ) -> Tsig_typeext ( mapper . variant mapper typ , List . map ~ f ( : mapper . ctor_decl mapper ) ctors ) | Tsig_request ( typ , ctor ) -> Tsig_request ( mapper . type_expr mapper typ , mapper . ctor_decl mapper ctor ) | Tsig_multiple sigs -> Tsig_multiple ( mapper . signature mapper sigs ) | Tsig_prover sigs -> Tsig_prover ( mapper . signature mapper sigs ) | Tsig_convert ( name , typ ) -> Tsig_convert ( ident mapper name , mapper . type_expr mapper typ )
let module_sig mapper { msig_desc ; msig_loc } = { msig_loc = mapper . location mapper msig_loc ; msig_desc = mapper . module_sig_desc mapper msig_desc }
let module_sig_desc mapper = function | Tmty_sig sigs -> Tmty_sig ( mapper . signature mapper sigs ) | Tmty_name name -> Tmty_name ( path mapper name ) | Tmty_alias name -> Tmty_alias ( path mapper name ) | Tmty_abstract -> Tmty_abstract | Tmty_functor ( name , fsig , msig ) -> Tmty_functor ( str mapper name , mapper . module_sig mapper fsig , mapper . module_sig mapper msig )
let statements mapper = List . map ~ f ( : mapper . statement mapper )
let statement mapper { stmt_desc ; stmt_loc } = { stmt_loc = mapper . location mapper stmt_loc ; stmt_desc = mapper . statement_desc mapper stmt_desc }
let statement_desc mapper = function | Tstmt_value ( p , e ) -> Tstmt_value ( mapper . pattern mapper p , mapper . expression mapper e ) | Tstmt_instance ( name , e ) -> Tstmt_instance ( ident mapper name , mapper . expression mapper e ) | Tstmt_type decl -> Tstmt_type ( mapper . type_decl mapper decl ) | Tstmt_convtype ( decl , tconv , convname , conv ) -> Tstmt_convtype ( mapper . type_decl mapper decl , type_conv mapper tconv , ident mapper convname , mapper . convert mapper conv ) | Tstmt_rectype decls -> Tstmt_rectype ( List . map ~ f ( : mapper . type_decl mapper ) decls ) | Tstmt_module ( name , me ) -> Tstmt_module ( ident mapper name , mapper . module_expr mapper me ) | Tstmt_modtype ( name , mty ) -> Tstmt_modtype ( ident mapper name , mapper . module_sig mapper mty ) | Tstmt_open name -> Tstmt_open ( path mapper name ) | Tstmt_open_instance name -> Tstmt_open_instance ( path mapper name ) | Tstmt_typeext ( typ , ctors ) -> Tstmt_typeext ( mapper . variant mapper typ , List . map ~ f ( : mapper . ctor_decl mapper ) ctors ) | Tstmt_request ( typ , ctor , handler ) -> Tstmt_request ( mapper . type_expr mapper typ , mapper . ctor_decl mapper ctor , Option . map handler ~ f ( : fun ( p , e ) -> ( Option . map ~ f ( : mapper . pattern mapper ) p , mapper . expression mapper e ) ) ) | Tstmt_multiple stmts -> Tstmt_multiple ( mapper . statements mapper stmts ) | Tstmt_prover stmts -> Tstmt_prover ( mapper . statements mapper stmts ) | Tstmt_convert ( name , typ , conv ) -> Tstmt_convert ( ident mapper name , mapper . type_expr mapper typ , mapper . convert mapper conv )
let module_expr mapper { mod_desc ; mod_loc } = { mod_loc = mapper . location mapper mod_loc ; mod_desc = mapper . module_desc mapper mod_desc }
let module_desc mapper = function | Tmod_struct stmts -> Tmod_struct ( mapper . statements mapper stmts ) | Tmod_name name -> Tmod_name ( path mapper name ) | Tmod_functor ( name , fsig , me ) -> Tmod_functor ( str mapper name , mapper . module_sig mapper fsig , mapper . module_expr mapper me )
let location ( _mapper : mapper ) ( loc : Location . t ) = loc
let longident mapper = function | Longident . Lident str -> Longident . Lident str | Ldot ( l , str ) -> Ldot ( mapper . longident mapper l , str ) | Lapply ( l1 , l2 ) -> Lapply ( mapper . longident mapper l1 , mapper . longident mapper l2 )
let path mapper = function | Path . Pident ident -> Path . Pident ( mapper . ident mapper ident ) | Path . Pdot ( path , mode , str ) -> Path . Pdot ( mapper . path mapper path , mode , str ) | Path . Pocamldot ( path , mode , str , ocaml_name ) -> Path . Pocamldot ( mapper . path mapper path , mode , str , ocaml_name ) | Path . Papply ( path1 , path2 ) -> Path . Papply ( mapper . path mapper path1 , mapper . path mapper path2 )
let ident ( _mapper : mapper ) ( ident : Ident . t ) = ident
let default_iterator = { type_expr ; type_desc ; variant ; row_tag ; field_decl ; ctor_args ; ctor_decl ; type_decl ; type_decl_desc ; literal ; pattern ; pattern_desc ; expression ; expression_desc ; convert_body ; convert_body_desc ; convert ; convert_desc ; signature_item ; signature ; signature_desc ; module_sig ; module_sig_desc ; statement ; statements ; statement_desc ; module_expr ; module_desc ; location ; longident ; ident ; path ; type0 = Type0_map . default_mapper }
let test_check_typed_dictionaries context = let assert_test_typed_dictionary source = let mypy_extensions_stub = { handle = " mypy_extensions . pyi " ; source = { | import typing def TypedDict ( typename : str , fields : typing . Dict [ str , typing . Type [ _T ] ] , total : bool = . . . , ) -> typing . Type [ dict ] : . . . } ; | } in let typed_dictionary_for_import = { handle = " foo / bar / baz . py " ; source = { | from mypy_extensions import TypedDict class ClassBasedTypedDictGreekLetters ( TypedDict ) : alpha : int beta : str gamma : bool class ClassBasedNonTotalTypedDictGreekLetters ( TypedDict , total = False ) : alpha : int beta : str gamma : bool def decorator ( cls : C ) -> C : return cls @ decorator class DecoratedClassBasedTypedDictGreekLetters ( TypedDict ) : alpha : int beta : str gamma : bool } ; | } in assert_type_errors ~ context ~ update_environment_with [ : mypy_extensions_stub ; typed_dictionary_for_import ] source in assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : int ) -> str : return " " def f ( ) -> None : movie : Movie a = foo ( movie [ ' year ' ] ) } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Empty = mypy_extensions . TypedDict ( ' Empty ' , { } ) d : Empty reveal_type ( d ) } | [ " Revealed type [ - 1 ] : Revealed type for ` d ` is ` Empty ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : int ) -> str : return " " def f ( ) -> None : movie : Movie a = foo ( movie [ ' name ' ] ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` int ` but got ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : int ) -> str : return " " def f ( ) -> None : movie : Movie a = foo ( movie [ ' yar ' ] ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` int ` but got ` str ` . " ; " TypedDict accessed with a missing key [ 27 ] : TypedDict ` Movie ` has no key ` yar ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : int ) -> str : return " " def f ( ) -> None : movie : Movie key = " year " a = foo ( movie [ key ] ) } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : int ) -> str : return " " def f ( key : str ) -> None : movie : Movie a = foo ( movie [ key ] ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` int ` but got ` str ` . " ; " TypedDict accessed with a non - literal [ 26 ] : TypedDict key must be a string literal . " ^ " Expected one of ( ' name ' , ' year ' ) . " ; ] ; assert_test_typed_dictionary { | import typing_extensions Movie = typing_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : int ) -> str : return " " def f ( key : str ) -> None : movie : Movie a = foo ( movie [ key ] ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` int ` but got ` str ` . " ; " TypedDict accessed with a non - literal [ 26 ] : TypedDict key must be a string literal . " ^ " Expected one of ( ' name ' , ' year ' ) . " ; ] ; assert_test_typed_dictionary { | import typing Movie = typing . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : int ) -> str : return " " def f ( key : str ) -> None : movie : Movie a = foo ( movie [ key ] ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` int ` but got ` str ` . " ; " TypedDict accessed with a non - literal [ 26 ] : TypedDict key must be a string literal . " ^ " Expected one of ( ' name ' , ' year ' ) . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) Film = mypy_extensions . TypedDict ( ' Film ' , { ' name ' : str , ' year ' : ' int ' , ' director ' : str } ) def foo ( movie : Movie ) -> str : return movie [ " name " ] def f ( ) -> None : movie : Film a = foo ( movie ) } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) Actor = mypy_extensions . TypedDict ( ' Actor ' , { ' name ' : str , ' birthyear ' : ' int ' } ) def foo ( movie : Movie ) -> str : return movie [ " name " ] def f ( ) -> None : actor : Actor a = foo ( actor ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` Movie ` but got ` Actor ` . " ; ] ; assert_test_typed_dictionary { | from mypy_extensions import TypedDict Movie = TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) Cat = TypedDict ( ' Cat ' , { ' name ' : str , ' breed ' : str } ) Named = TypedDict ( ' Named ' , { ' name ' : str } ) def foo ( x : int , a : Movie , b : Cat ) -> Named : if x == 7 : q = a else : q = b return q } | [ ] ; assert_test_typed_dictionary { | from mypy_extensions import TypedDict Movie = TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) Cat = TypedDict ( ' Cat ' , { ' name ' : str , ' breed ' : str } ) def foo ( x : int , a : Movie , b : Cat ) -> int : if x == 7 : q = a else : q = b return q [ " year " ] } | [ " Incompatible return type [ 7 ] : Expected ` int ` but got ` str ` . " ; " TypedDict accessed with a missing key [ 27 ] : TypedDict ` Cat ` has no key ` year ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions from typing import Mapping , Any Baz = mypy_extensions . TypedDict ( ' Baz ' , { ' foo ' : int , ' bar ' : str } ) def foo ( dictionary : Mapping [ str , Any ] ) -> None : pass def f ( ) -> None : baz : Baz a = foo ( baz ) } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions from typing import Mapping class A : pass class B ( A ) : pass Baz = mypy_extensions . TypedDict ( ' Baz ' , { ' foo ' : A , ' bar ' : B } ) def foo ( dictionary : Mapping [ str , A ] ) -> A : return dictionary [ " foo " ] def f ( ) -> None : baz : Baz a = foo ( baz ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` Mapping [ str , A ] ` but got ` Baz ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions from typing import Mapping class A : pass class B ( A ) : pass class C ( A ) : pass Baz = mypy_extensions . TypedDict ( ' Baz ' , { ' foo ' : A , ' bar ' : B } ) def foo ( x : int , a : Baz , b : Mapping [ str , C ] ) -> Mapping [ str , A ] : if x == 7 : q = a else : q = b return q } | [ " Incompatible return type [ 7 ] : Expected ` Mapping [ str , A ] ` but got ` Mapping [ str , object ] ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Baz = mypy_extensions . TypedDict ( ' Baz ' , { ' foo ' : int , ' bar ' : int } ) def foo ( x : int , a : Baz ) -> int : if x == 7 : q = a [ " fou " ] else : q = a [ " bar " ] return q } | [ " TypedDict accessed with a missing key [ 27 ] : TypedDict ` Baz ` has no key ` fou ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Baz = mypy_extensions . TypedDict ( ' Baz ' , { ' foo ' : int , ' bar ' : int } ) def foo ( x : int , a : Baz ) -> int : if x == 7 : k = " foo " q = a [ k ] else : q = a [ " bar " ] return q } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Baz = mypy_extensions . TypedDict ( ' Baz ' , { ' first_very_long_field ' : int , ' second_very_long_field ' : int , ' third_very_long_field ' : int , ' fourth_very_long_field ' : int , ' fifth_very_long_field ' : int } ) def foo ( x : int , a : Baz ) -> int : if x == 7 : k = " foo " q = a [ k ] else : q = a [ " first_very_long_field " ] return q } | [ " TypedDict accessed with a missing key [ 27 ] : TypedDict ` Baz ` has no key ` foo ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Baz = mypy_extensions . TypedDict ( ' Baz ' , { ' first_very_long_field ' : int , ' second_very_long_field ' : int , ' third_very_long_field ' : int , ' fourth_very_long_field ' : int , ' fifth_very_long_field ' : int } ) def foo ( a : Baz ) -> int : . . . def bar ( ** kwargs : int ) -> None : foo ( kwargs ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` Baz ` but got ` Dict [ str , int ] ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) def foo ( ) -> int : reveal_type ( Movie . __init__ ) movie = Movie ( name ' = Blade Runner ' , year = 1982 ) return movie [ ' year ' ] } | [ " Revealed type [ - 1 ] : Revealed type for ` Movie . __init__ ` is ` typing . Callable ( __init__ ) [ . . . , \ unknown ] [ [ [ Named ( self , unknown ) , KeywordOnly ( name , str ) , KeywordOnly ( year , int ) ] , \ Movie ] [ [ Named ( self , unknown ) , Movie ] , Movie ] ] ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) def foo ( ) -> int : movie = Movie ( year = 1982 , name ' = Blade Runner ' ) return movie [ ' year ' ] } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) def foo ( ) -> int : movie = Movie ( name = 1982 , year ' = Blade Runner ' ) return movie [ ' year ' ] } | [ " Incompatible parameter type [ 6 ] : In call ` __init__ ` , for 1st parameter ` name ` expected \ ` str ` but got ` int ` . " ; " Incompatible parameter type [ 6 ] : In call ` __init__ ` , for 2nd parameter ` year ` expected \ ` int ` but got ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) def foo ( ) -> int : movie = Movie ( ' Blade Runner ' , 1982 ) return movie [ ' year ' ] } | [ " Too many arguments [ 19 ] : Call ` __init__ ` expects 0 positional arguments , 2 were provided . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) def foo ( ) -> int : movie = Movie ( ' Blade Runner ' ) return movie [ ' year ' ] } | [ " Incompatible parameter type [ 6 ] : In call ` __init__ ` , for 1st positional only parameter \ expected ` Movie ` but got ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) def foo ( ) -> int : movie = Movie ( { " name " : " Blade Runner " , " year " : 1982 } ) return movie [ ' year ' ] } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) def foo ( ) -> int : movie = Movie ( { " name " : 1982 , " year " : " Blade Runner " } ) return movie [ ' year ' ] } | [ " TypedDict initialization error [ 55 ] : Expected type ` str ` for ` Movie ` field ` name ` but got \ ` int ` . " ; " TypedDict initialization error [ 55 ] : Expected type ` int ` for ` Movie ` field ` year ` but got \ ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) def foo ( ) -> int : movie = Movie ( name ' = Blade Runner ' , year = 1982 , extra = 42 ) return movie [ ' year ' ] } | [ " Unexpected keyword [ 28 ] : Unexpected keyword argument ` extra ` to call ` __init__ ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } ) def foo ( ) -> int : movie = Movie ( year = 1982 ) return movie [ ' year ' ] } | [ " Missing argument [ 20 ] : Call ` __init__ ` expects argument ` name ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : int } , total = False ) def foo ( ) -> int : movie = Movie ( year = 1982 ) return movie [ ' year ' ] } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie movie [ ' name ' ] = ' new name ' } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie movie [ ' name ' ] = 7 } | [ " Invalid TypedDict operation [ 54 ] : Expected ` str ` to be assigned to ` Movie ` field ` name ` but \ got ` int ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie movie [ ' nme ' ] = ' new name ' } | [ " TypedDict accessed with a missing key [ 27 ] : TypedDict ` Movie ` has no key ` nme ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions class A ( ) : pass class B ( A ) : pass Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' something ' : A } ) def f ( ) -> None : movie : Movie movie [ ' something ' ] = B ( ) } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions class A ( ) : pass class B ( A ) : pass Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' something ' : B } ) def f ( ) -> None : movie : Movie movie [ ' something ' ] = A ( ) } | [ " Invalid TypedDict operation [ 54 ] : Expected ` B ` to be assigned to ` Movie ` field ` something ` \ but got ` A ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie movie [ ' year ' ] += 7 } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie reveal_type ( len ( movie ) ) } | [ " Revealed type [ - 1 ] : Revealed type for ` len ( movie ) ` is ` int ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie for k in movie : reveal_type ( k ) } | [ " Revealed type [ - 1 ] : Revealed type for ` k ` is ` str ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie b = " key " in movie reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` b ` is ` bool ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie v = movie [ ' name ' ] reveal_type ( v ) v = movie . get ( ' name ' ) reveal_type ( v ) v = movie . get ( ' name ' , True ) reveal_type ( v ) v = movie . get ( ' nae ' , True ) } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` str ` . " ; " Revealed type [ - 1 ] : Revealed type for ` v ` is ` typing . Optional [ str ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` v ` is " ^ " ` typing . Union [ typing_extensions . Literal [ True ] , str ] ` . " ; " TypedDict accessed with a missing key [ 27 ] : TypedDict ` Movie ` has no key ` nae ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie v = movie . keys ( ) reveal_type ( v ) v = movie . values ( ) reveal_type ( v ) v = movie . items ( ) reveal_type ( v ) } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` typing . AbstractSet [ str ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` v ` is ` typing . ValuesView [ object ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` v ` is " ^ " ` typing . AbstractSet [ typing . Tuple [ str , object ] ] ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie v = movie . copy ( ) reveal_type ( v ) } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is " ^ " ` Movie ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie v = movie . setdefault ( ' name ' , ' newname ' ) reveal_type ( v ) v = movie . setdefault ( ' name ' , 7 ) v = movie . setdefault ( ' nme ' , ' newname ' ) } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` str ` . " ; " Incompatible parameter type [ 6 ] : In call ` TypedDictionary . setdefault ` , for 2nd positional \ only parameter expected ` str ` but got ` int ` . " ; " TypedDict accessed with a missing key [ 27 ] : TypedDict ` Movie ` has no key ` nme ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie movie . update ( ) movie . update ( name = " newName " ) movie . update ( year = 15 ) movie . update ( name = " newName " , year = 15 ) } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie movie . update ( name = 15 , year = " backwards " ) movie . update ( yar = " missing " ) } | [ " Incompatible parameter type [ 6 ] : In call ` TypedDictionary . update ` , for 1st parameter ` name ` \ expected ` str ` but got ` int ` . " ; " Incompatible parameter type [ 6 ] : In call ` TypedDictionary . update ` , for 2nd parameter ` year ` \ expected ` int ` but got ` str ` . " ; " Unexpected keyword [ 28 ] : Unexpected keyword argument ` yar ` to call " ^ " ` TypedDictionary . update ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) movie1 : Movie movie2 : Movie movie2 . update ( movie1 ) movie2 . update ( 7 ) } | [ " Incompatible parameter type [ 6 ] : In call ` TypedDictionary . update ` , for 1st positional only \ parameter expected ` Movie ` but got ` int ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions # MovieNonTotal = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } , total = False ) MovieNonTotal = mypy_extensions . TypedDict ( ' MovieNonTotal ' , { ' name ' : str , ' year ' : ' int ' } , total = False ) def f ( ) -> None : movieNonTotal : MovieNonTotal v = movieNonTotal . pop ( " name " ) reveal_type ( v ) v = movieNonTotal . pop ( " name " , False ) reveal_type ( v ) v = movieNonTotal . pop ( " nae " , False ) } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is ` str ` . " ; " Revealed type [ - 1 ] : Revealed type for ` v ` is " ^ " ` typing . Union [ typing_extensions . Literal [ False ] , str ] ` . " ; " TypedDict accessed with a missing key [ 27 ] : TypedDict ` MovieNonTotal ` has no key ` nae ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie movie . pop ( " name " ) } | [ " Undefined attribute [ 16 ] : ` Movie ` has no attribute ` pop ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions MovieNonTotal = mypy_extensions . TypedDict ( ' MovieNonTotal ' , { ' name ' : str , ' year ' : ' int ' } , total = False ) def f ( ) -> None : movieNonTotal : MovieNonTotal movieNonTotal . __delitem__ ( " name " ) movieNonTotal . __delitem__ ( " nae " ) } | [ " TypedDict accessed with a missing key [ 27 ] : TypedDict ` MovieNonTotal ` has no key ` nae ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie movie . __delitem__ ( " name " ) } | [ " Undefined attribute [ 16 ] : ` Movie ` has no attribute ` __delitem__ ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions MovieNonTotal = mypy_extensions . TypedDict ( ' MovieNonTotal ' , { ' name ' : str , ' year ' : ' int ' } , total = False ) MoviePlus = mypy_extensions . TypedDict ( ' MoviePlus ' , { ' name ' : str , ' year ' : ' int ' , ' director ' : str } ) def f ( ) -> None : moviePlus : MoviePlus movieNonTotal : MovieNonTotal v = movieNonTotal . get ( " name " , False ) reveal_type ( v ) v = len ( movieNonTotal ) reveal_type ( v ) v = movieNonTotal . setdefault ( ' name ' , " n " ) reveal_type ( v ) } | [ " Revealed type [ - 1 ] : Revealed type for ` v ` is " ^ " ` typing . Union [ typing_extensions . Literal [ False ] , str ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` v ` is ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` v ` is ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> None : movie : Movie movie [ ' name ' ] += 7 } | [ " Unsupported operand [ 58 ] : ` ` + is not supported for operand types ` str ` and ` int ` . " ; " Invalid TypedDict operation [ 54 ] : Expected ` str ` to be assigned to ` Movie ` field ` name ` but \ got ` int ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) ReversedMovie = mypy_extensions . TypedDict ( ' ReversedMovie ' , { ' year ' : ' int ' , ' name ' : str } ) def f ( ) -> None : movie : Movie movie [ ' name ' ] = 7 reversedMovie : ReversedMovie reversedMovie [ ' name ' ] = 7 } | [ " Invalid TypedDict operation [ 54 ] : Expected ` str ` to be assigned to ` Movie ` field ` name ` but \ got ` int ` . " ; " Invalid TypedDict operation [ 54 ] : Expected ` str ` to be assigned to ` ReversedMovie ` field \ ` name ` but got ` int ` . " ; ] ; assert_test_typed_dictionary { | from foo . bar . baz import ClassBasedTypedDictGreekLetters def f ( ) -> int : baz = ClassBasedTypedDictGreekLetters ( alpha = 7 , beta = " a " , gamma = True ) return baz [ ' alpha ' ] } | [ ] ; assert_test_typed_dictionary { | from foo . bar . baz import ClassBasedTypedDictGreekLetters def f ( ) -> int : baz = ClassBasedTypedDictGreekLetters ( alpha = 7 , gamma = True ) return baz [ ' alpha ' ] } | [ " Missing argument [ 20 ] : Call ` __init__ ` expects argument ` beta ` . " ] ; assert_test_typed_dictionary { | from foo . bar . baz import ClassBasedNonTotalTypedDictGreekLetters def f ( ) -> int : baz = ClassBasedNonTotalTypedDictGreekLetters ( alpha = 7 , gamma = True ) return baz [ ' alpha ' ] } | [ ] ; assert_test_typed_dictionary { | from foo . bar . baz import DecoratedClassBasedTypedDictGreekLetters def f ( ) -> int : baz = DecoratedClassBasedTypedDictGreekLetters ( alpha = 7 , beta = " a " , gamma = True ) return baz [ ' alpha ' ] } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions NamelessTypedDict = mypy_extensions . TypedDict ( { ' name ' : str , ' year ' : int } ) def foo ( x : int ) -> str : return " " def f ( ) -> None : movie : NamelessTypedDict a = foo ( movie [ ' year ' ] ) } | [ " Missing global annotation [ 5 ] : Globally accessible variable ` NamelessTypedDict ` " ^ " has no type specified . " ; " Missing argument [ 20 ] : Call ` mypy_extensions . TypedDict ` expects argument ` fields ` . " ; " Undefined or invalid type [ 11 ] : Annotation ` NamelessTypedDict ` is not defined as a type . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : Movie ) -> str : return x [ " name " ] def f ( x : str , y : int ) -> None : foo ( { ' name ' : x , ' year ' : y } ) } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : Movie ) -> str : return x [ " name " ] def f ( ) -> None : foo ( { ' name ' : " Blade Runner " , ' year ' : 1982 } ) } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : Movie ) -> str : return x [ " name " ] def f ( ) -> None : foo ( { ' name ' : ' Blade Runner ' , ' year ' : ' 1982 ' } ) } | [ " TypedDict initialization error [ 55 ] : Expected type ` int ` for ` Movie ` field ` year ` but got \ ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : Movie ) -> str : return x [ " name " ] def f ( x : str , y : int ) -> None : foo ( { ' name ' : ' Blade Runner ' , x : y } ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` Movie ` but got ` Dict [ str , Union [ int , str ] ] ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def foo ( x : Movie ) -> str : return x [ " name " ] def f ( ) -> None : foo ( { ' name ' : " Blade Runner " , ' year ' : 1982 , ' extra_key ' : 1 } ) } | [ " TypedDict initialization error [ 55 ] : TypedDict ` Movie ` has no field ` extra_key ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> str : movie : Movie = { ' name ' : " Blade Runner " , ' year ' : 1982 } return movie [ ' name ' ] } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( x : bool ) -> str : movie : Movie = { ' name ' : " Blade Runner " , ' year ' : 1982 , ' bonus ' : x } reveal_type ( movie ) return movie [ ' name ' ] } | [ " TypedDict initialization error [ 55 ] : TypedDict ` Movie ` has no field ` bonus ` . " ; " Revealed type [ - 1 ] : Revealed type for ` movie ` is ` Movie ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> str : movie : Movie = { ' name ' : " Blade Runner " , ' year ' : ' 1982 ' } return movie [ ' name ' ] } | [ " TypedDict initialization error [ 55 ] : Expected type ` int ` for ` Movie ` field ` year ` but got \ ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Movie ( mypy_extensions . TypedDict , total = False ) : name : str year : int def f ( ) -> int : movie : Movie = { ' name ' : " Blade Runner " } reveal_type ( movie ) # this will fail at runtime , but that ' s the cost of doing business with non - total # typeddicts return movie [ ' year ' ] } | [ " Revealed type [ - 1 ] : Revealed type for ` movie ` is ` Movie ` . " ] ; assert_test_typed_dictionary { | import mypy_extensions class Movie ( mypy_extensions . TypedDict , total = False ) : name : str year : int def f ( ) -> int : movie : Movie = { ' name ' : 1982 } return movie [ ' year ' ] } | [ " TypedDict initialization error [ 55 ] : Expected type ` str ` for ` Movie ` field ` name ` but got \ ` int ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> Movie : return { ' name ' : " Blade Runner " , ' year ' : 1982 } } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' scores ' : dict } ) def f ( ) -> Movie : return Movie ( scores = { " imdb " : 8 . 1 } ) } | [ " Invalid type parameters [ 24 ] : Generic type ` dict ` expects 2 type parameters , use \ ` typing . Dict ` to avoid runtime subscripting errors . " ; ] ; assert_test_typed_dictionary { | from typing import Dict import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' scores ' : Dict } ) def f ( ) -> Movie : return Movie ( scores = { " imdb " : 8 . 1 } ) } | [ " Invalid type parameters [ 24 ] : Generic type ` dict ` expects 2 type parameters , use \ ` typing . Dict ` to avoid runtime subscripting errors . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) def f ( ) -> Movie : return { ' name ' : " Blade Runner " , ' year ' : ' 1982 ' } } | [ " TypedDict initialization error [ 55 ] : Expected type ` int ` for ` Movie ` field ` year ` but got \ ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base ( ) : pass class Child ( Base ) : pass Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' something ' : Base } ) def f ( ) -> Movie : return { ' name ' : " Blade Runner " , ' something ' : Child ( ) } } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions class TotalTypedDict ( mypy_extensions . TypedDict ) : required : int foo = TotalTypedDict ( required = 0 ) } | [ ] ; assert_test_typed_dictionary { | import mypy_extensions class NotTotalTypedDict ( mypy_extensions . TypedDict , total = False ) : required : int foo = NotTotalTypedDict ( ) } | [ ] ; assert_test_typed_dictionary { | import typing import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : typing . Any , ' year ' : ' int ' } ) class Bar ( mypy_extensions . TypedDict ) : x : typing . Any } | [ " Prohibited any [ 33 ] : Explicit annotation for ` name ` cannot be ` Any ` . " ; " Prohibited any [ 33 ] : Explicit annotation for ` x ` cannot be ` Any ` . " ; ] ; assert_test_typed_dictionary { | import typing import mypy_extensions class Bar ( mypy_extensions . TypedDict ) : items : typing . List [ int ] foo : int } | [ ] ; assert_test_typed_dictionary { | import typing import mypy_extensions from typing import Protocol class HasField ( Protocol ) : some_field : int class RegularClass : some_field : int = 1 class Bar ( mypy_extensions . TypedDict ) : some_field : int def expects_has_field ( x : HasField ) -> None : . . . x : RegularClass d : Bar expects_has_field ( x ) expects_has_field ( d ) } | [ " Incompatible parameter type [ 6 ] : In call ` expects_has_field ` , for 1st positional only \ parameter expected ` HasField ` but got ` Bar ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions Movie = mypy_extensions . TypedDict ( ' Movie ' , { ' name ' : str , ' year ' : ' int ' } ) Movie2 = mypy_extensions . TypedDict ( ' Movie2 ' , { ' name ' : str } ) movie : Movie movie2 : Movie2 x = movie if True else movie2 reveal_type ( x ) } | [ " Missing global annotation [ 5 ] : Globally accessible variable ` x ` has type \ ` typing . Union [ Movie , Movie2 ] ` but no type is specified . " ; " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing . Union [ Movie , Movie2 ] ` . " ; ] ; assert_test_typed_dictionary { | from mypy_extensions import TypedDict class HasIllegalInitializedField ( TypedDict ) : foo : int bar : str = " hello " movie : HasIllegalInitializedField movie [ " bar " ] reveal_type ( movie [ " bar " ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` movie [ " \ bar " ] ` \ is ` str ` . " ] ; ( )
let test_check_typed_dictionary_inference context = let assert_test_typed_dictionary source = let mypy_extensions_stub = { handle = " mypy_extensions . pyi " ; source = { | import typing def TypedDict ( typename : str , fields : typing . Dict [ str , typing . Type [ _T ] ] , total : bool = . . . , ) -> typing . Type [ dict ] : . . . } ; | } in assert_type_errors ~ context ~ update_environment_with [ : mypy_extensions_stub ] source in assert_test_typed_dictionary { | from typing import Dict import mypy_extensions class FooTypedDict ( mypy_extensions . TypedDict ) : foo : int bar : str def baz ( data : Dict [ str , FooTypedDict ] ) -> None : pass baz ( data { ' = hello ' : { ' foo ' : 3 , ' bar ' : ' hello ' } } ) } | [ ] ; assert_test_typed_dictionary { | from typing import Dict import mypy_extensions class FooTypedDict ( mypy_extensions . TypedDict ) : foo : int bar : str data : Dict [ str , FooTypedDict ] = { ' hello ' : { ' foo ' : 3 , ' bar ' : ' hello ' } } } | [ ] ; assert_test_typed_dictionary { | from typing import Dict import mypy_extensions class FooTypedDict ( mypy_extensions . TypedDict ) : foo : int bar : str data : Dict [ str , Dict [ str , FooTypedDict ] ] = { ' hello ' : { ' nested_dictionary ' : { ' foo ' : 3 , ' bar ' : ' hello ' } } } } | [ ] ; assert_test_typed_dictionary { | from typing import Dict import mypy_extensions class FooTypedDict ( mypy_extensions . TypedDict ) : dict_within_typed_dict : Dict [ str , int ] bar : str data : Dict [ str , FooTypedDict ] = { ' hello ' : { ' dict_within_typed_dict ' : { ' x ' : 3 , ' y ' : 7 } , ' bar ' : ' hello ' } } } | [ ] ; assert_test_typed_dictionary { | from typing import Dict , Mapping , Union def baz ( data : Dict [ str , Union [ Mapping [ str , int ] , bool , int ] ] ) -> None : pass baz ( data { ' = hello ' : 3 } ) } | [ ] ; assert_test_typed_dictionary { | from typing import Dict import mypy_extensions class FooTypedDict ( mypy_extensions . TypedDict ) : foo : int bar : str class NestedTypedDict ( mypy_extensions . TypedDict ) : foo : FooTypedDict bar : str data : Dict [ str , NestedTypedDict ] = { ' hello ' : { ' foo ' : { ' foo ' : 3 , ' bar ' : ' hello ' } , ' bar ' : ' hello ' } } } | [ ] ; assert_test_typed_dictionary { | from typing import Dict , Protocol import mypy_extensions class NonTotal ( mypy_extensions . TypedDict , total = False ) : foo : int bar : str class Total ( mypy_extensions . TypedDict ) : foo : int bar : str class Copyable ( Protocol ) : def keys ( self ) -> object : . . . class Poppable ( Protocol ) : def pop ( self ) -> object : . . . def expects_copyable ( x : Copyable ) -> None : . . . def expects_poppable ( x : Poppable ) -> None : . . . def foo ( n : NonTotal , t : Total ) -> None : expects_copyable ( n ) expects_copyable ( t ) expects_poppable ( t ) # total dicts are not poppable expects_poppable ( n ) } | [ " Incompatible parameter type [ 6 ] : In call ` expects_poppable ` , for 1st positional only \ parameter expected ` Poppable ` but got ` Total ` . " ; ] ; assert_test_typed_dictionary { | from typing import Dict , Optional import mypy_extensions class FooTypedDict ( mypy_extensions . TypedDict ) : foo : int bar : str data : Optional [ FooTypedDict ] = { ' foo ' : 3 , ' bar ' : ' hello ' } } | [ ] ; ( )
let test_check_typed_dictionary_inheritance context = let assert_test_typed_dictionary source = let mypy_extensions_stub = { handle = " mypy_extensions . pyi " ; source = { | import typing def TypedDict ( typename : str , fields : typing . Dict [ str , typing . Type [ _T ] ] , total : bool = . . . , ) -> typing . Type [ dict ] : . . . } ; | } in let typed_dictionary_helpers = { handle = " helpers . py " ; source = { | import mypy_extensions class Base ( mypy_extensions . TypedDict ) : foo : int class Child ( Base ) : bar : str class GrandChild ( Child ) : baz : str class ExplicitChild ( mypy_extensions . TypedDict ) : foo : int bar : str class NonChild ( mypy_extensions . TypedDict ) : foo : int baz : str class Movie ( mypy_extensions . TypedDict ) : name : str year : int class MultipleInheritance ( GrandChild , Movie ) : total : int def takes_base ( d : Base ) -> None : . . . def takes_child ( d : Child ) -> None : . . . def takes_grandchild ( d : GrandChild ) -> None : . . . def takes_explicit_child ( d : ExplicitChild ) -> None : . . . def takes_nonchild ( d : NonChild ) -> None : . . . base : Base = { " foo " : 3 } child : Child = { " foo " : 3 , " bar " : " hello " } grandchild : GrandChild = { " foo " : 3 , " bar " : " hello " , " baz " : " world " } explicit_child : ExplicitChild = { " foo " : 3 , " bar " : " hello " } non_child : NonChild = { " foo " : 3 , " baz " : " hello " } } ; | } in assert_type_errors ~ context ~ update_environment_with [ : mypy_extensions_stub ; typed_dictionary_helpers ] source in assert_test_typed_dictionary { | from helpers import Base , Child , GrandChild , child , grandchild d : Base reveal_type ( d ) d : GrandChild = child child [ " bar " ] reveal_type ( child [ " bar " ] ) grandchild [ " bar " ] reveal_type ( grandchild [ " bar " ] ) grandchild [ " foo " ] reveal_type ( grandchild [ " foo " ] ) grandchild [ " non_existent " ] # An attribute from a superclass shouldn ' t be seen as a field . grandchild [ " __doc__ " ] } | [ " Revealed type [ - 1 ] : Revealed type for ` d ` is ` Base ` . " ; " Incompatible variable type [ 9 ] : d is declared to have type ` GrandChild ` but is used as type \ ` Child ` . " ; " Revealed type [ - 1 ] : Revealed type for ` helpers . child [ " \ bar " ] ` \ is ` str ` . " ; " Revealed type [ - 1 ] : Revealed type for ` helpers . grandchild [ " \ bar " ] ` \ is ` str ` . " ; " Revealed type [ - 1 ] : Revealed type for ` helpers . grandchild [ " \ foo " ] ` \ is ` int ` . " ; " TypedDict accessed with a missing key [ 27 ] : TypedDict ` GrandChild ` has no key \ ` non_existent ` . " ; " TypedDict accessed with a missing key [ 27 ] : TypedDict ` GrandChild ` has no key ` __doc__ ` . " ; ] ; assert_test_typed_dictionary { | from helpers import child , Child child . bar reveal_type ( child . bar ) child . non_existent reveal_type ( child . non_existent ) } | [ " Undefined attribute [ 16 ] : ` Child ` has no attribute ` bar ` . " ; " Revealed type [ - 1 ] : Revealed type for ` helpers . child . bar ` is ` unknown ` . " ; " Undefined attribute [ 16 ] : ` Child ` has no attribute ` non_existent ` . " ; " Revealed type [ - 1 ] : Revealed type for ` helpers . child . non_existent ` is ` unknown ` . " ; ] ; assert_test_typed_dictionary { | from helpers import Base , Child , GrandChild wrong1 : Base = { } wrong2 : Child = { " foo " : 3 } wrong3 : GrandChild = { " foo " : 3 , " bar " : " hello " } correct1 : Base = { " foo " : 3 } correct2 : Child = { " foo " : 3 , " bar " : " hello " } correct3 : GrandChild = { " foo " : 3 , " bar " : " hello " , " baz " : " world " } } | [ " TypedDict initialization error [ 55 ] : Missing required field ` foo ` for TypedDict ` Base ` . " ; " TypedDict initialization error [ 55 ] : Missing required field ` bar ` for TypedDict ` Child ` . " ; " TypedDict initialization error [ 55 ] : Missing required field ` baz ` for TypedDict \ ` GrandChild ` . " ; ] ; assert_test_typed_dictionary { | from helpers import Base , base , child , grandchild , explicit_child , non_child x0 : Base = base x1 : Base = child x2 : Base = grandchild x3 : Base = explicit_child x4 : Base = non_child } | [ ] ; assert_test_typed_dictionary { | from helpers import Base , Child , NonChild , child , base , grandchild , explicit_child , non_child from typing_extensions import * x0 : Child = child x1 : Child = base x2 : Child = grandchild x3 : Child = explicit_child x4 : Child = non_child } | [ " Incompatible variable type [ 9 ] : x1 is declared to have type ` Child ` but is used as type \ ` Base ` . " ; " Incompatible variable type [ 9 ] : x4 is declared to have type ` Child ` but is used as type \ ` NonChild ` . " ; ] ; assert_test_typed_dictionary { | from helpers import Base , Child , ExplicitChild , NonChild , GrandChild from helpers import grandchild , base , child , explicit_child , non_child x0 : GrandChild = grandchild x1 : GrandChild = base x2 : GrandChild = child x3 : GrandChild = explicit_child x4 : GrandChild = non_child } | [ " Incompatible variable type [ 9 ] : x1 is declared to have type ` GrandChild ` but is used as \ type ` Base ` . " ; " Incompatible variable type [ 9 ] : x2 is declared to have type ` GrandChild ` but is used as \ type ` Child ` . " ; " Incompatible variable type [ 9 ] : x3 is declared to have type ` GrandChild ` but is used as \ type ` ExplicitChild ` . " ; " Incompatible variable type [ 9 ] : x4 is declared to have type ` GrandChild ` but is used as \ type ` NonChild ` . " ; ] ; assert_test_typed_dictionary { | from helpers import ExplicitChild , Base , NonChild from helpers import explicit_child , base , child , grandchild , non_child x0 : ExplicitChild = explicit_child x1 : ExplicitChild = base x2 : ExplicitChild = child x3 : ExplicitChild = grandchild x4 : ExplicitChild = non_child } | [ " Incompatible variable type [ 9 ] : x1 is declared to have type ` ExplicitChild ` but is used as \ type ` Base ` . " ; " Incompatible variable type [ 9 ] : x4 is declared to have type ` ExplicitChild ` but is used as \ type ` NonChild ` . " ; ] ; assert_test_typed_dictionary { | from helpers import NonChild , Child , Base , ExplicitChild from helpers import explicit_child , base , child , grandchild , non_child x0 : NonChild = non_child x1 : NonChild = base x2 : NonChild = child x3 : NonChild = grandchild x4 : NonChild = explicit_child } | [ " Incompatible variable type [ 9 ] : x1 is declared to have type ` NonChild ` but is used as type \ ` Base ` . " ; " Incompatible variable type [ 9 ] : x2 is declared to have type ` NonChild ` but is used as type \ ` Child ` . " ; " Incompatible variable type [ 9 ] : x4 is declared to have type ` NonChild ` but is used as type \ ` ExplicitChild ` . " ; ] ; assert_test_typed_dictionary { | from helpers import takes_base , base , child , grandchild , explicit_child , non_child takes_base ( base ) takes_base ( child ) takes_base ( grandchild ) takes_base ( explicit_child ) takes_base ( non_child ) } | [ ] ; assert_test_typed_dictionary { | from helpers import Base , Child , NonChild from helpers import takes_child , base , child , grandchild , explicit_child , non_child takes_child ( base ) takes_child ( child ) takes_child ( grandchild ) takes_child ( explicit_child ) takes_child ( non_child ) } | [ " Incompatible parameter type [ 6 ] : In call ` takes_child ` , for 1st positional only parameter \ expected ` Child ` but got ` Base ` . " ; " Incompatible parameter type [ 6 ] : In call ` takes_child ` , for 1st positional only parameter \ expected ` Child ` but got ` NonChild ` . " ; ] ; assert_test_typed_dictionary { | from helpers import Base , GrandChild , Child , ExplicitChild , NonChild from helpers import takes_grandchild , base , child , grandchild , explicit_child , non_child takes_grandchild ( base ) takes_grandchild ( child ) takes_grandchild ( grandchild ) takes_grandchild ( explicit_child ) takes_grandchild ( non_child ) } | [ " Incompatible parameter type [ 6 ] : In call ` takes_grandchild ` , for 1st positional only \ parameter expected ` GrandChild ` but got ` Base ` . " ; " Incompatible parameter type [ 6 ] : In call ` takes_grandchild ` , for 1st positional only \ parameter expected ` GrandChild ` but got ` Child ` . " ; " Incompatible parameter type [ 6 ] : In call ` takes_grandchild ` , for 1st positional only \ parameter expected ` GrandChild ` but got ` ExplicitChild ` . " ; " Incompatible parameter type [ 6 ] : In call ` takes_grandchild ` , for 1st positional only \ parameter expected ` GrandChild ` but got ` NonChild ` . " ; ] ; assert_test_typed_dictionary { | from helpers import Base , NonChild , Child , ExplicitChild from helpers import takes_nonchild , base , child , grandchild , explicit_child , non_child takes_nonchild ( base ) takes_nonchild ( child ) takes_nonchild ( grandchild ) takes_nonchild ( explicit_child ) takes_nonchild ( non_child ) } | [ " Incompatible parameter type [ 6 ] : In call ` takes_nonchild ` , for 1st positional only \ parameter expected ` NonChild ` but got ` Base ` . " ; " Incompatible parameter type [ 6 ] : In call ` takes_nonchild ` , for 1st positional only \ parameter expected ` NonChild ` but got ` Child ` . " ; " Incompatible parameter type [ 6 ] : In call ` takes_nonchild ` , for 1st positional only \ parameter expected ` NonChild ` but got ` ExplicitChild ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class TotalBase ( mypy_extensions . TypedDict ) : foo : int class NonTotalChild ( TotalBase , total = False ) : bar : str def f ( ) -> None : d : NonTotalChild = { " foo " : 1 } reveal_type ( d [ " bar " ] ) d2 : NonTotalChild = { " bar " : " hello " } } | [ " Revealed type [ - 1 ] : Revealed type for ` d [ " \ bar " ] ` \ is ` str ` . " ; " TypedDict initialization error [ 55 ] : Missing required field ` foo ` for TypedDict \ ` NonTotalChild ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class NonTotalBase ( mypy_extensions . TypedDict , total = False ) : foo : int class TotalChild ( NonTotalBase ) : bar : str def f ( ) -> None : d : TotalChild = { " bar " : " hello " } reveal_type ( d ) reveal_type ( d [ " foo " ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` d ` is ` TotalChild ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d [ " \ foo " ] ` \ is ` int ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class NonTotalBase ( mypy_extensions . TypedDict , total = False ) : foo : int class NonTotalChild ( NonTotalBase , total = False ) : bar : str d : NonTotalChild reveal_type ( d [ " foo " ] ) reveal_type ( d [ " bar " ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` d [ " \ foo " ] ` \ is ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d [ " \ bar " ] ` \ is ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class TotalBase ( mypy_extensions . TypedDict ) : foo : int class NonTotalChild ( TotalBase , total = False ) : bar : str class TotalChild ( TotalBase ) : bar : str d : NonTotalChild d2 : TotalChild d3 : TotalBase = d d4 : TotalBase = d2 } | [ ] ; assert_test_typed_dictionary { | from helpers import Child , child child : Child child [ " foo " ] child [ " bar " ] child [ " non_existent " ] reveal_type ( child [ " foo " ] ) reveal_type ( child [ " bar " ] ) } | [ " TypedDict accessed with a missing key [ 27 ] : TypedDict ` Child ` has no key ` non_existent ` . " ; " Revealed type [ - 1 ] : Revealed type for ` child [ " \ foo " ] ` \ is ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` child [ " \ bar " ] ` \ is ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base ( mypy_extensions . TypedDict ) : foo : int class Child ( Base , total = False ) : bar : int child : Child y : int = child . pop ( " foo " ) y : int = child . pop ( " bar " ) child . __delitem__ ( " foo " ) child . __delitem__ ( " bar " ) } | [ " Invalid TypedDict operation [ 54 ] : Cannot ` pop ` required field ` foo ` from TypedDict ` Child ` . " ; " Invalid TypedDict operation [ 54 ] : Cannot delete required field ` foo ` from TypedDict ` Child ` . " ; ] ; assert_test_typed_dictionary { | from helpers import MultipleInheritance d : MultipleInheritance reveal_type ( d ) x : int = d [ " bar " ] y : str = d [ " total " ] } | [ " Revealed type [ - 1 ] : Revealed type for ` d ` is ` MultipleInheritance ` . " ; " Incompatible variable type [ 9 ] : x is declared to have type ` int ` but is used as type ` str ` . " ; " Incompatible variable type [ 9 ] : y is declared to have type ` str ` but is used as type ` int ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base ( mypy_extensions . TypedDict ) : foo : int class Child ( Base ) : . . . d : Child x : str = d [ " foo " ] reveal_type ( d ) } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` str ` but is used as type ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d ` is ` Child ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base ( mypy_extensions . TypedDict ) : foo : int class Child ( Base ) : foo : int reveal_type ( Child . __init__ ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . Child . __init__ ` is \ ` typing . Callable ( __init__ ) [ . . . , unknown ] [ [ [ Named ( self , unknown ) , KeywordOnly ( foo , int ) ] , \ Child ] [ [ Named ( self , unknown ) , Child ] , Child ] ] ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base1 ( mypy_extensions . TypedDict ) : foo : int class Base2 ( mypy_extensions . TypedDict ) : foo : int class Child ( Base1 , Base2 ) : foo : int reveal_type ( Child . __init__ ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . Child . __init__ ` is \ ` typing . Callable ( __init__ ) [ . . . , unknown ] [ [ [ Named ( self , unknown ) , KeywordOnly ( foo , int ) ] , \ Child ] [ [ Named ( self , unknown ) , Child ] , Child ] ] ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base ( mypy_extensions . TypedDict ) : foo : int class Base2 ( mypy_extensions . TypedDict ) : bar : str class Child ( Base , Base2 ) : foo : str bar : int reveal_type ( Child . __init__ ) } | [ " Inconsistent override [ 15 ] : ` bar ` overrides attribute defined in ` Base2 ` inconsistently . \ Type ` int ` is not a subtype of the overridden attribute ` str ` . " ; " Inconsistent override [ 15 ] : ` foo ` overrides attribute defined in ` Base ` inconsistently . \ Type ` str ` is not a subtype of the overridden attribute ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . Child . __init__ ` is \ ` typing . Callable ( __init__ ) [ . . . , unknown ] [ [ [ Named ( self , unknown ) , KeywordOnly ( bar , int ) , \ KeywordOnly ( foo , str ) ] , Child ] [ [ Named ( self , unknown ) , Child ] , Child ] ] ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base ( mypy_extensions . TypedDict ) : foo : int class Child ( Base , total = False ) : foo : int } | [ " Inconsistent override [ 15 ] : ` foo ` overrides attribute defined in ` Base ` inconsistently . \ Type ` int ` is not a subtype of the overridden attribute ` int ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base1 ( mypy_extensions . TypedDict ) : foo : int bar : str class Base2 ( mypy_extensions . TypedDict ) : foo : str bar : int class Child ( Base1 , Base2 ) : baz : str d : Child x : int = d [ " bar " ] y : str = d [ " bar " ] } | [ " Invalid inheritance [ 39 ] : Field ` bar ` has type ` str ` in base class ` Base1 ` and type ` int ` \ in base class ` Base2 ` . " ; " Invalid inheritance [ 39 ] : Field ` foo ` has type ` int ` in base class ` Base1 ` and type ` str ` \ in base class ` Base2 ` . " ; " Incompatible variable type [ 9 ] : x is declared to have type ` int ` but is used as type ` str ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base1 ( mypy_extensions . TypedDict ) : foo : int class Base2 ( mypy_extensions . TypedDict , total = False ) : foo : int class Child ( Base1 , Base2 ) : baz : str } | [ " Invalid inheritance [ 39 ] : ` foo ` is a required field in base class ` Base1 ` and a \ non - required field in base class ` Base2 ` ( because of ` total = False ` ) . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base1 ( mypy_extensions . TypedDict ) : foo : int class Base2 ( mypy_extensions . TypedDict ) : foo : str class Child ( Base1 , Base2 ) : foo : str } | [ " Invalid inheritance [ 39 ] : Field ` foo ` has type ` int ` in base class ` Base1 ` and type ` str ` \ in base class ` Base2 ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions class Base ( mypy_extensions . TypedDict ) : foo : int class NonTypedDict : not_a_field : int class Child ( Base , NonTypedDict ) : baz : str class NonTotalChild ( Base , NonTypedDict , total = False ) : non_total_baz : str reveal_type ( Child . __init__ ) reveal_type ( NonTotalChild . __init__ ) } | [ " Uninitialized attribute [ 13 ] : Attribute ` not_a_field ` is declared in class ` NonTypedDict ` \ to have type ` int ` but is never initialized . " ; " Invalid inheritance [ 39 ] : ` NonTypedDict ` is not a valid parent class for a typed \ dictionary . Expected a typed dictionary . " ; " Invalid inheritance [ 39 ] : ` NonTypedDict ` is not a valid parent class for a typed \ dictionary . Expected a typed dictionary . " ; " Revealed type [ - 1 ] : Revealed type for ` test . Child . __init__ ` is \ ` typing . Callable ( __init__ ) [ . . . , unknown ] [ [ [ Named ( self , unknown ) , KeywordOnly ( baz , str ) , \ KeywordOnly ( foo , int ) ] , Child ] [ [ Named ( self , unknown ) , Child ] , Child ] ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . NonTotalChild . __init__ ` is \ ` typing . Callable ( __init__ ) [ . . . , unknown ] [ [ [ Named ( self , unknown ) , KeywordOnly ( foo , int ) , \ KeywordOnly ( non_total_baz , str , default ) ] , NonTotalChild ] [ [ Named ( self , unknown ) , \ NonTotalChild ] , NonTotalChild ] ] ` . " ; ] ; ( )
let test_check_typed_dictionary_in_alias context = let assert_test_typed_dictionary source = let mypy_extensions_stub = { handle = " mypy_extensions . pyi " ; source = { | import typing def TypedDict ( typename : str , fields : typing . Dict [ str , typing . Type [ _T ] ] , total : bool = . . . , ) -> typing . Type [ dict ] : . . . } ; | } in let typed_dictionary_helpers = { handle = " helpers . py " ; source = { | import mypy_extensions class Base ( mypy_extensions . TypedDict ) : foo : int class Child ( Base ) : bar : str class GrandChild ( Child ) : baz : str class ExplicitChild ( mypy_extensions . TypedDict ) : foo : int bar : str class NonChild ( mypy_extensions . TypedDict ) : foo : int baz : str class Movie ( mypy_extensions . TypedDict ) : name : str year : int class MultipleInheritance ( GrandChild , Movie ) : total : int def takes_base ( d : Base ) -> None : . . . def takes_child ( d : Child ) -> None : . . . def takes_grandchild ( d : GrandChild ) -> None : . . . def takes_explicit_child ( d : ExplicitChild ) -> None : . . . def takes_nonchild ( d : NonChild ) -> None : . . . base : Base = { " foo " : 3 } child : Child = { " foo " : 3 , " bar " : " hello " } grandchild : GrandChild = { " foo " : 3 , " bar " : " hello " , " baz " : " world " } explicit_child : ExplicitChild = { " foo " : 3 , " bar " : " hello " } non_child : NonChild = { " foo " : 3 , " baz " : " hello " } } ; | } in assert_type_errors ~ context ~ update_environment_with [ : mypy_extensions_stub ; typed_dictionary_helpers ] source in assert_test_typed_dictionary { | from helpers import child , Child from typing import List X = Child xs : X = child ys : X reveal_type ( xs ) reveal_type ( ys ) Y = List [ Child ] y : Y reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` xs ` is ` Child ` . " ; " Revealed type [ - 1 ] : Revealed type for ` ys ` is ` Child ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` List [ Child ] ` . " ; ] ; assert_test_typed_dictionary { | from helpers import Child , child from typing import List X = List [ Child ] xs : X = [ child , child ] ys : X = 1 } | [ " Incompatible variable type [ 9 ] : ys is declared to have type ` List [ Child ] ` but is used as \ type ` int ` . " ; ] ; assert_test_typed_dictionary { | from helpers import Child , Base , grandchild , base , child from typing import Callable , List f : Callable [ [ Child ] , None ] f ( grandchild ) f ( base ) xs : List [ Child ] = [ child , child ] } | [ " Incompatible parameter type [ 6 ] : In anonymous call , for 1st positional only parameter \ expected ` Child ` but got ` Base ` . " ; ] ; assert_test_typed_dictionary { | from helpers import Child , Base , GrandChild , base , child from typing import Generic , TypeVar T = TypeVar ( " T " ) class G ( Generic [ T ] ) : x : T def __init__ ( self , x : T ) -> None : self . x = x def return_T ( self ) -> T : . . . class C ( G [ Child ] ) : . . . reveal_type ( C . __init__ ) reveal_type ( C . return_T ) C ( base ) d : Base = C ( child ) . x reveal_type ( d ) d2 : GrandChild = C ( child ) . x d3 : GrandChild = C ( child ) . return_T ( ) reveal_type ( C ( child ) . x ) reveal_type ( C ( child ) ) def foo ( x : G [ T ] ) -> T : return x . return_T ( ) def bar ( c : C ) -> None : x = foo ( c ) reveal_type ( x ) y = c . return_T ( ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . C . __init__ ` is \ ` typing . Callable ( G . __init__ ) [ [ Named ( self , G [ Child ] ) , Named ( x , Child ) ] , None ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . C . return_T ` is \ ` typing . Callable ( G . return_T ) [ [ Named ( self , G [ Child ] ) ] , Child ] ` . " ; " Incompatible parameter type [ 6 ] : In call ` G . __init__ ` , for 1st positional only parameter \ expected ` Child ` but got ` Base ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d ` is ` Base ` ( inferred : ` Child ` ) . " ; " Incompatible variable type [ 9 ] : d2 is declared to have type ` GrandChild ` but is used as \ type ` Child ` . " ; " Incompatible variable type [ 9 ] : d3 is declared to have type ` GrandChild ` but is used as \ type ` Child ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . C ( helpers . child ) . x ` is ` Child ` . " ; " Revealed type [ - 1 ] : Revealed type for ` test . C ( helpers . child ) ` is ` C ` . " ; " Revealed type [ - 1 ] : Revealed type for ` x ` is ` Child ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Child ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions from typing import List X = List [ int ] class RegularTypedDict ( mypy_extensions . TypedDict ) : use_alias : X other_alias : List [ X ] d : RegularTypedDict reveal_type ( d [ " use_alias " ] ) reveal_type ( d [ " other_alias " ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` d [ " \ use_alias " ] ` \ is ` List [ int ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d [ " \ other_alias " ] ` \ is ` List [ List [ int ] ] ` . " ; ] ; assert_test_typed_dictionary { | from helpers import * from typing import List X = List [ int ] class OtherChild ( Base ) : use_alias : X other_alias : List [ X ] d : OtherChild reveal_type ( d [ " use_alias " ] ) reveal_type ( d [ " other_alias " ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` d [ " \ use_alias " ] ` \ is ` List [ int ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d [ " \ other_alias " ] ` \ is ` List [ List [ int ] ] ` . " ; ] ; assert_test_typed_dictionary { | import mypy_extensions from typing import List X = List [ int ] class RegularTypedDict ( mypy_extensions . TypedDict ) : use_alias : X other_alias : List [ X ] Y = List [ RegularTypedDict ] d : Y reveal_type ( d ) reveal_type ( d [ 0 ] [ " use_alias " ] ) reveal_type ( d [ 0 ] [ " other_alias " ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` d ` is ` List [ RegularTypedDict ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d [ 0 ] [ " \ use_alias " ] ` \ is ` List [ int ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d [ 0 ] [ " \ other_alias " ] ` \ is ` List [ List [ int ] ] ` . " ; ] ; assert_test_typed_dictionary { | from helpers import * from typing import List X = List [ int ] class OtherChild ( Base ) : use_alias : X other_alias : List [ X ] Y = List [ OtherChild ] d : Y reveal_type ( d [ 0 ] [ " other_alias " ] ) reveal_type ( d [ 0 ] [ " foo " ] ) } | [ " Revealed type [ - 1 ] : Revealed type for ` d [ 0 ] [ " \ other_alias " ] ` \ is ` List [ List [ int ] ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d [ 0 ] [ " \ foo " ] ` \ is ` int ` . " ; ] ; assert_test_typed_dictionary { | from helpers import Child from typing import Callable , List def decorator ( f : Callable [ [ int ] , str ] ) -> Callable [ [ Child ] , Child ] : . . . @ decorator def foo ( x : int ) -> str : . . . reveal_type ( foo ( 1 ) ) d : int = foo ( 1 ) } | [ " Revealed type [ - 1 ] : Revealed type for ` test . foo ( 1 ) ` is ` Child ` . " ; " Incompatible variable type [ 9 ] : d is declared to have type ` int ` but is used as type \ ` Child ` . " ; " Incompatible parameter type [ 6 ] : In anonymous call , for 1st positional only parameter \ expected ` Child ` but got ` int ` . " ; ] ; ( )
let test_check_optional_typed_dictionary context = assert_type_errors ~ context { | from typing import Any , TypedDict , Optional class MyDict ( TypedDict ) : a : str b : str def foo ( b : bool ) -> Optional [ MyDict ] : if b : return None return { " a " : str ( 1 ) , " b " : " hi " } def bar ( b : bool ) -> Optional [ MyDict ] : if b : return None return { " a " : str ( 1 ) , " b " : 0 } } | [ " TypedDict initialization error [ 55 ] : Expected type ` str ` for ` MyDict ` field ` b ` but got \ ` int ` . " ; ]
let ( ) = " typed_dictionary " >::: [ " check_typed_dictionaries " >:: test_check_typed_dictionaries ; " check_typed_dictionary_inference " >:: test_check_typed_dictionary_inference ; " check_typed_dictionary_inheritance " >:: test_check_typed_dictionary_inheritance ; " check_typed_dictionary_in_alias " >:: test_check_typed_dictionary_in_alias ; " check_optional_typed_dictionary " >:: test_check_optional_typed_dictionary ; ] |> Test . run
type native_repr_kind = Unboxed | Untagged
type error = Repeated_parameter | Duplicate_constructor of string | Too_many_constructors | Duplicate_label of string | Recursive_abbrev of string | Cycle_in_def of string * type_expr | Definition_mismatch of type_expr * Includecore . type_mismatch option | Constraint_failed of type_expr * type_expr | Inconsistent_constraint of Env . t * Ctype . Unification_trace . t | Type_clash of Env . t * Ctype . Unification_trace . t | Parameters_differ of Path . t * type_expr * type_expr | Null_arity_external | Missing_native_external | Unbound_type_var of type_expr * type_declaration | Cannot_extend_private_type of Path . t | Not_extensible_type of Path . t | Extension_mismatch of Path . t * Includecore . type_mismatch | Rebind_wrong_type of Longident . t * Env . t * Ctype . Unification_trace . t | Rebind_mismatch of Longident . t * Path . t * Path . t | Rebind_private of Longident . t | Variance of Typedecl_variance . error | Unavailable_type_constructor of Path . t | Bad_fixed_type of string | Unbound_type_var_ext of type_expr * extension_constructor | Val_in_structure | Multiple_native_repr_attributes | Cannot_unbox_or_untag_type of native_repr_kind | Deep_unbox_or_untag_attribute of native_repr_kind | Immediacy of Typedecl_immediacy . error | Bad_unboxed_attribute of string | Wrong_unboxed_type_float | Boxed_and_unboxed | Nonrec_gadt
let get_unboxed_from_attributes sdecl = let unboxed = Builtin_attributes . has_unboxed sdecl . ptype_attributes in let boxed = Builtin_attributes . has_boxed sdecl . ptype_attributes in match boxed , unboxed , ! Clflags . unboxed_types with | true , true , _ -> raise ( Error ( sdecl . ptype_loc , Boxed_and_unboxed ) ) | true , false , _ -> unboxed_false_default_false | false , true , _ -> unboxed_true_default_false | false , false , false -> unboxed_false_default_true | false , false , true -> unboxed_true_default_true
let add_type ~ check id decl env = Builtin_attributes . warning_scope ~ ppwarning : false decl . type_attributes ( fun ( ) -> Env . add_type ~ check id decl env )
let enter_type rec_flag env sdecl id = let needed = match rec_flag with | Asttypes . Nonrecursive -> begin match sdecl . ptype_kind with | Ptype_variant scds -> List . iter ( fun cd -> if cd . pcd_res <> None then raise ( Error ( cd . pcd_loc , Nonrec_gadt ) ) ) scds | _ -> ( ) end ; Btype . is_row_name ( Ident . name id ) | Asttypes . Recursive -> true in if not needed then env else let decl = { type_params = List . map ( fun _ -> Btype . newgenvar ( ) ) sdecl . ptype_params ; type_arity = List . length sdecl . ptype_params ; type_kind = Type_abstract ; type_private = sdecl . ptype_private ; type_manifest = begin match sdecl . ptype_manifest with None -> None | Some _ -> Some ( Ctype . newvar ( ) ) end ; type_variance = List . map ( fun _ -> Variance . full ) sdecl . ptype_params ; type_is_newtype = false ; type_expansion_scope = Btype . lowest_level ; type_loc = sdecl . ptype_loc ; type_attributes = sdecl . ptype_attributes ; type_immediate = Unknown ; type_unboxed = unboxed_false_default_false ; } in add_type ~ check : true id decl env
let update_type temp_env env id loc = let path = Path . Pident id in let decl = Env . find_type path temp_env in match decl . type_manifest with None -> ( ) | Some ty -> let params = List . map ( fun _ -> Ctype . newvar ( ) ) decl . type_params in try Ctype . unify env ( Ctype . newconstr path params ) ty with Ctype . Unify trace -> raise ( Error ( loc , Type_clash ( env , trace ) ) )
let get_unboxed_type_representation env ty = match Typedecl_unboxed . get_unboxed_type_representation env ty with | Typedecl_unboxed . This x -> Some x | _ -> None
let is_float env ty = match get_unboxed_type_representation env ty with Some { desc = Tconstr ( p , _ , _ ) ; _ } -> Path . same p Predef . path_float | _ -> false
let is_fixed_type sd = let rec has_row_var sty = match sty . ptyp_desc with Ptyp_alias ( sty , _ ) -> has_row_var sty | Ptyp_class _ | Ptyp_object ( _ , Open ) | Ptyp_variant ( _ , Open , _ ) | Ptyp_variant ( _ , Closed , Some _ ) -> true | _ -> false in match sd . ptype_manifest with None -> false | Some sty -> sd . ptype_kind = Ptype_abstract && sd . ptype_private = Private && has_row_var sty
let set_fixed_row env loc p decl = let tm = match decl . type_manifest with None -> assert false | Some t -> Ctype . expand_head env t in let rv = match tm . desc with Tvariant row -> let row = Btype . row_repr row in tm . desc <- Tvariant { row with row_fixed = Some Fixed_private } ; if Btype . static_row row then Btype . newgenty Tnil else row . row_more | Tobject ( ty , _ ) -> snd ( Ctype . flatten_fields ty ) | _ -> raise ( Error ( loc , Bad_fixed_type " is not an object or variant " ) ) in if not ( Btype . is_Tvar rv ) then raise ( Error ( loc , Bad_fixed_type " has no row variable " ) ) ; rv . desc <- Tconstr ( p , decl . type_params , ref Mnil )
let make_params env params = let make_param ( sty , v ) = try ( transl_type_param env sty , v ) with Already_bound -> raise ( Error ( sty . ptyp_loc , Repeated_parameter ) ) in List . map make_param params
let transl_labels env closed lbls = assert ( lbls <> [ ] ) ; let all_labels = ref String . Set . empty in List . iter ( fun { pld_name = { txt = name ; loc } } -> if String . Set . mem name ! all_labels then raise ( Error ( loc , Duplicate_label name ) ) ; all_labels := String . Set . add name ! all_labels ) lbls ; let mk { pld_name = name ; pld_mutable = mut ; pld_type = arg ; pld_loc = loc ; pld_attributes = attrs } = Builtin_attributes . warning_scope attrs ( fun ( ) -> let arg = Ast_helper . Typ . force_poly arg in let cty = transl_simple_type env closed arg in { ld_id = Ident . create_local name . txt ; ld_name = name ; ld_mutable = mut ; ld_type = cty ; ld_loc = loc ; ld_attributes = attrs } ) in let lbls = List . map mk lbls in let lbls ' = List . map ( fun ld -> let ty = ld . ld_type . ctyp_type in let ty = match ty . desc with Tpoly ( t , [ ] ) -> t | _ -> ty in { Types . ld_id = ld . ld_id ; ld_mutable = ld . ld_mutable ; ld_type = ty ; ld_loc = ld . ld_loc ; ld_attributes = ld . ld_attributes } ) lbls in lbls , lbls '
let transl_constructor_arguments env closed = function | Pcstr_tuple l -> let l = List . map ( transl_simple_type env closed ) l in Types . Cstr_tuple ( List . map ( fun t -> t . ctyp_type ) l ) , Cstr_tuple l | Pcstr_record l -> let lbls , lbls ' = transl_labels env closed l in Types . Cstr_record lbls ' , Cstr_record lbls
let make_constructor env type_path type_params sargs sret_type = match sret_type with | None -> let args , targs = transl_constructor_arguments env true sargs in targs , None , args , None , type_params | Some sret_type -> let z = narrow ( ) in reset_type_variables ( ) ; let args , targs = transl_constructor_arguments env false sargs in let tret_type = transl_simple_type env false sret_type in let ret_type = tret_type . ctyp_type in let params = match ( Ctype . repr ret_type ) . desc with | Tconstr ( p ' , params , _ ) when Path . same type_path p ' -> params | _ -> raise ( Error ( sret_type . ptyp_loc , Constraint_failed ( ret_type , Ctype . newconstr type_path type_params ) ) ) in widen z ; targs , Some tret_type , args , Some ret_type , params
let check_type_var loc univ id = let f t = ( Btype . repr t ) . id = id in if not ( List . exists f univ ) then raise ( Error ( loc , Wrong_unboxed_type_float ) )
let rec check_unboxed_abstract_arg loc univ ty = match ty . desc with | Tvar _ -> check_type_var loc univ ty . id | Tarrow ( _ , t1 , t2 , _ ) | Tfield ( _ , _ , t1 , t2 ) -> check_unboxed_abstract_arg loc univ t1 ; check_unboxed_abstract_arg loc univ t2 | Ttuple args | Tconstr ( _ , args , _ ) | Tpackage ( _ , _ , args ) -> List . iter ( check_unboxed_abstract_arg loc univ ) args | Tobject ( fields , r ) -> check_unboxed_abstract_arg loc univ fields ; begin match ! r with | None -> ( ) | Some ( _ , args ) -> List . iter ( check_unboxed_abstract_arg loc univ ) args end | Tnil | Tunivar _ -> ( ) | Tlink e -> check_unboxed_abstract_arg loc univ e | Tsubst _ -> assert false | Tvariant { row_fields ; row_more ; row_name } -> List . iter ( check_unboxed_abstract_row_field loc univ ) row_fields ; check_unboxed_abstract_arg loc univ row_more ; begin match row_name with | None -> ( ) | Some ( _ , args ) -> List . iter ( check_unboxed_abstract_arg loc univ ) args end | Tpoly ( t , _ ) -> check_unboxed_abstract_arg loc univ t match field with | Rpresent ( Some ty ) -> check_unboxed_abstract_arg loc univ ty | Reither ( _ , args , _ , r ) -> List . iter ( check_unboxed_abstract_arg loc univ ) args ; begin match ! r with | None -> ( ) | Some f -> check_unboxed_abstract_row_field loc univ ( " " , f ) end | Rabsent | Rpresent None -> ( )
let rec check_unboxed_gadt_arg loc univ env ty = match get_unboxed_type_representation env ty with | Some { desc = Tvar _ ; id } -> check_type_var loc univ id | Some { desc = Tarrow _ | Ttuple _ | Tpackage _ | Tobject _ | Tnil | Tvariant _ ; _ } -> ( ) | Some { desc = Tconstr ( p , args , _ ) ; _ } -> let tydecl = Env . find_type p env in assert ( not tydecl . type_unboxed . unboxed ) ; if tydecl . type_kind = Type_abstract then List . iter ( check_unboxed_abstract_arg loc univ ) args | Some { desc = Tfield _ | Tlink _ | Tsubst _ ; _ } -> assert false | Some { desc = Tunivar _ ; _ } -> ( ) | Some { desc = Tpoly ( t2 , _ ) ; _ } -> check_unboxed_gadt_arg loc univ env t2 | None -> ( )
let transl_declaration env sdecl id = reset_type_variables ( ) ; Ctype . begin_def ( ) ; let tparams = make_params env sdecl . ptype_params in let params = List . map ( fun ( cty , _ ) -> cty . ctyp_type ) tparams in let cstrs = List . map ( fun ( sty , sty ' , loc ) -> transl_simple_type env false sty , transl_simple_type env false sty ' , loc ) sdecl . ptype_cstrs in let raw_status = get_unboxed_from_attributes sdecl in if raw_status . unboxed && not raw_status . default then begin match sdecl . ptype_kind with | Ptype_abstract -> raise ( Error ( sdecl . ptype_loc , Bad_unboxed_attribute " it is abstract " ) ) | Ptype_variant [ { pcd_args = Pcstr_tuple [ ] ; _ } ] -> raise ( Error ( sdecl . ptype_loc , Bad_unboxed_attribute " its constructor has no argument " ) ) | Ptype_variant [ { pcd_args = Pcstr_tuple [ _ ] ; _ } ] -> ( ) | Ptype_variant [ { pcd_args = Pcstr_tuple _ ; _ } ] -> raise ( Error ( sdecl . ptype_loc , Bad_unboxed_attribute " its constructor has more than one argument " ) ) | Ptype_variant [ { pcd_args = Pcstr_record [ { pld_mutable = Immutable ; _ } ] ; _ } ] -> ( ) | Ptype_variant [ { pcd_args = Pcstr_record [ { pld_mutable = Mutable ; _ } ] ; _ } ] -> raise ( Error ( sdecl . ptype_loc , Bad_unboxed_attribute " it is mutable " ) ) | Ptype_variant [ { pcd_args = Pcstr_record _ ; _ } ] -> raise ( Error ( sdecl . ptype_loc , Bad_unboxed_attribute " its constructor has more than one argument " ) ) | Ptype_variant _ -> raise ( Error ( sdecl . ptype_loc , Bad_unboxed_attribute " it has more than one constructor " ) ) | Ptype_record [ { pld_mutable = Immutable ; _ } ] -> ( ) | Ptype_record [ { pld_mutable = Mutable ; _ } ] -> raise ( Error ( sdecl . ptype_loc , Bad_unboxed_attribute " it is mutable " ) ) | Ptype_record _ -> raise ( Error ( sdecl . ptype_loc , Bad_unboxed_attribute " it has more than one field " ) ) | Ptype_open -> raise ( Error ( sdecl . ptype_loc , Bad_unboxed_attribute " extensible variant types cannot be unboxed " ) ) end ; let unboxed_status = match sdecl . ptype_kind with | Ptype_variant [ { pcd_args = Pcstr_tuple [ _ ] ; _ } ] | Ptype_variant [ { pcd_args = Pcstr_record [ { pld_mutable = Immutable ; _ } ] ; _ } ] | Ptype_record [ { pld_mutable = Immutable ; _ } ] -> raw_status | _ -> unboxed_false_default_false in let unbox = unboxed_status . unboxed in let ( tkind , kind ) = match sdecl . ptype_kind with | Ptype_abstract -> Ttype_abstract , Type_abstract | Ptype_variant scstrs -> if List . exists ( fun cstr -> cstr . pcd_res <> None ) scstrs then begin match cstrs with [ ] -> ( ) | ( _ , _ , loc ) :: _ -> Location . prerr_warning loc Warnings . Constraint_on_gadt end ; let all_constrs = ref String . Set . empty in List . iter ( fun { pcd_name = { txt = name } } -> if String . Set . mem name ! all_constrs then raise ( Error ( sdecl . ptype_loc , Duplicate_constructor name ) ) ; all_constrs := String . Set . add name ! all_constrs ) scstrs ; if List . length ( List . filter ( fun cd -> cd . pcd_args <> Pcstr_tuple [ ] ) scstrs ) > ( Config . max_tag + 1 ) then raise ( Error ( sdecl . ptype_loc , Too_many_constructors ) ) ; let make_cstr scstr = let name = Ident . create_local scstr . pcd_name . txt in let targs , tret_type , args , ret_type , cstr_params = make_constructor env ( Path . Pident id ) params scstr . pcd_args scstr . pcd_res in if Config . flat_float_array && unbox then begin match Datarepr . constructor_existentials args ret_type with | _ , [ ] -> ( ) | [ argty ] , _ex -> check_unboxed_gadt_arg sdecl . ptype_loc cstr_params env argty | _ -> assert false end ; let tcstr = { cd_id = name ; cd_name = scstr . pcd_name ; cd_args = targs ; cd_res = tret_type ; cd_loc = scstr . pcd_loc ; cd_attributes = scstr . pcd_attributes } in let cstr = { Types . cd_id = name ; cd_args = args ; cd_res = ret_type ; cd_loc = scstr . pcd_loc ; cd_attributes = scstr . pcd_attributes } in tcstr , cstr in let make_cstr scstr = Builtin_attributes . warning_scope scstr . pcd_attributes ( fun ( ) -> make_cstr scstr ) in let tcstrs , cstrs = List . split ( List . map make_cstr scstrs ) in Ttype_variant tcstrs , Type_variant cstrs | Ptype_record lbls -> let lbls , lbls ' = transl_labels env true lbls in let rep = if unbox then Record_unboxed false else if List . for_all ( fun l -> is_float env l . Types . ld_type ) lbls ' then Record_float else Record_regular in Ttype_record lbls , Type_record ( lbls ' , rep ) | Ptype_open -> Ttype_open , Type_open in let ( tman , man ) = match sdecl . ptype_manifest with None -> None , None | Some sty -> let no_row = not ( is_fixed_type sdecl ) in let cty = transl_simple_type env no_row sty in Some cty , Some cty . ctyp_type in let decl = { type_params = params ; type_arity = List . length params ; type_kind = kind ; type_private = sdecl . ptype_private ; type_manifest = man ; type_variance = List . map ( fun _ -> Variance . full ) params ; type_is_newtype = false ; type_expansion_scope = Btype . lowest_level ; type_loc = sdecl . ptype_loc ; type_attributes = sdecl . ptype_attributes ; type_immediate = Unknown ; type_unboxed = unboxed_status ; } in List . iter ( fun ( cty , cty ' , loc ) -> let ty = cty . ctyp_type in let ty ' = cty ' . ctyp_type in try Ctype . unify env ty ty ' with Ctype . Unify tr -> raise ( Error ( loc , Inconsistent_constraint ( env , tr ) ) ) ) cstrs ; Ctype . end_def ( ) ; if is_fixed_type sdecl then begin let p , _ = try Env . find_type_by_name ( Longident . Lident ( Ident . name id ^ " # row " ) ) env with Not_found -> assert false in set_fixed_row env sdecl . ptype_loc p decl end ; begin match decl . type_manifest with None -> ( ) | Some ty -> if Ctype . cyclic_abbrev env id ty then raise ( Error ( sdecl . ptype_loc , Recursive_abbrev sdecl . ptype_name . txt ) ) ; end ; { typ_id = id ; typ_name = sdecl . ptype_name ; typ_params = tparams ; typ_type = decl ; typ_cstrs = cstrs ; typ_loc = sdecl . ptype_loc ; typ_manifest = tman ; typ_kind = tkind ; typ_private = sdecl . ptype_private ; typ_attributes = sdecl . ptype_attributes ; }
let generalize_decl decl = List . iter Ctype . generalize decl . type_params ; Btype . iter_type_expr_kind Ctype . generalize decl . type_kind ; begin match decl . type_manifest with | None -> ( ) | Some ty -> Ctype . generalize ty end
let rec check_constraints_rec env loc visited ty = let ty = Ctype . repr ty in if TypeSet . mem ty ! visited then ( ) else begin visited := TypeSet . add ty ! visited ; match ty . desc with | Tconstr ( path , args , _ ) -> let args ' = List . map ( fun _ -> Ctype . newvar ( ) ) args in let ty ' = Ctype . newconstr path args ' in begin try Ctype . enforce_constraints env ty ' with Ctype . Unify _ -> assert false | Not_found -> raise ( Error ( loc , Unavailable_type_constructor path ) ) end ; if not ( Ctype . matches env ty ty ' ) then raise ( Error ( loc , Constraint_failed ( ty , ty ' ) ) ) ; List . iter ( check_constraints_rec env loc visited ) args | Tpoly ( ty , tl ) -> let _ , ty = Ctype . instance_poly false tl ty in check_constraints_rec env loc visited ty | _ -> Btype . iter_type_expr ( check_constraints_rec env loc visited ) ty end
let check_constraints_labels env visited l pl = let rec get_loc name = function [ ] -> assert false | pld :: tl -> if name = pld . pld_name . txt then pld . pld_type . ptyp_loc else get_loc name tl in List . iter ( fun { Types . ld_id = name ; ld_type = ty } -> check_constraints_rec env ( get_loc ( Ident . name name ) pl ) visited ty ) l
let check_constraints env sdecl ( _ , decl ) = let visited = ref TypeSet . empty in begin match decl . type_kind with | Type_abstract -> ( ) | Type_variant l -> let find_pl = function Ptype_variant pl -> pl | Ptype_record _ | Ptype_abstract | Ptype_open -> assert false in let pl = find_pl sdecl . ptype_kind in let pl_index = let foldf acc x = String . Map . add x . pcd_name . txt x acc in List . fold_left foldf String . Map . empty pl in List . iter ( fun { Types . cd_id = name ; cd_args ; cd_res } -> let { pcd_args ; pcd_res ; _ } = try String . Map . find ( Ident . name name ) pl_index with Not_found -> assert false in begin match cd_args , pcd_args with | Cstr_tuple tyl , Pcstr_tuple styl -> List . iter2 ( fun sty ty -> check_constraints_rec env sty . ptyp_loc visited ty ) styl tyl | Cstr_record tyl , Pcstr_record styl -> check_constraints_labels env visited tyl styl | _ -> assert false end ; match pcd_res , cd_res with | Some sr , Some r -> check_constraints_rec env sr . ptyp_loc visited r | _ -> ( ) ) l | Type_record ( l , _ ) -> let find_pl = function Ptype_record pl -> pl | Ptype_variant _ | Ptype_abstract | Ptype_open -> assert false in let pl = find_pl sdecl . ptype_kind in check_constraints_labels env visited l pl | Type_open -> ( ) end ; begin match decl . type_manifest with | None -> ( ) | Some ty -> let sty = match sdecl . ptype_manifest with Some sty -> sty | _ -> assert false in check_constraints_rec env sty . ptyp_loc visited ty end
let check_coherence env loc dpath decl = match decl with { type_kind = ( Type_variant _ | Type_record _ | Type_open ) ; type_manifest = Some ty } -> begin match ( Ctype . repr ty ) . desc with Tconstr ( path , args , _ ) -> begin try let decl ' = Env . find_type path env in let err = if List . length args <> List . length decl . type_params then Some Includecore . Arity else if not ( Ctype . equal env false args decl . type_params ) then Some Includecore . Constraint else Includecore . type_declarations ~ loc ~ equality : true env ~ mark : true ( Path . last path ) decl ' dpath ( Subst . type_declaration ( Subst . add_type_path dpath path Subst . identity ) decl ) in if err <> None then raise ( Error ( loc , Definition_mismatch ( ty , err ) ) ) with Not_found -> raise ( Error ( loc , Unavailable_type_constructor path ) ) end | _ -> raise ( Error ( loc , Definition_mismatch ( ty , None ) ) ) end | _ -> ( )