@@ -163,6 +163,7 @@ impl<'db> Type<'db> {
163163 db,
164164 CallableSignature :: from_overloads ( signatures) ,
165165 callable. kind ( db) ,
166+ callable. provenance ( db) ,
166167 )
167168 } ) )
168169 }
@@ -183,6 +184,7 @@ impl<'db> Type<'db> {
183184 db,
184185 CallableSignature :: from_overloads ( signatures) ,
185186 callable. kind ( db) ,
187+ callable. provenance ( db) ,
186188 ) ) ;
187189 }
188190 }
@@ -230,13 +232,15 @@ impl<'db> Type<'db> {
230232 db,
231233 CallableSignature :: from_overloads ( method. signatures ( db) ) ,
232234 CallableTypeKind :: Regular ,
235+ CallableFunctionProvenance :: None ,
233236 ) ) ) ,
234237
235238 Type :: WrapperDescriptor ( wrapper_descriptor) => {
236239 Some ( CallableTypes :: one ( CallableType :: new (
237240 db,
238241 CallableSignature :: from_overloads ( wrapper_descriptor. signatures ( db) ) ,
239242 CallableTypeKind :: Regular ,
243+ CallableFunctionProvenance :: None ,
240244 ) ) )
241245 }
242246
@@ -324,6 +328,33 @@ pub enum CallableTypeKind {
324328 ParamSpecValue ,
325329}
326330
331+ /// Source-function provenance retained by a callable signature.
332+ ///
333+ /// A [`CallableType`] can describe a bare callable shape, such as one from `Callable[...]`. For
334+ /// function-like sources, such as a [`FunctionType`] upcast to a [`CallableType`] or a lambda, this
335+ /// records whether the source function has an explicit return annotation.
336+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash , get_size2:: GetSize ) ]
337+ pub enum CallableFunctionProvenance {
338+ /// The callable does not retain source-function provenance.
339+ None ,
340+
341+ /// The callable came from a function without an explicit return annotation.
342+ ImplicitReturn ,
343+
344+ /// The callable came from a function with an explicit return annotation.
345+ ExplicitReturn ,
346+ }
347+
348+ impl CallableFunctionProvenance {
349+ pub ( crate ) fn from_function_return_annotation ( has_explicit_return_annotation : bool ) -> Self {
350+ if has_explicit_return_annotation {
351+ Self :: ExplicitReturn
352+ } else {
353+ Self :: ImplicitReturn
354+ }
355+ }
356+ }
357+
327358/// A "policy" enum that describes how `type[]` types should be upcast
328359/// to `Callable` types.
329360///
@@ -380,6 +411,20 @@ pub struct CallableType<'db> {
380411 pub ( crate ) signatures : CallableSignature < ' db > ,
381412
382413 pub ( super ) kind : CallableTypeKind ,
414+
415+ /// Source-function return-annotation provenance retained by this callable.
416+ ///
417+ /// Function-like values can retain their source-function provenance when converted to a
418+ /// callable signature:
419+ /// ```python
420+ /// def decorator(cls) -> object: ...
421+ /// ```
422+ ///
423+ /// Callables that are only known from a callable shape do not retain that provenance:
424+ /// ```python
425+ /// def decorator_factory() -> Callable[[type[object]], object]: ...
426+ /// ```
427+ pub ( crate ) provenance : CallableFunctionProvenance ,
383428}
384429
385430pub ( super ) fn walk_callable_type < ' db , V : visitor:: TypeVisitor < ' db > + ?Sized > (
@@ -401,6 +446,7 @@ impl<'db> CallableType<'db> {
401446 db,
402447 CallableSignature :: single ( signature) ,
403448 CallableTypeKind :: Regular ,
449+ CallableFunctionProvenance :: None ,
404450 )
405451 }
406452
@@ -409,6 +455,7 @@ impl<'db> CallableType<'db> {
409455 db,
410456 CallableSignature :: single ( signature) ,
411457 CallableTypeKind :: FunctionLike ,
458+ CallableFunctionProvenance :: None ,
412459 )
413460 }
414461
@@ -420,6 +467,7 @@ impl<'db> CallableType<'db> {
420467 db,
421468 CallableSignature :: single ( Signature :: new ( parameters, Type :: unknown ( ) ) ) ,
422469 CallableTypeKind :: ParamSpecValue ,
470+ CallableFunctionProvenance :: None ,
423471 )
424472 }
425473
@@ -441,7 +489,12 @@ impl<'db> CallableType<'db> {
441489 }
442490
443491 pub ( crate ) fn into_regular ( self , db : & ' db dyn Db ) -> CallableType < ' db > {
444- CallableType :: new ( db, self . signatures ( db) , CallableTypeKind :: Regular )
492+ CallableType :: new (
493+ db,
494+ self . signatures ( db) ,
495+ CallableTypeKind :: Regular ,
496+ self . provenance ( db) ,
497+ )
445498 }
446499
447500 /// Returns the reduced callable produced by partially applying selected overloads.
@@ -453,6 +506,7 @@ impl<'db> CallableType<'db> {
453506 db,
454507 CallableSignature :: partially_apply ( db, overloads) ?,
455508 CallableTypeKind :: Regular ,
509+ CallableFunctionProvenance :: None ,
456510 ) )
457511 }
458512
@@ -482,6 +536,7 @@ impl<'db> CallableType<'db> {
482536 db,
483537 self . signatures ( db) . bind_self ( db, self_type) ,
484538 self . kind ( db) ,
539+ self . provenance ( db) ,
485540 )
486541 }
487542
@@ -490,6 +545,7 @@ impl<'db> CallableType<'db> {
490545 db,
491546 self . signatures ( db) . apply_self ( db, self_type) ,
492547 self . kind ( db) ,
548+ self . provenance ( db) ,
493549 )
494550 }
495551
@@ -498,7 +554,12 @@ impl<'db> CallableType<'db> {
498554 /// Specifically, this represents a callable type with a single signature:
499555 /// `(*args: object, **kwargs: object) -> Never`.
500556 pub ( crate ) fn bottom ( db : & ' db dyn Db ) -> CallableType < ' db > {
501- Self :: new ( db, CallableSignature :: bottom ( ) , CallableTypeKind :: Regular )
557+ Self :: new (
558+ db,
559+ CallableSignature :: bottom ( ) ,
560+ CallableTypeKind :: Regular ,
561+ CallableFunctionProvenance :: None ,
562+ )
502563 }
503564
504565 pub ( super ) fn recursive_type_normalized_impl (
@@ -512,6 +573,7 @@ impl<'db> CallableType<'db> {
512573 self . signatures ( db)
513574 . recursive_type_normalized_impl ( db, div, nested) ?,
514575 self . kind ( db) ,
576+ self . provenance ( db) ,
515577 ) )
516578 }
517579
@@ -531,6 +593,7 @@ impl<'db> CallableType<'db> {
531593 self . signatures ( db)
532594 . apply_type_mapping_impl ( db, type_mapping, tcx, visitor) ,
533595 self . kind ( db) ,
596+ self . provenance ( db) ,
534597 )
535598 }
536599
@@ -625,6 +688,7 @@ impl<'db> CallableTypes<'db> {
625688 db,
626689 CallableSignature :: from_overloads ( overloads) ,
627690 CallableTypeKind :: Regular ,
691+ CallableFunctionProvenance :: None ,
628692 )
629693 . into_precise_functools_partial_instance ( db, wrapped)
630694 }
0 commit comments