Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-105286: Improve typing.py docstrings #105287

Merged
merged 12 commits into from
Jun 5, 2023

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Jun 4, 2023

Copy link
Member Author

@AlexWaygood AlexWaygood left a 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:

@@ -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.
Copy link
Member Author

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

Lib/typing.py Outdated
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.
Copy link
Member Author

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.

* 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.
Copy link
Member Author

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

Lib/typing.py Outdated
Comment on lines 254 to 255
_collect_parameters((T, Callable[P, T])) == (T, P)
assert _collect_parameters((T, Callable[P, T])) == (T, P)
Copy link
Member Author

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.


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
Copy link
Member Author

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.

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)])
Copy link
Member Author

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.

Copy link
Contributor

@hauntsaninja hauntsaninja left a 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

Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thanks!

Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
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.
Copy link
Member

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?

Copy link
Member Author

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

Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Copy link
Member

@JelleZijlstra JelleZijlstra left a 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!

@AlexWaygood AlexWaygood enabled auto-merge (squash) June 5, 2023 13:54
@AlexWaygood AlexWaygood merged commit f714aa2 into python:main Jun 5, 2023
21 checks passed
@miss-islington
Copy link
Contributor

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

@miss-islington
Copy link
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

@miss-islington
Copy link
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

@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
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@bedevere-bot
Copy link

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

@bedevere-bot bedevere-bot removed the needs backport to 3.12 bug and security fixes label Jun 5, 2023
AlexWaygood added a commit that referenced this pull request Jun 5, 2023
gh-105286: Improve `typing.py` docstrings (#105287)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@bedevere-bot
Copy link

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

@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
pythongh-105286: Improve `typing.py` docstrings (python#105287)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
ambv pushed a commit that referenced this pull request Jun 5, 2023
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants