Skip to content

Use _typeshed.Self where __enter__ returns self#5698

Merged
Akuli merged 2 commits into
python:masterfrom
domdfcoding:enter-self
Jun 27, 2021
Merged

Use _typeshed.Self where __enter__ returns self#5698
Akuli merged 2 commits into
python:masterfrom
domdfcoding:enter-self

Conversation

@domdfcoding
Copy link
Copy Markdown
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
Copy Markdown
Contributor

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

@JelleZijlstra
Copy link
Copy Markdown
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
Copy Markdown
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