Class SelfType

java.lang.Object
org.sonar.plugins.python.api.types.v2.SelfType
All Implemented Interfaces:
PythonType

@Beta public final class SelfType extends Object implements PythonType
Represents typing.Self or typing_extensions.Self type as defined in PEP 673.

The Self type is used to indicate that a method returns an instance of the same type as the enclosing class. This is particularly useful in methods that return instances of the class, such as constructors or builder methods.

Important invariants:

  • A SelfType must NOT wrap an ObjectType - instead the ObjectType should wrap the SelfType
  • A SelfType must NOT wrap a UnionType - instead the UnionType should wrap the SelfType

Use the static factory method of(PythonType) to create instances, which ensures these invariants are maintained.

See Also:
  • Method Details

    • of

      public static PythonType of(@Nullable PythonType type)
      Creates a SelfType wrapping the given type.

      This factory method ensures the following invariants:

      • If the type is null, UNKNOWN, or not a supported type, it returns UNKNOWN
      • If the type is already a SelfType, it returns it unchanged
      • The type ObjectType[ClassType[A]] is converted to ObjectType[SelfType[ClassType[A]]]
      • The type ObjectType[UnionType[ClassType[A], ClassType[B]]] is converted to ObjectType[UnionType[SelfType[ClassType[A]], SelfType[ClassType[B]]]]
      • The type ClassType[A] is converted to SelfType[ClassType[A]]
      Parameters:
      type - the type to wrap in a SelfType (can be null, in which case UNKNOWN is returned)
      Returns:
      a PythonType representing Self[type], with proper structure maintained, or UNKNOWN if the type is not supported
    • fromTypeWrapper

      public static PythonType fromTypeWrapper(TypeWrapper typeWrapper)
    • innerType

      public PythonType innerType()
    • typeWrapper

      public TypeWrapper typeWrapper()
    • name

      public String name()
      Specified by:
      name in interface PythonType
    • displayName

      public Optional<String> displayName()
      Specified by:
      displayName in interface PythonType
    • instanceDisplayName

      public Optional<String> instanceDisplayName()
      Specified by:
      instanceDisplayName in interface PythonType
    • isCompatibleWith

      public boolean isCompatibleWith(PythonType another)
      Specified by:
      isCompatibleWith in interface PythonType
    • key

      public String key()
      Specified by:
      key in interface PythonType
    • resolveMember

      public Optional<PythonType> resolveMember(String memberName)
      Description copied from interface: PythonType
      Attempts to resolve a PythonType's member It should return Optional.empty() when the member doesn't exist, and PythonType.UNKNOWN when the member may exist but can't be resolved
      Specified by:
      resolveMember in interface PythonType
    • hasMember

      public TriBool hasMember(String memberName)
      Specified by:
      hasMember in interface PythonType
    • definitionLocation

      public Optional<LocationInFile> definitionLocation()
      Specified by:
      definitionLocation in interface PythonType
    • unwrappedType

      public PythonType unwrappedType()
      Specified by:
      unwrappedType in interface PythonType
    • typeSource

      public TypeSource typeSource()
      Specified by:
      typeSource in interface PythonType
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object