From 16181e58a6ea3dfb2808487a36c58aff947782a0 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Tue, 16 Nov 2021 18:00:38 -0800 Subject: [PATCH 1/2] PEP 613: Loosen class scope restriction of explicit TypeAlias Per discussion on typing-sig https://mail.python.org/archives/list/typing-sig@python.org/thread/CGOO7GPPECGMLFDUDXSSXTRADI4BXYCS/ --- pep-0613.rst | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/pep-0613.rst b/pep-0613.rst index f11c5625921..b96b4467030 100644 --- a/pep-0613.rst +++ b/pep-0613.rst @@ -95,6 +95,17 @@ across the codebase can be suppressed. Scope Restrictions: ******************* +:: + + class Foo: + x = ClassName + y: TypeAlias = ClassName + z: Type[ClassName] = ClassName + +Type aliases are valid within class scope, both implicitly (``x``) and +explicitly (``y``). If the line should be interpreted as a class +variable, it must be explicitly annotated as (``z``) + :: x = ClassName @@ -103,7 +114,7 @@ Scope Restrictions: The outer ``x`` is a valid type alias, but type checkers must error if the inner ``x`` is ever used as a type because type aliases cannot be defined -inside a nested scope. +inside of a function. This is confusing because the alias declaration rule is not explicit, and because a type error will not be thrown on the location of the inner type alias declaration but rather on every one of its subsequent use cases. @@ -116,10 +127,10 @@ but rather on every one of its subsequent use cases. def bar() -> None: x: TypeAlias = ClassName -With explicit aliases, the outer assignment is still a valid type variable, -and the inner assignment can either be a valid local variable or a clear error, -communicating to the author that type aliases cannot be defined inside a nested -scope. +With explicit aliases, the outer assignment is still a valid type variable. +Inside ``foo``, the inner assignment should be interpreted as ``x: Type[ClassName]``. +Inside ``bar``, the the type checker should a clear error, communicating to the +author that type aliases cannot be defined inside a function. Specification @@ -200,6 +211,14 @@ appealing because it still sticks with the ``MyType = int`` assignment syntax, and adds some information for the type checker purely as an annotation. +Version History +=============== + +* 2021-11-16 + + * Allow TypeAlias inside class scope + + Copyright ========= From 1f7ca331fe97fb82e755f66036273ca819b67413 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Wed, 12 Jan 2022 13:17:26 -0800 Subject: [PATCH 2/2] Apply CAM-Gerlach's suggestions from code review Co-authored-by: CAM Gerlach --- pep-0613.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pep-0613.rst b/pep-0613.rst index b96b4467030..5367ac1cc74 100644 --- a/pep-0613.rst +++ b/pep-0613.rst @@ -104,7 +104,7 @@ Scope Restrictions: Type aliases are valid within class scope, both implicitly (``x``) and explicitly (``y``). If the line should be interpreted as a class -variable, it must be explicitly annotated as (``z``) +variable, it must be explicitly annotated (``z``). :: @@ -129,8 +129,8 @@ but rather on every one of its subsequent use cases. With explicit aliases, the outer assignment is still a valid type variable. Inside ``foo``, the inner assignment should be interpreted as ``x: Type[ClassName]``. -Inside ``bar``, the the type checker should a clear error, communicating to the -author that type aliases cannot be defined inside a function. +Inside ``bar``, the type checker should raise a clear error, communicating +to the author that type aliases cannot be defined inside a function. Specification