Skip to content
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

Use _typeshed.Self where __enter__ returns self #5698

Merged
merged 2 commits into from
Jun 27, 2021
Merged

Use _typeshed.Self where __enter__ returns self #5698

merged 2 commits into from
Jun 27, 2021

Conversation

domdfcoding
Copy link
Contributor

I have made a start on annotating __enter__ methods as def __enter__(self: Self) -> Self: ... where the method just returns the class instance (self)

The current annotations assume that the __enter__ method on subclasses returns an instance of the parent class.

There are more than these, but they are either more complicated or are already annotated as def __enter__(self: _T) -> _T: ...


This is a step towards #5676, and is related to #5675 and #5602

@github-actions
Copy link
Contributor

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

@JelleZijlstra
Copy link
Member

Thank you! We should also use Self for __enter__ methods that are already annotated with a typevar, but obviously that's less urgent.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

I checked all of these against the implementation and they're indeed just return self.

@Akuli Akuli marked this pull request as ready for review June 27, 2021 19:58
@Akuli Akuli merged commit 58559e5 into python:master Jun 27, 2021
@domdfcoding domdfcoding deleted the enter-self branch July 27, 2021 18:09
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