Skip to content

Updated type annotations to match those in typeshed - #40

Merged
agronholm merged 1 commit into
mainfrom
update-annotations
Nov 13, 2022
Merged

Updated type annotations to match those in typeshed#40
agronholm merged 1 commit into
mainfrom
update-annotations

Conversation

@agronholm

Copy link
Copy Markdown
Owner

No description provided.

@coveralls

Copy link
Copy Markdown

@agronholm

Copy link
Copy Markdown
Owner Author

@Zac-HD do you have interest in reviewing this?

@Zac-HD Zac-HD left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Happy to review, looks good though two questions below:

Comment on lines +166 to +167
) -> tuple[BaseExceptionGroup[_BaseExceptionT] | None, Self | None] | tuple[
Self | None, Self | None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not seeing how we get a non-Self case from this, since we're using .derive()?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I'm not sure; maybe my implementation misses a case? This is how it was annotated in the typeshed though, so 🤷

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, might be a typeshed bug then? I'd be fine with merging it as-is though; it doesn't seem particularly important to investigate.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, got it: the correct return-type annotation is tuple[BaseExceptionGroup[_BaseExceptionT] | None, BaseExceptionGroup[_BaseExceptionT] | None], no use of Self or overrides. That's because the more precise annotation is only true for subclasses which implement .derive(), in which case they should also ship more precise (but still not Self) annotations for .subgroup() and .split().

I'll open an upstream issue against typeshed shortly.

instance: ExceptionGroup[_ExceptionT_co] = super().__new__(
cls, __message, __exceptions
)
if cls is ExceptionGroup:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be if issubclass(cls, Exception) (or ExceptionGroup)? As-is I could subclass ExceptionGroup and then store a BaseException in that just fine.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Yes, this is how the upstream implementation works too. Try running this on Python 3.11:

class CustomExcGroup(ExceptionGroup):
    pass

exc = CustomExcGroup("message", [SystemExit(), ValueError()])

Works fine. This is somewhat covered in PEP 654: https://peps.python.org/pep-0654/#subclassing-exception-groups

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yep, I should probably go complain ask about this upstream 😅

@agronholm
agronholm merged commit cbac14e into main Nov 13, 2022
@agronholm
agronholm deleted the update-annotations branch November 13, 2022 21:19
@agronholm

Copy link
Copy Markdown
Owner Author

Thanks for the quick response! This was actually blocking an important upcoming AnyIO changeset from passing mypy checks.

@Zac-HD

Zac-HD commented Nov 13, 2022

Copy link
Copy Markdown
Contributor

Glad I could help!

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.

3 participants