Skip to content

Conversation

@nathanielmanistaatgoogle
Copy link
Contributor

@nathanielmanistaatgoogle nathanielmanistaatgoogle commented Sep 7, 2018

traceback.FrameSummary's line parameter is a string that is the text
of a line of code, not an int that is the line number of a line of
code.

Also relax type of traceback.format_list's extracted_list parameter
in 3.5-and-later from List[FrameSummary] to Iterable[FrameSummary].
Never ask for a List when asking for a Sequence will do, and never ask
for a Sequence when asking for a Collection will do, and never ask for
a Collection when asking for an Iterable will do (and so on in other
cases, but this case stops at Iterable).

Also add a note about deliberately not supporting for type-checking
purposes the 3.4-and-earlier List[Tuple[str, int, str, Optional[str]]]
semantics for traceback.format_list's extracted_list parameter.
traceback currently supports the old type of parameter, but if
authors are typechecking their 3.5-and-later code, it's reasonable to
expect that they'll want to modernize their code to use FrameSummary
objects rather than four-tuples.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! I left a note below.

# enough to type-check their 3.5-or-later code, they're probably diligent
# enough to pass FrameSummarys rather than make use of the
# only-for-backwards-compatibility four-tuple parameter type semantics.
def format_list(extracted_list: Iterable[FrameSummary]) -> List[str]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Supplying a list (or iterable) of tuples is not discouraged by the docs and still explicitly supported. Therefore, I'd indeed prefer the type to be Iterable[Union[_PT, FrameSummary]]. Also, could you make the same change to StackSummary.from_list() for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see where you're coming from, but... it just feels like maybe someone's reaching for a chance to deprecate older semantics and I want to help that process along.

Also ad hoc polymorphism is no one's friend (despite its long history in Python...).

I've filed a documentation-clarification bug; let's see what kind of response it gets after a few days?

Copy link
Contributor Author

@nathanielmanistaatgoogle nathanielmanistaatgoogle left a comment

Choose a reason for hiding this comment

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

Thanks!

# enough to type-check their 3.5-or-later code, they're probably diligent
# enough to pass FrameSummarys rather than make use of the
# only-for-backwards-compatibility four-tuple parameter type semantics.
def format_list(extracted_list: Iterable[FrameSummary]) -> List[str]: ...
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see where you're coming from, but... it just feels like maybe someone's reaching for a chance to deprecate older semantics and I want to help that process along.

Also ad hoc polymorphism is no one's friend (despite its long history in Python...).

I've filed a documentation-clarification bug; let's see what kind of response it gets after a few days?

@nathanielmanistaatgoogle
Copy link
Contributor Author

I think this is now ready for integration - there are changes that I would have liked to have made here, but they're instead described in TODOs while the conversation continues in the bug.

@nathanielmanistaatgoogle nathanielmanistaatgoogle force-pushed the traceback branch 2 times, most recently from 0d5cbe5 to 59985ae Compare September 18, 2018 05:09
@nathanielmanistaatgoogle
Copy link
Contributor Author

@srittau: what do you think of the state of this change? I think this is mergeable now; have you taken a look?

rchen152
rchen152 previously approved these changes Sep 20, 2018
@rchen152
Copy link
Collaborator

This PR looks good to me.
@srittau Are there any other changes you want to see?

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thank you for the changes. One more thing I noticed.

# drop support for _PT.
@classmethod
def from_list(cls, a_list: List[_PT]) -> StackSummary: ...
def from_list(cls, a_list: List[Union[_PT, FrameSummary]]) -> StackSummary: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will probably not work as expected due to the invariance of List. (Although when I just tested it, mypy for some reason interpreted List[Union[_PT, FrameSummary]] as List[Any].) This should be Union[List[_PT], List[FrameSummary]].

Copy link
Member

Choose a reason for hiding this comment

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

Why not just Iterable or Sequence? Even if the documentation says "list", we routinely accept a wider type in stubs; documentation is often written in a more informal style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@JelleZijlstra: I agree!

... but however routine it is, it didn't strike me as a good idea for typeshed to describe as acceptable a wider type than what a standard library module's maintainers officially describe as supported.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think both are acceptable. Personally, I would go with a List as that is what the stdlib maintainers seem to prefer, but accepting an Iterable is fine, too. The latter has the advantage of being covariant.

@nathanielmanistaatgoogle
Copy link
Contributor Author

This is an important and productive conversation that we are having here but in the meantime I have extracted the non-controversial parts of this change into pull request 2470.

Will respond to the outstanding comments here sometime in the next few days I promise! :-)

Never ask for a List when asking for a MutableSequence will do. Never
ask for a MutableSequence when asking for a Sequence will do. Never ask
for a Sequence when asking for a Collection will do. Never ask for a
Collection when asking for an Iterable will do.

Also drop support for Tuple[str, int, str, Optional[str]] elements in
the iterables passed to traceback.format_list and
traceback.StackSummary.from_list - if authors are diligent enough to be
type-checking their code, they are diligent enough to be making use of
their dependencies according to their preferred semantics rather than
their backwards-compatibility carve-outs.
@nathanielmanistaatgoogle nathanielmanistaatgoogle changed the title Fix traceback.FrameSummary's "line" parameter Loosen parameters of traceback.format_list and traceback.StackSummary.from_list Sep 24, 2018
@nathanielmanistaatgoogle
Copy link
Contributor Author

Retitled to reflect current content, and for now this change is aspirational based on how the discussion in Python bug 34648 proceeds. But I think it's a worthwhile aspiration! :-P

@rchen152 rchen152 dismissed their stale review September 24, 2018 23:40

Dismissing my review because the purpose of this PR has changed since I approved it.

def format_list(extracted_list: List[FrameSummary]) -> List[str]: ...
# NOTE(https://bugs.python.org/issue34648): Authors who are diligent
# enough to type-check their code are authors who are diligent enough to
# upgrade their Tuple[str, int, str, Optional[str]]s to FrameSummarys.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this kind of rhetoric belongs in a comment. I would be willing to make this accept an Iterable instead of a List, but there's no need for the comments.

@srittau
Copy link
Collaborator

srittau commented Dec 2, 2018

I am closing this PR for now, based on the discussion in bpo-34648. I think changing List to Iterable in the parameter types still has merit, but disallowing tuples, which are explicitly documented to work is not the task of the type system. I also agree on @JelleZijlstra's point about the comment rhetoric.

@srittau srittau closed this Dec 2, 2018
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