Skip to content

Add _typeshed.(Opt)ExcInfo #7645

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 8 commits into from
Apr 17, 2022
Merged

Add _typeshed.(Opt)ExcInfo #7645

merged 8 commits into from
Apr 17, 2022

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Apr 16, 2022

No description provided.

@srittau srittau marked this pull request as draft April 16, 2022 18:58
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

from types import FrameType, TracebackType
from typing import IO, Any, Callable
from typing_extensions import TypeAlias

_ExcInfo: TypeAlias = tuple[type[BaseException] | None, BaseException | None, TracebackType | None]

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, tuple[type[BaseException], BaseException, TracebackType] | tuple[None, None, None] is a different type to tuple[type[BaseException] | None, BaseException | None, TracebackType | None], so this is a semantic change

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed, but usually the latter was a shortcut used for the former. See the typical __exit__ annotation that should properly be annotated using an overload.

Copy link
Member

Choose a reason for hiding this comment

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

Right, the latter is a supertype of the former

Copy link
Member

Choose a reason for hiding this comment

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

I believe it's not strictly true that it's always either all-None or all-non-None, at least before 3.11. (Irit did some work in 3.11 that made it always true.) See here: https://github.com/python/cpython/blob/84c279b514141f608cf480905c87d48998e296d1/Python/ceval.c#L4474

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This could be a remnant of Python 2 string exceptions.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's better to err on the side of safety here and define _typeshed.OptExcInfo as tuple[type[BaseException] | None, BaseException | None, TracebackType | None] instead of tuple[type[BaseException], BaseException, TracebackType] | tuple[None, None, None]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Primer output looks good and from a type safety perspective, I'd rather go with the more strict/correct annotation. Personally, I've not come across a situation where the type is None when the other fields are not, and allowing that could certainly introduce subtle bugs. It would also disagree with the documentation of both sys.exc_info and contextmanager.__exit__.

Copy link
Member

Choose a reason for hiding this comment

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

@JelleZijlstra, do you know of any real-world examples where some (but not all) elements of the tuple are None?

If not, I'm fine with merging this, as it's probably just a theoretical issue :)

Copy link
Member

Choose a reason for hiding this comment

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

From python/cpython#89874 (comment), sounds like it could only happen with interesting use of the C API, so probably not worth worrying about.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@srittau srittau marked this pull request as ready for review April 16, 2022 19:39
@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@AlexWaygood AlexWaygood merged commit 321359c into python:master Apr 17, 2022
@srittau srittau deleted the exc-info branch April 18, 2022 11:59
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