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

Import the generics documentation from mypy #1416

Merged
merged 3 commits into from
Jun 25, 2023

Conversation

hauntsaninja
Copy link
Collaborator

No description provided.

docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Show resolved Hide resolved
docs/source/generics.rst Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Show resolved Hide resolved
docs/source/generics.rst Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
reveal_type(box.first([1, 2, 3])) # Revealed type is "builtins.int"
reveal_type(box.pair_with_first([1, 2, 3])) # Revealed type is "tuple[builtins.int, builtins.str]"

In particular, the ``self`` argument may also be generic, allowing a
Copy link
Member

Choose a reason for hiding this comment

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

This example isn't great as it should be using Self. Not sure if there's a better example to give, though.

docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
docs/source/generics.rst Outdated Show resolved Hide resolved
Generics
========

You may have seen type hints like ``list[str]`` or ``dict[str, int]`` in Python
Copy link
Member

Choose a reason for hiding this comment

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

General question: I'm not sure how backwards-compatible these docs are meant to be. In your last PR, I noticed you used Optional[X] instead of X | None throughout, but didn't mention it because I assumed you were trying to keep the code examples compatible with py38+. But this document uses PEP-585 syntax throughout, and doesn't mention the older way of writing these types until the last section right at the bottom.

I prefer making the new syntax more prominent than the old syntax, as this document does, but maybe we should have the note about how to do things in a pre-PEP-585 world slightly higher up?

Copy link
Member

Choose a reason for hiding this comment

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

I think the ideal would be if we had some selector at the top of examples, similar to the one for Unix/Windows at https://packaging.python.org/en/latest/tutorials/packaging-projects/, where users could toggle between "Python 3.9+" and "Python 3.7-3.8" or so.

Setting that up is better left for a separate PR though.

Copy link
Member

@AlexWaygood AlexWaygood Jun 23, 2023

Choose a reason for hiding this comment

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

Yes, that would be ideal! And fine to leave it to another PR, agreed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For the mypy docs this is based on, I went by https://pypistats.org/packages/mypy minor version counts to determine what version should be emphasised. 3.9 is mode and median.

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@hauntsaninja
Copy link
Collaborator Author

Thank you everyone for reviews!

@hauntsaninja hauntsaninja merged commit 7d72ce0 into python:master Jun 25, 2023
2 checks passed
@hauntsaninja hauntsaninja deleted the generic-docs branch June 25, 2023 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants