@@ -509,6 +509,8 @@ enum AttributeBase {
509509 /// Properties are handled via a special case so that we can understand
510510 /// setter decorators.
511511 Property ( Type ) ,
512+ /// Attribute access on `Self` from inside a class
513+ SelfType ( ClassType ) ,
512514 /// Result of a super() call. See Type::SuperInstance for details on what these fields are.
513515 SuperInstance ( ClassType , SuperObj ) ,
514516 /// Typed dictionaries have similar properties to dict and Mapping, with some exceptions
@@ -524,6 +526,7 @@ pub enum ClassBase {
524526 ClassDef ( Class ) ,
525527 ClassType ( ClassType ) ,
526528 Quantified ( Quantified , ClassType ) ,
529+ SelfType ( ClassType ) ,
527530}
528531
529532impl ClassBase {
@@ -532,14 +535,16 @@ impl ClassBase {
532535 ClassBase :: ClassDef ( c) => c,
533536 ClassBase :: ClassType ( c) => c. class_object ( ) ,
534537 ClassBase :: Quantified ( _, c) => c. class_object ( ) ,
538+ ClassBase :: SelfType ( c) => c. class_object ( ) ,
535539 }
536540 }
537541
538542 pub fn targs ( & self ) -> Option < & TArgs > {
539543 match self {
540544 ClassBase :: ClassDef ( ..) => None ,
541- ClassBase :: ClassType ( c) => Some ( c. targs ( ) ) ,
542- ClassBase :: Quantified ( _, c) => Some ( c. targs ( ) ) ,
545+ ClassBase :: ClassType ( c) | ClassBase :: Quantified ( _, c) | ClassBase :: SelfType ( c) => {
546+ Some ( c. targs ( ) )
547+ }
543548 }
544549 }
545550
@@ -548,6 +553,7 @@ impl ClassBase {
548553 ClassBase :: ClassDef ( c) => Type :: ClassDef ( c) ,
549554 ClassBase :: ClassType ( c) => Type :: Type ( Box :: new ( c. to_type ( ) ) ) ,
550555 ClassBase :: Quantified ( q, _) => Type :: type_form ( q. to_type ( ) ) ,
556+ ClassBase :: SelfType ( c) => Type :: type_form ( Type :: SelfType ( c) ) ,
551557 }
552558 }
553559}
@@ -1550,6 +1556,13 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
15501556 ) ) ,
15511557 }
15521558 }
1559+ AttributeBase :: SelfType ( cls) => match self . get_self_attribute ( & cls, attr_name) {
1560+ Some ( attr) => LookupResult :: found ( attr) ,
1561+ None => LookupResult :: not_found ( NotFoundOn :: ClassInstance (
1562+ cls. class_object ( ) . dupe ( ) ,
1563+ base_copy,
1564+ ) ) ,
1565+ } ,
15531566 }
15541567 }
15551568
@@ -1579,6 +1592,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
15791592 }
15801593 }
15811594 AttributeBase :: ClassInstance ( cls)
1595+ | AttributeBase :: SelfType ( cls)
15821596 | AttributeBase :: EnumLiteral ( LitEnum { class : cls, .. } )
15831597 | AttributeBase :: Quantified ( _, Some ( cls) )
15841598 | AttributeBase :: SuperInstance ( cls, _)
@@ -1755,10 +1769,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
17551769 match ty {
17561770 Type :: ClassType ( class_type) => Some ( AttributeBase :: ClassInstance ( class_type) ) ,
17571771 Type :: ClassDef ( cls) => Some ( AttributeBase :: ClassObject ( ClassBase :: ClassDef ( cls) ) ) ,
1758- Type :: SelfType ( class_type) => Some ( AttributeBase :: ClassInstance ( class_type) ) ,
1759- Type :: Type ( box Type :: SelfType ( class_type) ) => Some ( AttributeBase :: ClassObject (
1760- ClassBase :: ClassType ( class_type. clone ( ) ) ,
1761- ) ) ,
1772+ Type :: SelfType ( class_type) => Some ( AttributeBase :: SelfType ( class_type) ) ,
1773+ Type :: Type ( box Type :: SelfType ( class_type) ) => {
1774+ Some ( AttributeBase :: ClassObject ( ClassBase :: SelfType ( class_type) ) )
1775+ }
17621776 Type :: TypedDict ( td) | Type :: PartialTypedDict ( td) => {
17631777 Some ( AttributeBase :: TypedDict ( td. clone ( ) ) )
17641778 }
@@ -2210,6 +2224,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
22102224 ) {
22112225 match & base {
22122226 AttributeBase :: ClassInstance ( class)
2227+ | AttributeBase :: SelfType ( class)
22132228 | AttributeBase :: EnumLiteral ( LitEnum { class, .. } )
22142229 | AttributeBase :: Quantified ( _, Some ( class) ) => {
22152230 self . completions_class_type ( class, expected_attribute_name, res)
0 commit comments