GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — bpo-43224: Draft implementation of PEP 646 by mrahtz · Pull Request #30398 · python/cpython Conversation
|
|
|
||
| if (gi->exhausted) { | ||
| gi->obj = NULL; | ||
| return NULL; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I feel like you could potentially be missing a decref here for gi->obj since I see it being increfed in gi_iter. If so,
| return NULL; | |
| Py_SETREF(gi->obj, NULL); | |
| return NULL; |
If I'm just shooting my mouth off, then just please ignore what I said :).
One way to test for refleaks is to run the tests with the refleak hunter (requires a debug build):
python_d.exe -m test test_typing -R 3:3
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oh, super nice about the refleak hunter - I had no idea that was a thing!
And good catch about the decref too. One thing I'm unsure about is where we should do it, though. Reading https://docs.python.org/3/c-api/gcsupport.html, am I right in thinking that the iterator object gi counts as a container type, because it contains references to another object? If so, that same page says "Before fields which refer to other containers are invalidated, PyObject_GC_UnTrack() must be called". That would imply we should be setting gi->obj to NULL in the deallocator, in which case we'd also call the setref there?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Friendly poke :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| slices[expr_ty]: | ||
| | a=slice !',' { a } | ||
| | a[asdl_expr_seq*]=','.slice+ [','] { _PyAST_Tuple(a, Load, EXTRA) } | ||
| | a[asdl_expr_seq*]=','.(slice | starred_expression)+ [','] { _PyAST_Tuple(a, Load, EXTRA) } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to move starred_expression as another option of slice as we already have named_expression there
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I just tried this, but then I get SyntaxError: can't use starred expression here for e.g. a[*b].
I think what happens is, if we move starred_expression to slice (as | a=starred_expression { a }), then when we do a[*b], it triggers the first option of slices, where the starred_expression doesn't get processed by _PyAST_Tuple.
I can't see any way to make sure _PyAST_Tuple definitely gets called for starred_expression without doing something to slices, so I think the original attempt with (slice | starred_expression) is cleanest. Wdyt?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| param_no_default[arg_ty]: | ||
| | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) } | ||
| | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) } | ||
| param_no_default_star_annotation[arg_ty]: |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Why not join these to param_no_default? That way we can remove the extra rule in star_etc.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Hmm, wouldn't that mess with other things that use param_no_default though? E.g. kwds uses param_no_default, so if we joined these to param_no_default, wouldn't we accidentally allow def foo(***args)?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Friendly poke :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Based on Fidget-Spinner's feedback: 1. Remove the explicit flag for 'exhausted', instead using 'gi->obj == NULL' to tell whether the iterator is exhausted. 2. Construct `starred_tuple` using the `Py_GenericAlias` constructor instead of constructing it manually. 3. Fix reference counting. 4. Make `Py_GenericAliasIterType` static.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Just a couple of things I noticed while working on the typing-extensions implementation.
I'm not going to be the one to merge this, but I think it might make sense to split this PR into separate PRs for the grammar changes and the typing.py changes, since they touch very different parts of the codebase and need review from different people.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| class StarredTuple(_Final, _Immutable, _root=True): | ||
| """Implementation of starred tuple for older versions of Python. | ||
|
|
||
| A starred tuple is e.g. tuple[*tuple[int, str]]. In newer versions of |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
But this implementation is only used in new versions of Python, so why do we need this?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, thanks for the reminder on this too - I was unsure about this.
The two things I had in mind were:
typing.py for the sake of code (or maybe even pickle?) compatibility. Unpack is probably the most important one, so we can read backwards-compatible code written as class C(Generic[Unpack[Ts]]). I'm admittedly less sure about StarredTuple; I don't think there's ever a reason to instantiate it explicitly (and in fact maybe it should be private?). In any case, not sure what best practice is in cases like this - @gvanrossum can you comment?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| def __class_getitem__(cls, item): | ||
| if isinstance(item, types.GenericAlias) and item.__origin__ is tuple: | ||
| # tuple[...] | ||
| return StarredTuple(item) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Could this return an instance of Unpack instead so we don't need to add an additional class?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It's definitely an option, but I'd prefer to keep Unpack as only implementing the operator, rather implementing both the operator and the wrapper class for unpacked items - in line with the principle of "Each component should only do one thing".
Also, I think Unpack could get really messy if we have to merge in the guts of both StarredTuple and UnpackedTypeVarTuple. And there are a bunch of places where we want to check for an unpacked TypeVarTuple but not an unpacked StarredTuple, so they would get more complicated too.
Maybe there's context I'm missing though. Is there a big downside (e.g. maintenance burden) to adding another class?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| elif isinstance(item, TypeVarTuple): | ||
| return item._unpacked | ||
| else: | ||
| raise TypeError( |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to not raise an error here, so we don't hobble potential future extensions. For example, I anticipate that we may want to support Unpack[TypedDict] to type **kwargs in the future.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
a) If the concern is future extensions, couldn't we just amend this function when that need arises in the future?
b) Since the result of unpacking depends on what's being unpacked, what would we do instead of raising an error here?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
This PR has grown pretty big, and Pradeep agrees with Jelle that it might be better to split this up into separate parts - so I'm going to close this PR, and open a few new ones instead. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return self._name | ||
|
|
||
|
|
||
| class UnpackedTypeVarTuple(_Final, _Immutable, _root=True): |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@pradeep90 In the previous review, you mentioned you'd prefer to avoid this middleman class, instead for example making it so that starring a TypeVarTuple results in an instance of Unpack.
That example solution seems suboptimal to me for two reasons:
instance(obj, UnpackedTypeVarTuple) seems more readable to me than isinstance(obj, Unpacked).Unpack both be an operator and an object.Another option would be something like:
class TypeVarTuple: def __init__(self, name, _starred=False, _id=None): self.name = name self._starred = starred self._id = id def __iter__(self): yield TypeVarTuple(self.name, _starred=True, _id=id(self)) def __eq__(self, other): if self._id is not None and other._id is not None: return self._id == other._id else: return object.__eq__(self, other) # Not sure whether this is right, but you get the gist def __repr__(self): return ('*' if self._starred else '') + self.name
I lean away from this solution too because I feel like manually managing IDs could lead to subtle bugs, and isinstance(obj, UnpackedTypeVarTuple) is still simpler than isinstance(obj, TypeVarTuple) and obj._starred, but WDYT?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| self.__module__ = def_mod | ||
|
|
||
|
|
||
| class TypeVarTuple(_Final, _Immutable, _root=True): |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@pradeep90 You mentioned in the previous review you'd prefer to have this inherit from _TypeVarLike. I'm not sure I understood your reasoning - could you explain what you meant by "The flags are things that the typechecker has to deal with"?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
|
||
| if (gi->exhausted) { | ||
| gi->obj = NULL; | ||
| return NULL; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oh, super nice about the refleak hunter - I had no idea that was a thing!
And good catch about the decref too. One thing I'm unsure about is where we should do it, though. Reading https://docs.python.org/3/c-api/gcsupport.html, am I right in thinking that the iterator object gi counts as a container type, because it contains references to another object? If so, that same page says "Before fields which refer to other containers are invalidated, PyObject_GC_UnTrack() must be called". That would imply we should be setting gi->obj to NULL in the deallocator, in which case we'd also call the setref there?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
|
||
| if (gi->exhausted) { | ||
| gi->obj = NULL; | ||
| return NULL; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Friendly poke :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| param_no_default[arg_ty]: | ||
| | a=param ',' tc=TYPE_COMMENT? { _PyPegen_add_type_comment_to_arg(p, a, tc) } | ||
| | a=param tc=TYPE_COMMENT? &')' { _PyPegen_add_type_comment_to_arg(p, a, tc) } | ||
| param_no_default_star_annotation[arg_ty]: |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Friendly poke :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@JelleZijlstra @pablogsal @Fidget-Spinner @pradeep90 Oh, gosh, I'm so sorry - I've just realised I'd misunderstood a critical part of GitHub's code review interface! I'd assumed all these comments had been posted weeks ago - I'd missed the part where you actually have to click the 'Submit code review' button to post them! Uff. I'll copy over these threads to the new PRs. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@mrahtz I can review and help with the GC part. Feel free to add me as a reviewer in that PR |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.