From 6fd40efb6cf5a337b7aab569bdf1373f8cd1828d Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 26 Sep 2024 19:03:01 -0700 Subject: [PATCH 1/3] Permit inferring Self for unannotated self --- docs/spec/annotations.rst | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/docs/spec/annotations.rst b/docs/spec/annotations.rst index ce675a1f..07b9c284 100644 --- a/docs/spec/annotations.rst +++ b/docs/spec/annotations.rst @@ -27,14 +27,10 @@ general type possible, or ignored, by any type checker. It is recommended but not required that checked functions have annotations for all arguments and the return type. For a checked function, the default annotation for arguments and for the return type -is ``Any``. An exception is the first argument of instance and -class methods. If it is not annotated, then it is assumed to have the -type of the containing class for instance methods, and a type object -type corresponding to the containing class object for class methods. -For example, in class ``A`` the first argument of an instance method -has the implicit type ``A``. In a class method, the precise type of -the first argument cannot be represented using the available type -notation. +is ``Any``. An exception to the above is the first argument of +instance and class methods (conventionally named ``self`` or ``cls``), +which type checkers should assume to have an appropriate type, as per +:ref:`annotating-methods`. (Note that the return type of ``__init__`` ought to be annotated with ``-> None``. The reason for this is subtle. If ``__init__`` assumed @@ -354,9 +350,16 @@ Annotating instance and class methods ------------------------------------- In most cases the first argument of class and instance methods -does not need to be annotated, and it is assumed to have the -type of the containing class for instance methods, and a type object -type corresponding to the containing class object for class methods. +(conventionally named ``self`` or ``cls``) does not need to be annotated. + +If the argument is not annotated, then for instance methods it is +assumed to have the type of the containing class or :ref:`Self +`, and for class methods the type object type corresponding to +the containing class object or ``type[Self]``. For example, in class +``A`` the first argument of an instance method has the implicit type +``A``. In a class method, the precise type of the first argument +cannot be represented using the available type notation. + In addition, the first argument in an instance method can be annotated with a type variable. In this case the return type may use the same type variable, thus making that method a generic function. For example:: From 8719e63731b4a04588fb91ac930ef08f077238d2 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 26 Sep 2024 20:20:53 -0700 Subject: [PATCH 2/3] nit --- docs/spec/annotations.rst | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/spec/annotations.rst b/docs/spec/annotations.rst index 07b9c284..5d5bd796 100644 --- a/docs/spec/annotations.rst +++ b/docs/spec/annotations.rst @@ -349,16 +349,15 @@ types cannot be specified:: Annotating instance and class methods ------------------------------------- -In most cases the first argument of class and instance methods +In most cases the first argument of instance and class methods (conventionally named ``self`` or ``cls``) does not need to be annotated. If the argument is not annotated, then for instance methods it is assumed to have the type of the containing class or :ref:`Self `, and for class methods the type object type corresponding to -the containing class object or ``type[Self]``. For example, in class -``A`` the first argument of an instance method has the implicit type -``A``. In a class method, the precise type of the first argument -cannot be represented using the available type notation. +the containing class object or ``type[Self]``. In a class method, the +precise type of the first argument cannot be represented using the +available type notation. In addition, the first argument in an instance method can be annotated with a type variable. In this case the return type may use the same From 46a3046d8fcbe53b066dfff5c0dcb1dd0b091936 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 26 Sep 2024 22:45:48 -0700 Subject: [PATCH 3/3] remove sentence --- docs/spec/annotations.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/spec/annotations.rst b/docs/spec/annotations.rst index 5d5bd796..d5703726 100644 --- a/docs/spec/annotations.rst +++ b/docs/spec/annotations.rst @@ -355,9 +355,7 @@ In most cases the first argument of instance and class methods If the argument is not annotated, then for instance methods it is assumed to have the type of the containing class or :ref:`Self `, and for class methods the type object type corresponding to -the containing class object or ``type[Self]``. In a class method, the -precise type of the first argument cannot be represented using the -available type notation. +the containing class object or ``type[Self]``. In addition, the first argument in an instance method can be annotated with a type variable. In this case the return type may use the same