GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-105286: Improve `typing.py` docstrings by AlexWaygood · Pull Request #105287 · python/cpython

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Jun 4, 2023 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: Improve typing.py docstrings #105286

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
Improve typing.py docstrings
e808f2b
@AlexWaygood
AlexWaygood requested review from Fidget-Spinner, JelleZijlstra and gvanrossum as code owners June 4, 2023 18:11
@bedevere-bot bedevere-bot added the awaiting core review label Jun 4, 2023
@bedevere-bot bedevere-bot mentioned this pull request Jun 4, 2023
Improve typing.py docstrings #105286
Closed
@AlexWaygood AlexWaygood added skip news topic-typing needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Jun 4, 2023
AlexWaygood
AlexWaygood commented Jun 4, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline justifications for some of the changes:

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/typing.py
@@ -1,20 +1,21 @@
"""
The typing module: Support for gradual typing as defined by PEP 484.
The typing module: Support for gradual typing as defined by PEP 484 and subsequent PEPs.

@AlexWaygood AlexWaygood Jun 4, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many other PEPs have revised the typing spec since PEP 484

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/typing.py
Comment on lines -8 to +10
Any, NoReturn, Never, ClassVar, Union, Optional, Concatenate, Unpack
* Classes whose instances can be type arguments in addition to types:
ForwardRef, TypeVar and ParamSpec
NoReturn, Never, ClassVar, Self, Concatenate, Unpack, and others.
* Classes whose instances can be type arguments:
TypeVar, ParamSpec, TypeVarTuple.

@AlexWaygood AlexWaygood Jun 4, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Any is now its own class, rather than being an instance of _SpecialForm
  • Numerous other _SpecialForms have now been added. Rather than attempting to list them all (which would just go out of date again), I've just added "and others" at the end of an abbreviated list.
  • Instances of ForwardRef aren't actually accepted by type checkers as type annotations, as far as I know, so that was somewhat confusing here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/typing.py
* Public helper functions: get_type_hints, overload, cast, no_type_check,
no_type_check_decorator.
* Generic aliases for collections.abc ABCs and few additional protocols.
* Public helper functions: get_type_hints, overload, cast, final, and others.

@AlexWaygood AlexWaygood Jun 4, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many other public helper functions have been added that aren't listed here, so I just made it an abbreviated list and put "and others" at the end

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/typing.py
Comment on lines -254 to +255
_collect_parameters((T, Callable[P, T])) == (T, P)
assert _collect_parameters((T, Callable[P, T])) == (T, P)

@AlexWaygood AlexWaygood Jun 4, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guido mentioned in #101827 that he didn't like this style of code example, where it's not really a REPL example, but also isn't something you'd ever put in an executable .py file either. I agree that using an explicit assert is better for this kind of code example.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/typing.py
There is no runtime checking of these properties. The decorator
sets the ``__final__`` attribute to ``True`` on the decorated object
to allow runtime introspection.
attempts to set the ``__final__`` attribute to ``True`` on the decorated

@AlexWaygood AlexWaygood Jun 4, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If AttributeError or TypeError is encountered, the __final__ attribute won't be set.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/typing.py
Comment on lines -2725 to +2727
Alternative equivalent keyword syntax is also accepted::

Employee = NamedTuple('Employee', name=str, id=int)
An alternative equivalent functional syntax is also accepted::

Employee = NamedTuple('Employee', [('name', str), ('id', int)])

@AlexWaygood AlexWaygood Jun 4, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a partial revert of #104945.

#104945 removed any mention of the functional syntax from this docstring. I agree with removing the mention of Python 3.5 from the docstring (also done in #104945), but I disagree with removing any mention of the functional syntax. The functional syntax is supported by type checkers, and still quite common in many codebases.

However, I do think we should remove any mention of the keyword-argument syntax, since this way of creating NamedTuples isn't supported by any type checker that I know of.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
hauntsaninja
hauntsaninja reviewed Jun 5, 2023
View reviewed changes

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, two minor comments

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood @hauntsaninja
Apply suggestions from code review
7f20599
Co-authored-by: Shantanu <[email protected]>
sobolevn
sobolevn reviewed Jun 5, 2023
View reviewed changes

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Outdated
no_type_check_decorator.
* Generic aliases for collections.abc ABCs and few additional protocols.
* Public helper functions: get_type_hints, overload, cast, final, and others.
* Deprecated aliases for collections.abc ABCs.

@sobolevn sobolevn Jun 5, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All points except this one have examples. Is it intentional?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AlexWaygood AlexWaygood Jun 5, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, they're deprecated ;p

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
AlexWaygood and others added 8 commits June 5, 2023 08:43
@AlexWaygood @sobolevn
Update Lib/typing.py
d59d72b
Co-authored-by: Nikita Sobolev <[email protected]>
@AlexWaygood
Update Lib/typing.py
f96723e
@AlexWaygood @sobolevn
Colons
9f132e9
Co-authored-by: Nikita Sobolev <[email protected]>
@AlexWaygood
Use two colons everywhere
e7983b2
@AlexWaygood
Newlines following class docstrings as per PEP 257
ff90369
@AlexWaygood
Nit
d54c06b
@AlexWaygood
Fix first line of NoReturn docstring
ab3e627
@AlexWaygood
Better first sentences
748c55f
JelleZijlstra
JelleZijlstra reviewed Jun 5, 2023
View reviewed changes
Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood added 2 commits June 5, 2023 14:47
@AlexWaygood
Make module docstring more user-facing
f9e27e2
@AlexWaygood
Capitalise Point2D
58fbb0f
JelleZijlstra
JelleZijlstra approved these changes Jun 5, 2023
View reviewed changes

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the improvements!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood reacted with rocket emoji
All reactions
  • 🚀 1 reaction
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Jun 5, 2023
@AlexWaygood
AlexWaygood enabled auto-merge (squash) June 5, 2023 13:54
@AlexWaygood
AlexWaygood merged commit f714aa2 into python:main Jun 5, 2023
@miss-islington

miss-islington commented Jun 5, 2023

Copy link
Copy Markdown
Contributor

Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the awaiting merge label Jun 5, 2023
@miss-islington

miss-islington commented Jun 5, 2023

Copy link
Copy Markdown
Contributor

Sorry, @AlexWaygood, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker f714aa2c29eae5cc4b5b54e4d83d83eebd5eac53 3.12

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington miss-islington assigned AlexWaygood Jun 5, 2023
@miss-islington

miss-islington commented Jun 5, 2023

Copy link
Copy Markdown
Contributor

Sorry @AlexWaygood, I had trouble checking out the 3.11 backport branch.
Please retry by removing and re-adding the "needs backport to 3.11" label.
Alternatively, you can backport using cherry_picker on the command line.
cherry_picker f714aa2c29eae5cc4b5b54e4d83d83eebd5eac53 3.11

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood
AlexWaygood deleted the improve-typing-docstrings branch June 5, 2023 14:19
AlexWaygood added a commit to AlexWaygood/cpython that referenced this pull request Jun 5, 2023
@AlexWaygood @hauntsaninja @sobolevn
pythongh-105286: Improve typing.py docstrings (python#105287)
e4b6cb7
Co-authored-by: Shantanu <[email protected]>
Co-authored-by: Nikita Sobolev <[email protected]>
@bedevere-bot

bedevere-bot commented Jun 5, 2023

Copy link
Copy Markdown

GH-105319 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Jun 5, 2023
AlexWaygood added a commit that referenced this pull request Jun 5, 2023
@AlexWaygood @hauntsaninja @sobolevn
[3.12] gh-105286: Improve typing.py docstrings (#105287) (#105319)
83fc562
gh-105286: Improve `typing.py` docstrings (#105287)

Co-authored-by: Shantanu <[email protected]>
Co-authored-by: Nikita Sobolev <[email protected]>
@bedevere-bot

bedevere-bot commented Jun 5, 2023

Copy link
Copy Markdown

GH-105322 is a backport of this pull request to the 3.11 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jun 5, 2023
AlexWaygood added a commit to AlexWaygood/cpython that referenced this pull request Jun 5, 2023
@AlexWaygood @hauntsaninja @sobolevn
[3.11] pythongh-105286: Improve typing.py docstrings (python#105287)
4b28281
pythongh-105286: Improve `typing.py` docstrings (python#105287)

Co-authored-by: Shantanu <[email protected]>
Co-authored-by: Nikita Sobolev <[email protected]>
ambv pushed a commit that referenced this pull request Jun 5, 2023
@AlexWaygood @hauntsaninja @sobolevn
[3.11] gh-105286: Improve typing.py docstrings (#105287) (#105322)
93d9e99
Co-authored-by: Shantanu <[email protected]>
Co-authored-by: Nikita Sobolev <[email protected]>
@erlend-aasland erlend-aasland mentioned this pull request Jun 6, 2023
3.12 backport gh 105236 #105358
Closed
@AlexWaygood AlexWaygood mentioned this pull request Jun 6, 2023
gh-105286: Further improvements to typing.py docstrings #105363
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sobolevn sobolevn sobolevn left review comments
@hauntsaninja hauntsaninja hauntsaninja left review comments
@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@gvanrossum gvanrossum Awaiting requested review from gvanrossum
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@AlexWaygood AlexWaygood

Labels

skip news topic-typing

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@AlexWaygood @miss-islington @bedevere-bot @JelleZijlstra @sobolevn @hauntsaninja
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.