Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-46589: Improve documentation for typing._GenericAlias #31026

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

Merged
merged 4 commits into from
Feb 7, 2022

Conversation

mrahtz
Copy link
Contributor

@mrahtz mrahtz commented Jan 30, 2022

While implementing PEP 646, I was getting really confused about what was happening in typing._GenericAlias, so I've tried to improve it with the following changes:

  • Added docstrings for __init__ and __getitem__.
  • Renamed params to args. (My understanding is that in the context of generic types, the term 'arguments' should be used to refer to the list of things passed through the square brackets - e.g. in tuple[int, T], (int, T) are the arguments.)
  • Refactored a bunch of edge cases into _determine_new_args.
  • Reduced the nesting of edge case processing.
  • Added a comment explaining why flattening is needed with Callable.
  • Made some variable names a bit more descriptive:
    • substnew_arg_by_param
    • argold_arg or new_arg depending on the context

(I'm hoping this is just about trivial enough to be alright without an issue on bugs.python.org. If not, I'm happy to create one.) Done!

https://bugs.python.org/issue46589

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, this is helpful.

@Fidget-Spinner
Copy link
Member

This is awesome! This will definitely benefit new contributors. I can personally attest to taking the same path you took (and being confused at exactly the same parts). I'll send a full review at a later date.

PS. Please create an issue. It's easier for us to track this that way in case anything crops up after.

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

First pass on docstrings/comments.

@mrahtz mrahtz changed the title Improve documentation for typing._GenericAlias bpo-46589: Improve documentation for typing._GenericAlias Jan 30, 2022
Lib/typing.py Outdated
@@ -999,16 +999,42 @@ def __dir__(self):


class _GenericAlias(_BaseGenericAlias, _root=True):
def __init__(self, origin, params, *, inst=True, name=None,
"""The type of parameterized generics.
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this should be a docstring or a comment -- it's not a public class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, interesting, I wasn't aware of that convention. I've gone ahead and changed these docstrings to comments.

Copy link
Member

Choose a reason for hiding this comment

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

I am super sorry for making you do that! It seems the rest of this module uses docstrings for all internal types.

As penance I will fix it back before landing.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

(Eh, I said I'd change it back to docstrings, but I can't be bothered. It's fine this way, and thanks so much for figuring all this out!)

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.

6 participants