GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update `__exit__` parameters in stubs by Avasam · Pull Request #9696 · python/typeshed

Conversation

@Avasam

@Avasam Avasam commented Feb 9, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Follow-up to #9519 for third-party stubs now that a new version of mypy has been released
Ref #9297

I could not validate psycopg2, hdbcli and _cffi_backend so I left those as-is

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam added 2 commits February 8, 2023 23:09
@Avasam
Update __exit__ parameters in stubs
f35b8e9
@Avasam
Abstract startablecontext means not unused
2e8cba4
@github-actions

This comment has been minimized.

Sign in to view
@srittau

srittau commented Feb 9, 2023

Copy link
Copy Markdown
Collaborator

I'm not sure that replacing proper __exit__ types with (essentially) object is the right course of action. I can't imagine a situation where you would want to pass some arbitrary into __exit__. In fact I'd say that's a sign of potential bug (even if __exit__ itself accepts it.) It also makes sub-classing more difficult, as an overridden __exit__ would also need to accept object, instead of the proper types.

hauntsaninja, AlexWaygood, Akuli, JelleZijlstra, and Avasam reacted with thumbs up emoji
All reactions
  • 👍 5 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Avasam added 4 commits February 9, 2023 09:50
@Avasam
Restore stubs/SQLAlchemy/sqlalchemy/engine/base.pyi
71740e1
@Avasam
Revert changing already defined non-object exit params
56e7ad9
@Avasam
Merge branch '__exit__-parameters-stubs' of https://github.com/Avasam…
f6cd777
…/typeshed into __exit__-parameters-stubs
@Avasam
Merge branch 'main' into __exit__-parameters-stubs
b5cb959
@Avasam

Avasam commented Feb 9, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

[...] I can't imagine a situation where you would want to pass some arbitrary into __exit__. In fact I'd say that's a sign of potential bug (even if __exit__ itself accepts it.)
It also makes sub-classing more difficult, as an overridden __exit__ would also need to accept object, instead of the proper types.

Should non-star __a?exit__ parameters always use their exact type then? (even if unused). Maybe except for mock.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@srittau

srittau commented Feb 9, 2023

Copy link
Copy Markdown
Collaborator

I'd say ideally all __exit__ signatures would use the following signature (with the exception of the return type), even those that just use *args in the implementation:

_E = TypeVar("_E", bound=BaseException)

class X:
    @overload
    def __exit__(self, __type: None, __value: None, __exc: None) -> None: ...
    @overload
    def __exit__(self, __type: type[_E], __value: type[_E], __exc: TracebackType) -> None: ...

But since that is unwieldy, we can use the best approximation in a current situation. So I'm actually fine to use Unused for new annotations if that's the fastest way to do it, but we shouldn't regress back from the ideal situation. But I'd be interested in what the other maintainers think.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

This comment has been minimized.

Sign in to view
@Avasam

Avasam commented Feb 9, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

[...] So I'm actually fine to use Unused for new annotations if that's the fastest way to do it, but we shouldn't regress back from the ideal situation.

