From ca4ec2d5b2cb338516376e251b34550d294adfdd Mon Sep 17 00:00:00 2001 From: Kir Chou Date: Tue, 19 Dec 2023 21:02:13 +0900 Subject: [PATCH 1/7] Append more description to reveal_type() function. --- Doc/library/typing.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index ba2845eb17ddcc..63ea15298689fe 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2631,6 +2631,13 @@ Functions and decorators x = reveal_type(1) # prints "Runtime type is int" print(x) # prints "1" + Note that the runtime type inferred by ``reveal_type()`` can be different + from other static type checkers. Static type checkers in some cases will + know more (e.g. generic types, like the element types of a list) and in some + cases will know less (e.g. a runtime value will never be a union, it will + always be one concrete type or the other, but static type systems use union + types to represent that at runtime the value may be any of several types.) + .. versionadded:: 3.11 .. decorator:: dataclass_transform(*, eq_default=True, order_default=False, \ From 7c0f983543256a741885bd749a539a2d70643912 Mon Sep 17 00:00:00 2001 From: Kir Chou Date: Tue, 19 Dec 2023 21:41:59 +0900 Subject: [PATCH 2/7] Minor update for the first sentence. --- Doc/library/typing.rst | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 63ea15298689fe..459337ced6bea6 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2631,12 +2631,13 @@ Functions and decorators x = reveal_type(1) # prints "Runtime type is int" print(x) # prints "1" - Note that the runtime type inferred by ``reveal_type()`` can be different - from other static type checkers. Static type checkers in some cases will - know more (e.g. generic types, like the element types of a list) and in some - cases will know less (e.g. a runtime value will never be a union, it will - always be one concrete type or the other, but static type systems use union - types to represent that at runtime the value may be any of several types.) + Note that ``reveal_type()`` at runtime doesn't infer a type in the same way + that static type checkers do; it returns the actual type at runtime. Static + type checkers in some cases will know more (e.g. generic types, like the + element types of a list) and in some cases will know less (e.g. a runtime + value will never be a union, it will always be one concrete type or the + other, but static type systems use union types to represent that at runtime + the value may be any of several types.) .. versionadded:: 3.11 From 51c4973c208a22c3eb9b655876cd5a980778414c Mon Sep 17 00:00:00 2001 From: Kir Chou Date: Tue, 19 Dec 2023 21:41:59 +0900 Subject: [PATCH 3/7] Minor update for the first sentence. --- Doc/library/typing.rst | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 63ea15298689fe..459337ced6bea6 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2631,12 +2631,13 @@ Functions and decorators x = reveal_type(1) # prints "Runtime type is int" print(x) # prints "1" - Note that the runtime type inferred by ``reveal_type()`` can be different - from other static type checkers. Static type checkers in some cases will - know more (e.g. generic types, like the element types of a list) and in some - cases will know less (e.g. a runtime value will never be a union, it will - always be one concrete type or the other, but static type systems use union - types to represent that at runtime the value may be any of several types.) + Note that ``reveal_type()`` at runtime doesn't infer a type in the same way + that static type checkers do; it returns the actual type at runtime. Static + type checkers in some cases will know more (e.g. generic types, like the + element types of a list) and in some cases will know less (e.g. a runtime + value will never be a union, it will always be one concrete type or the + other, but static type systems use union types to represent that at runtime + the value may be any of several types.) .. versionadded:: 3.11 From 5ba02e8cd238109e2915d0f79a2695a98fb2b4f8 Mon Sep 17 00:00:00 2001 From: Kir Chou Date: Wed, 20 Dec 2023 20:57:12 +0900 Subject: [PATCH 4/7] Address the feedbacks, including changing the first two sentences, and shortening the added description. --- Doc/library/typing.rst | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 459337ced6bea6..4299c8aa72d14e 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2604,10 +2604,11 @@ Functions and decorators .. function:: reveal_type(obj, /) - Reveal the inferred static type of an expression. + Ask a static type checker to reveal the statically inferred type of an + expression. - When a static type checker encounters a call to this function, - it emits a diagnostic with the type of the argument. For example:: + When a static type checker encounters a call to this function, it emits a + diagnostic with the inferred static type of the argument. For example:: x: int = 1 reveal_type(x) # Revealed type is "builtins.int" @@ -2631,13 +2632,8 @@ Functions and decorators x = reveal_type(1) # prints "Runtime type is int" print(x) # prints "1" - Note that ``reveal_type()`` at runtime doesn't infer a type in the same way - that static type checkers do; it returns the actual type at runtime. Static - type checkers in some cases will know more (e.g. generic types, like the - element types of a list) and in some cases will know less (e.g. a runtime - value will never be a union, it will always be one concrete type or the - other, but static type systems use union types to represent that at runtime - the value may be any of several types.) + Note that the runtime type may be different from (more or less specific + than) the type statically inferred by a type checker. .. versionadded:: 3.11 From 4220e79fe5ad180b98a84f08291ed5d00d3c5f8e Mon Sep 17 00:00:00 2001 From: Kir Chou Date: Wed, 20 Dec 2023 21:18:34 +0900 Subject: [PATCH 5/7] Minor updates for minimize the diff + Same update in Lib/typing.py. --- Doc/library/typing.rst | 5 +++-- Lib/typing.py | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 4299c8aa72d14e..1fef3c5bd42e58 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2607,8 +2607,9 @@ Functions and decorators Ask a static type checker to reveal the statically inferred type of an expression. - When a static type checker encounters a call to this function, it emits a - diagnostic with the inferred static type of the argument. For example:: + When a static type checker encounters a call to this function, + it emits a diagnostic with the inferred static type of the argument. + For example:: x: int = 1 reveal_type(x) # Revealed type is "builtins.int" diff --git a/Lib/typing.py b/Lib/typing.py index 61b88a560e9dc5..f498fcb5faf55a 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3301,10 +3301,11 @@ def __enter__(self) -> 'TextIO': def reveal_type[T](obj: T, /) -> T: - """Reveal the inferred type of a variable. + """Ask a static type checker to reveal the statically inferred type of an + expression. - When a static type checker encounters a call to ``reveal_type()``, - it will emit the inferred type of the argument:: + When a static type checker encounters a call to this function, + it emits a diagnostic with the inferred static type of the argument. x: int = 1 reveal_type(x) From 5b36b4521851817fe17e9d7a62560575d748706e Mon Sep 17 00:00:00 2001 From: Kir Chou Date: Wed, 20 Dec 2023 21:28:13 +0900 Subject: [PATCH 6/7] Remove duplicated static in the sentences. --- Doc/library/typing.rst | 6 ++---- Lib/typing.py | 5 ++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 1fef3c5bd42e58..34c3cc0b597f3f 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2604,12 +2604,10 @@ Functions and decorators .. function:: reveal_type(obj, /) - Ask a static type checker to reveal the statically inferred type of an - expression. + Ask a static type checker to reveal the inferred type of an expression. When a static type checker encounters a call to this function, - it emits a diagnostic with the inferred static type of the argument. - For example:: + it emits a diagnostic with the inferred type of the argument. For example:: x: int = 1 reveal_type(x) # Revealed type is "builtins.int" diff --git a/Lib/typing.py b/Lib/typing.py index f498fcb5faf55a..01b908ef245be2 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3301,11 +3301,10 @@ def __enter__(self) -> 'TextIO': def reveal_type[T](obj: T, /) -> T: - """Ask a static type checker to reveal the statically inferred type of an - expression. + """Ask a static type checker to reveal the inferred type of an expression. When a static type checker encounters a call to this function, - it emits a diagnostic with the inferred static type of the argument. + it emits a diagnostic with the inferred type of the argument. x: int = 1 reveal_type(x) From cf9a6ad323614ff00da6727911f9899731d81b8c Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Wed, 20 Dec 2023 13:21:57 +0000 Subject: [PATCH 7/7] Remove some redundancy; a couple of other nits --- Doc/library/typing.rst | 20 ++++++++------------ Lib/typing.py | 6 +++--- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 34c3cc0b597f3f..63bd62d1f6679b 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2615,18 +2615,9 @@ Functions and decorators This can be useful when you want to debug how your type checker handles a particular piece of code. - The function returns its argument unchanged, which allows using - it within an expression:: - - x = reveal_type(1) # Revealed type is "builtins.int" - - Most type checkers support ``reveal_type()`` anywhere, even if the - name is not imported from ``typing``. Importing the name from - ``typing`` allows your code to run without runtime errors and - communicates intent more clearly. - - At runtime, this function prints the runtime type of its argument to stderr - and returns it unchanged:: + At runtime, this function prints the runtime type of its argument to + :data:`sys.stderr` and returns the argument unchanged (allowing the call to + be used within an expression):: x = reveal_type(1) # prints "Runtime type is int" print(x) # prints "1" @@ -2634,6 +2625,11 @@ Functions and decorators Note that the runtime type may be different from (more or less specific than) the type statically inferred by a type checker. + Most type checkers support ``reveal_type()`` anywhere, even if the + name is not imported from ``typing``. Importing the name from + ``typing``, however, allows your code to run without runtime errors and + communicates intent more clearly. + .. versionadded:: 3.11 .. decorator:: dataclass_transform(*, eq_default=True, order_default=False, \ diff --git a/Lib/typing.py b/Lib/typing.py index 01b908ef245be2..d7d793539b35b1 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3303,8 +3303,8 @@ def __enter__(self) -> 'TextIO': def reveal_type[T](obj: T, /) -> T: """Ask a static type checker to reveal the inferred type of an expression. - When a static type checker encounters a call to this function, - it emits a diagnostic with the inferred type of the argument. + When a static type checker encounters a call to ``reveal_type()``, + it will emit the inferred type of the argument:: x: int = 1 reveal_type(x) @@ -3313,7 +3313,7 @@ def reveal_type[T](obj: T, /) -> T: will produce output similar to 'Revealed type is "builtins.int"'. At runtime, the function prints the runtime type of the - argument and returns it unchanged. + argument and returns the argument unchanged. """ print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr) return obj