@@ -11,6 +11,7 @@ use crate::{
1111 LiteralValueTypeKind , MemberLookupPolicy , Parameter , Parameters , Signature ,
1212 SubclassOfInner , Type , TypeContext , TypeMapping , TypeVarBoundOrConstraints , UnionType ,
1313 constraints:: { ConstraintSet , IteratorConstraintsExtension } ,
14+ function:: OverloadLiteral ,
1415 known_instance:: FunctoolsPartialInstance ,
1516 relation:: { TypeRelation , TypeRelationChecker } ,
1617 signatures:: { CallableSignature , PartialSignatureApplication } ,
@@ -186,10 +187,9 @@ impl<'db> Type<'db> {
186187 . signatures ( db)
187188 . into_iter ( )
188189 . map ( |sig| sig. clone ( ) . with_return_type ( Type :: TypeVar ( tvar) ) ) ;
189- CallableType :: new (
190+ callable . with_signatures (
190191 db,
191192 CallableSignature :: from_overloads ( signatures) ,
192- callable. kind ( db) ,
193193 )
194194 } ) )
195195 }
@@ -206,10 +206,9 @@ impl<'db> Type<'db> {
206206 callable. signatures ( db) . into_iter ( ) . map ( |sig| {
207207 sig. clone ( ) . with_return_type ( Type :: TypeVar ( tvar) )
208208 } ) ;
209- callables. push ( CallableType :: new (
209+ callables. push ( callable . with_signatures (
210210 db,
211211 CallableSignature :: from_overloads ( signatures) ,
212- callable. kind ( db) ,
213212 ) ) ;
214213 }
215214 }
@@ -400,13 +399,18 @@ impl From<TypeRelation> for UpcastPolicy {
400399/// It can be written in type expressions using `typing.Callable`. `lambda` expressions are
401400/// inferred directly as `CallableType`s; all function-literal types are subtypes of a
402401/// `CallableType`.
403- #[ salsa:: interned( debug, heap_size=ruff_memory_usage:: heap_size) ]
402+ #[ salsa:: interned( debug, constructor=new_internal , heap_size=ruff_memory_usage:: heap_size) ]
404403pub struct CallableType < ' db > {
405404 #[ returns( ref) ]
406405 pub ( crate ) signatures : CallableSignature < ' db > ,
407406
408407 #[ returns( copy) ]
409408 pub ( super ) kind : CallableTypeKind ,
409+
410+ /// The declaration on which `@deprecated` wrapped this callable. Retain the declaration
411+ /// for diagnostic names, source annotations, and deduplication, independently of binding kind.
412+ #[ returns( copy) ]
413+ pub ( crate ) deprecated : Option < OverloadLiteral < ' db > > ,
410414}
411415
412416pub ( super ) fn walk_callable_type < ' db , V : visitor:: TypeVisitor < ' db > + ?Sized > (
@@ -423,6 +427,31 @@ pub(super) fn walk_callable_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
423427impl get_size2:: GetSize for CallableType < ' _ > { }
424428
425429impl < ' db > CallableType < ' db > {
430+ pub ( crate ) fn new < S > ( db : & ' db dyn Db , signatures : S , kind : CallableTypeKind ) -> Self
431+ where
432+ S : salsa:: Lookup < CallableSignature < ' db > > + std:: hash:: Hash ,
433+ CallableSignature < ' db > : salsa:: HashEqLike < S > ,
434+ {
435+ Self :: new_internal ( db, signatures, kind, None )
436+ }
437+
438+ pub ( crate ) fn with_deprecated ( self , db : & ' db dyn Db , deprecated : OverloadLiteral < ' db > ) -> Self {
439+ Self :: new_internal ( db, self . signatures ( db) , self . kind ( db) , Some ( deprecated) )
440+ }
441+
442+ /// Replace the signatures without losing binding behavior or deprecation metadata.
443+ pub ( crate ) fn with_signatures < S > ( self , db : & ' db dyn Db , signatures : S ) -> Self
444+ where
445+ S : salsa:: Lookup < CallableSignature < ' db > > + std:: hash:: Hash ,
446+ CallableSignature < ' db > : salsa:: HashEqLike < S > ,
447+ {
448+ Self :: new_internal ( db, signatures, self . kind ( db) , self . deprecated ( db) )
449+ }
450+
451+ pub ( crate ) fn with_kind ( self , db : & ' db dyn Db , kind : CallableTypeKind ) -> Self {
452+ Self :: new_internal ( db, self . signatures ( db) , kind, self . deprecated ( db) )
453+ }
454+
426455 pub ( crate ) fn single ( db : & ' db dyn Db , signature : Signature < ' db > ) -> CallableType < ' db > {
427456 CallableType :: new (
428457 db,
@@ -483,7 +512,7 @@ impl<'db> CallableType<'db> {
483512 }
484513
485514 pub ( crate ) fn into_regular ( self , db : & ' db dyn Db ) -> CallableType < ' db > {
486- CallableType :: new ( db , self . signatures ( db) , CallableTypeKind :: Regular )
515+ self . with_kind ( db, CallableTypeKind :: Regular )
487516 }
488517
489518 /// Retain every parameter signature and its generic context, but erase return types
@@ -545,19 +574,15 @@ impl<'db> CallableType<'db> {
545574 return self . into_regular ( db) ;
546575 }
547576
548- CallableType :: new (
549- db,
550- self . signatures ( db) . bind_self ( db, env, self_type) ,
551- self . kind ( db) ,
552- )
577+ self . with_signatures ( db, self . signatures ( db) . bind_self ( db, env, self_type) )
553578 }
554579
555580 pub ( crate ) fn into_function_like ( self , db : & ' db dyn Db ) -> CallableType < ' db > {
556- CallableType :: new ( db , self . signatures ( db) , CallableTypeKind :: FunctionLike )
581+ self . with_kind ( db, CallableTypeKind :: FunctionLike )
557582 }
558583
559584 pub ( crate ) fn into_dunder_paramspec ( self , db : & ' db dyn Db ) -> CallableType < ' db > {
560- CallableType :: new ( db , self . signatures ( db) , CallableTypeKind :: DunderParamSpec )
585+ self . with_kind ( db, CallableTypeKind :: DunderParamSpec )
561586 }
562587
563588 pub ( crate ) fn apply_self (
@@ -576,11 +601,10 @@ impl<'db> CallableType<'db> {
576601 receiver_type : Type < ' db > ,
577602 self_type : Type < ' db > ,
578603 ) -> CallableType < ' db > {
579- CallableType :: new (
604+ self . with_signatures (
580605 db,
581606 self . signatures ( db)
582607 . apply_self_with_receiver ( db, env, receiver_type, self_type) ,
583- self . kind ( db) ,
584608 )
585609 }
586610
@@ -599,12 +623,13 @@ impl<'db> CallableType<'db> {
599623 div : Type < ' db > ,
600624 nested : bool ,
601625 ) -> Option < Self > {
602- Some ( CallableType :: new (
603- db,
604- self . signatures ( db)
605- . recursive_type_normalized_impl ( db, env, div, nested) ?,
606- self . kind ( db) ,
607- ) )
626+ Some (
627+ self . with_signatures (
628+ db,
629+ self . signatures ( db)
630+ . recursive_type_normalized_impl ( db, env, div, nested) ?,
631+ ) ,
632+ )
608633 }
609634
610635 pub ( super ) fn apply_type_mapping_impl < ' a > (
@@ -618,11 +643,10 @@ impl<'db> CallableType<'db> {
618643 return replacements. get ( & self ) . copied ( ) . unwrap_or ( self ) ;
619644 }
620645
621- CallableType :: new (
646+ self . with_signatures (
622647 db,
623648 self . signatures ( db)
624649 . apply_type_mapping_impl ( db, type_mapping, tcx, visitor) ,
625- self . kind ( db) ,
626650 )
627651 }
628652
0 commit comments