Ok, I think my last commit satisfies your comment. (now the PR only adds new Unused when it's unused, new object for *args params and changes *args: object to *args: Unused when unused.

I'd say ideally all __exit__ signatures would use the following signature (with the exception of the return type), even those that just use *args in the implementation: [...]
But since that is unwieldy, we can use the best approximation in a current situation.

Given this isn't a "stopgap", "urgent" or "in the meantime" PR. It's just splitting off a would-be bigger PR about semantically using Unused instead of object or _Unused for unused parameters.
Since it's focusing entirely on exit dunder methods. I don't mind changing it to do it "the proper way" everywhere (and even documenting in CONTRIBUTING.md, and/or update Flake8-pyi, any decision about typing exit dunder methods)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@srittau

srittau commented Feb 9, 2023

Copy link
Copy Markdown
Collaborator

Side note: Changing __exit__ to overloads could be disruptive or could even now work. I'd definitely would test it with a small explorative PR first.

Avasam reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Avasam Avasam mentioned this pull request Feb 10, 2023
Update Unused parameters in stubs/ #9704
Merged
@JelleZijlstra

JelleZijlstra commented Feb 22, 2023

Copy link
Copy Markdown
Member

This PR has a lot of conflicts now.

I'd be hesitant to use the overloaded signature @srittau suggests, though it is probably technically correct:

  • There may be rare instances where only some of the arguments are None, though recent changes to CPython make this less and less likely.
  • Using this signature will force users writing subclasses to also write the verbose signature with overloads, which feels like unnecessary busywork.
srittau, AlexWaygood, and Avasam reacted with thumbs up emoji
All reactions
  • 👍 3 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Avasam

Avasam commented Feb 22, 2023

Copy link
Copy Markdown
Collaborator Author

This PR has a lot of conflicts now.

Expected, but not hard to fix :)

About the signature, should I leave it a-is, (can be done in a different PR and/or discussed further). At least get someway there? Or full-on change all signatures?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Avasam added 2 commits February 23, 2023 03:50
@Avasam
Merge branch 'main' of https://github.com/python/typeshed into __exit…
6d966c8
…__-parameters-stubs
@Avasam
.
6c733c3
@github-actions

This comment has been minimized.

Sign in to view
@AlexWaygood

AlexWaygood commented Feb 24, 2023

Copy link
Copy Markdown
Member

My preference would be to generally type __(a)exit__ methods like this, if the method uses *args at runtime:

def __exit__(self, *args: Unused) -> None: ...

And to generally type __(a)exit__ methods like this, if the method includes all three parameters explicitly at runtime:

