Skip to content

Improve traceback.FrameSummary #7210

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 15, 2022
Merged

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Feb 14, 2022

  • Add missing __len__ method.
  • lineno can be None
  • line is a read-only property that can be str or None, not a str attribute.
  • Improve __getitem__ with overloads.
  • Reflect changes to the class that have been made in 3.11 (new __init__ signature, new instance attributes).

Source code here: https://github.com/python/cpython/blob/b27195332e91e932501f16cf9877761b218a9c99/Lib/traceback.py#L243

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member Author

Diff from mypy_primer, showing the effect of this PR on open source code:

core (https://github.com/home-assistant/core)
+ homeassistant/util/async_.py:158: error: Item "None" of "Optional[str]" has no attribute "strip"  [union-attr]
+ homeassistant/util/async_.py:164: error: Item "None" of "Optional[str]" has no attribute "strip"  [union-attr]
+ homeassistant/helpers/frame.py:112: error: Item "None" of "Optional[str]" has no attribute "strip"  [union-attr]

Arguably these are true positives: line can clearly be None

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/debug/tbtools.py:378: error: Unsupported operand types for - ("None" and "int")  [operator]
+ src/werkzeug/debug/tbtools.py:378: note: Left operand is of type "Optional[int]"

pyp (https://github.com/hauntsaninja/pyp)
+ pyp.py:641: error: Argument 1 to "code_for_line" has incompatible type "Optional[int]"; expected "int"

core (https://github.com/home-assistant/core)
+ homeassistant/util/async_.py:158: error: Item "None" of "Optional[str]" has no attribute "strip"  [union-attr]
+ homeassistant/util/async_.py:164: error: Item "None" of "Optional[str]" has no attribute "strip"  [union-attr]
+ homeassistant/helpers/frame.py:112: error: Item "None" of "Optional[str]" has no attribute "strip"  [union-attr]

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jul 19, 2022

@AlexWaygood any idea under what circumstances lineno can be None?

edit: I got misled by something, removed some noise

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jul 19, 2022

In practice it looks like lineno's in traceback come from https://github.com/python/cpython/blob/b27195332e91e932501f16cf9877761b218a9c99/Lib/traceback.py#L329

which we have as an int

def tb_lineno(self) -> int: ...

@AlexWaygood
Copy link
Member Author

https://github.com/python/cpython/blob/b27195332e91e932501f16cf9877761b218a9c99/Lib/traceback.py#L305 looks pretty reachable to me, but I'll confess I don't know what real-life code ends up with us getting there :)

@hauntsaninja
Copy link
Collaborator

So should we be changing TracebackType.tb_lineno?

@AlexWaygood
Copy link
Member Author

I don't know. I'm not an exception-handling expert, and the language reference doesn't shed much light here. Feel free to revert the int -> int | None changes if you're confident I'm incorrect here :)

@Akuli
Copy link
Collaborator

Akuli commented Jul 19, 2022

This might have something to do with #6769. Presumably traceback line numbers come from stack frames.

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