-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Loosen parameters of traceback.format_list and traceback.StackSummary.from_list #2436
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
Loosen parameters of traceback.format_list and traceback.StackSummary.from_list #2436
Conversation
srittau
left a comment
There was a problem hiding this 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]: ... |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this 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]: ... |
There was a problem hiding this comment.
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?
f365bdf to
b6ba8a2
Compare
b6ba8a2 to
6161139
Compare
|
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. |
0d5cbe5 to
59985ae
Compare
|
@srittau: what do you think of the state of this change? I think this is mergeable now; have you taken a look? |
|
This PR looks good to me. |
srittau
left a comment
There was a problem hiding this 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.
stdlib/2and3/traceback.pyi
Outdated
| # 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: ... |
There was a problem hiding this comment.
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]].
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... 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.
There was a problem hiding this comment.
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.
|
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! :-) |
59985ae to
55caf8c
Compare
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.
55caf8c to
a5bbe2f
Compare
|
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 |
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. |
There was a problem hiding this comment.
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.
|
I am closing this PR for now, based on the discussion in bpo-34648. I think changing |
traceback.FrameSummary'slineparameter is a string that is the textof a line of code, not an int that is the line number of a line of
code.
Also relax type of
traceback.format_list'sextracted_listparameterin 3.5-and-later from
List[FrameSummary]toIterable[FrameSummary].Never ask for a
Listwhen asking for aSequencewill do, and never askfor a
Sequencewhen asking for aCollectionwill do, and never ask fora
Collectionwhen asking for anIterablewill do (and so on in othercases, 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'sextracted_listparameter.tracebackcurrently supports the old type of parameter, but ifauthors are typechecking their 3.5-and-later code, it's reasonable to
expect that they'll want to modernize their code to use
FrameSummaryobjects rather than four-tuples.