def __exit__(self, exc_typ: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None

(There will obviously be exceptions to these rules where doing something else makes sense.)


I agree with @srittau that using object or Unused for annotations with the three-argument form could make subclassing unnecessarily difficult, as subclasses will be obliged to use object for annotating those parameters in the subclass.

I also agree with @srittau that a truly precise signature for __(a)exit__ methods would require overloads, but that this would be pretty tedious to do everywhere, and probably wouldn't actually improve type safety significantly. And I agree with @JelleZijlstra that using overloads could also make it harder for people to subclass these classes, so has downsides as well.

srittau and Avasam reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood
Merge branch 'main' into __exit__-parameters-stubs
2363a88
@github-actions

This comment has been minimized.

Sign in to view
AlexWaygood and others added 2 commits February 24, 2023 23:58
@AlexWaygood
Merge branch 'main' into __exit__-parameters-stubs
c75545e
@pre-commit-ci
[pre-commit.ci] auto fixes from pre-commit.com hooks
21af467
@github-actions

This comment has been minimized.

Sign in to view
@Avasam

Avasam commented Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

And to generally type __(a)exit__ methods like this, if the method includes all three parameters explicitly at runtime:

def __exit__(self, exc_typ: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None

I know you said there could be exceptions. But that sounds like something Flake8-PYI could check for and/or add in its existing __(a)exit__ checks, if the exceptions are rare enough.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Avasam added 2 commits February 25, 2023 14:12
@Avasam
Apply PR suggestions
631e414
@Avasam
Merge branch '__exit__-parameters-stubs' of https://github.com/Avasam…
272e50f
…/typeshed into __exit__-parameters-stubs
@AlexWaygood

AlexWaygood commented Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

I know you said there could be exceptions. But that sounds like something Flake8-PYI could check for and/or add in its existing __(a)exit__ checks, if the exceptions are rare enough.

Y036 actually used to be stricter, but Jelle persuaded me to relax it slightly in PyCQA/flake8-pyi#209 after we came across a situation in #7625 (comment) where there was a good case for doing something different. Maybe the exceptions that break the rule are rare enough that we could consider reverting that change... idk, I don't really have a strong opinion on exactly how strict flake8-pyi should be on this ¯\_(ツ)_/¯

Avasam reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Avasam
post merge
249b539
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/caldav/caldav/davclient.pyi
def __enter__(self) -> Self: ...
def __exit__(self, exc_type: object, exc_value: object, traceback: object) -> None: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/influxdb-client/influxdb_client/client/influxdb_client.pyi
def __enter__(self) -> Self: ...
def __exit__(self, exc_type: object, exc_value: object, traceback: object) -> None: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/influxdb-client/influxdb_client/client/influxdb_client_async.pyi
async def __aenter__(self) -> Self: ...
async def __aexit__(self, exc_type: object, exc: object, tb: object) -> None: ...
async def __aexit__(
self, exc_type: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/pika/pika/adapters/blocking_connection.pyi
def __enter__(self) -> Self: ...
def __exit__(self, exc_type: object, value: object, traceback: object) -> None: ...
def __exit__(
self, exc_type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/psycopg2/psycopg2/_psycopg.pyi
def xid(self, format_id, gtrid, bqual) -> Xid: ...
def __enter__(self) -> Self: ...
def __exit__(self, __type: object, __name: object, __tb: object) -> None: ...
def __exit__(self, __type: type[BaseException] | None, __name: BaseException | None, __tb: TracebackType | None) -> None: ...

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/pyserial/serial/threaded/__init__.pyi
def __enter__(self) -> Protocol: ...
def __exit__(self, __exc_type: object, __exc_val: object, __exc_tb: object) -> None: ...
def __exit__(
self, __exc_type: type[BaseException] | None, __exc_val: BaseException | None, __exc_tb: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/redis/redis/asyncio/cluster.pyi
async def __aenter__(self) -> Self: ...
async def __aexit__(self, exc_type: object, exc_value: object, traceback: object) -> None: ...
async def __aexit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it still makes sense.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/tqdm/tqdm/std.pyi
def __enter__(self) -> Self: ...
def __exit__(self, exc_type: object, exc_value: object, traceback: object) -> None: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/redis/redis/asyncio/utils.pyi
async def __aenter__(self) -> Pipeline[_StrType]: ...
async def __aexit__(self, exc_type: object, exc_value: object, traceback: object) -> None: ...
async def __aexit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/redis/redis/asyncio/cluster.pyi
async def __aenter__(self) -> Self: ...
async def __aexit__(self, exc_type: object, exc_value: object, traceback: object) -> None: ...
async def __aexit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Feb 25, 2023
View reviewed changes
Comment thread stubs/redis/redis/asyncio/cluster.pyi
def __enter__(self) -> Self: ...
def __exit__(self, exc_type: object, exc_value: object, traceback: object) -> None: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None

@Avasam Avasam Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This used to be typed with objects. Please validate it's still correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Avasam

Avasam commented Feb 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

[...] I don't really have a strong opinion on exactly how strict flake8-pyi should be on this ¯\_(ツ)_/¯

Me neither, just throwing suggestions :)

Just in case, I flagged all the 3 parameters definitions that used to all be objects.

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

This comment has been minimized.

Sign in to view
AlexWaygood
AlexWaygood reviewed Feb 25, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

All the places you flagged look fine -- just one suggestion

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread stubs/humanfriendly/humanfriendly/testing.pyi Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Avasam @AlexWaygood
Update stubs/humanfriendly/humanfriendly/testing.pyi
7acd4cb
Co-authored-by: Alex Waygood <[email protected]>
AlexWaygood
AlexWaygood approved these changes Feb 25, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Avasam reacted with hooray emoji
All reactions
  • 🎉 1 reaction
@github-actions

github-actions Bot commented Feb 25, 2023

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood
AlexWaygood merged commit 52ec44f into python:main Feb 25, 2023
@Avasam
Avasam deleted the __exit__-parameters-stubs branch February 25, 2023 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@AlexWaygood AlexWaygood AlexWaygood approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@Avasam @srittau @JelleZijlstra @AlexWaygood
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.