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 — Fix annotations for `builtins.reversed.__new__` by jorenham · Pull Request #11646 · python/typeshed

Conversation

@jorenham

@jorenham jorenham commented Mar 21, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

fixes #11645


Deferred due to a mypy bug: python/mypy#15182

Sorry, something went wrong.

Uh oh!

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

bluenote10 reacted with eyes emoji
All reactions
  • 👀 1 reaction
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@srittau

srittau commented Mar 23, 2024

Copy link
Copy Markdown
Collaborator

It seems that mypy has problems with the first overload, but I'm not sure why. Everything seems correct to me and pyright seems to agree. For now, I would suggest to split this PR in two, one PR for the second overload and a separate PR for the first one. This should allow us to get part of the changes into typeshed soon, and will make it easier to debug the mypy problem.

jorenham reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@jorenham

jorenham commented Mar 25, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

@srittau I have tried many different approaches, but none of them seem to be accepted by mypy. As you said, pyright has no problem with this. I can't draw any other conclusions than this being a mypy bug, and it's easy to reproduce:

https://mypy-play.net/?mypy=latest&python=3.12&gist=0c754a7826a80f50ced671c26d891950&flags=strict

The expected behavior can be illustrated by pyright, which works as expected and doesn't require the type: ignore[misc]:
https://pyright-play.net/?strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAqiApgGoCGIAUFQVALyEkUgAUA5AewJQ1UDGAG3IBnEVAByYGAEEiCYgC4qUVVAAmxYFAD6OlMQDue1kJEAaKJTSLC3KAFoAfIWVr3UEMRgBXECisQNBUPVRDQqi8AN2JyQR14BVYpWXliVgAWACZubiA

I wasn't able to find anything on https://typing.readthedocs.io/ that would restrict the type of the return value from a (non-meta) class' __new__ method.
But for some reason, mypy blindly assumes that this is always a covariant type that is bound to the class at hand (i.e. __new__: (cls, ...) -> Self with Self somehow being covariant). Any annotations that suggest otherwise result in a type error in the misc category. But even if that error is explicitly suppressed, mypy will implicitly ignore it.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jorenham jorenham mentioned this pull request Mar 25, 2024
Honor return type of __new__ even if not a subclass python/mypy#15182
Closed
@srittau srittau added the status: deferred Issue or PR deferred until some precondition is fixed label Mar 25, 2024
@srittau

srittau commented Mar 25, 2024

Copy link
Copy Markdown
Collaborator

I've marked this PR as deferred for now due to the mypy bug.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jorenham

jorenham commented Mar 25, 2024

Copy link
Copy Markdown
Contributor Author

@srittau I don't quite follow.

Currently, all typecheckers will reject a reversed call, if its argument isn't iterable or a sequence, even if implements __reversed__. The reason why the typetests did not complain here, is because it simply wasn't tested.

This PR makes it so that (presumably) only mypy will incorrectly reject it, due to a bug. But the new tests that I included only fail because of mypy's bug.

So I don't see how deferring this would be any better than mergin this. I believe it's reasonable to ignore mypy's false negatives, since they're directly caused by a known bug.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jorenham jorenham mentioned this pull request Mar 25, 2024
Believe more __new__ return types python/mypy#16020
Closed
@srittau

srittau commented Mar 25, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Collaborator

The mypy primer output above shows the real-world impact this change has on a selected number of packages. Each green line is a new mypy warning. As you can see, this change unfortunately leads to an excessive amount of new errors. As far as I understand, many reversed(...) calls now return ...[Never], which leads to all the errors above.

jorenham and AlexWaygood 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.

@jorenham

jorenham commented Mar 25, 2024

Copy link
Copy Markdown
Contributor Author

@srittau ah ok I see. I blindly assumed that "green is good" for some reason 🤷🏻

All reactions

Sorry, something went wrong.

Uh oh!

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

@jorenham jorenham mentioned this pull request Apr 13, 2024
infer types of dunders DetachHead/basedpyright#288
Open
@jorenham

jorenham commented Apr 17, 2024

Copy link
Copy Markdown
Contributor Author

According to the updated typing spec, it is indeed allowed for __new__ to return some non Self type:
https://typing.readthedocs.io/en/latest/spec/constructors.html#new-method

All reactions

Sorry, something went wrong.

Uh oh!

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

@jorenham
jorenham force-pushed the re-versed branch from 80043b7 to 10a8f5c Compare April 17, 2024 18:56
@github-actions

This comment has been minimized.

Sign in to view
@jorenham jorenham mentioned this pull request Jul 10, 2024
Accept __getitem__-iterables in builtins.enumerate #12294
Closed
@jorenham jorenham mentioned this pull request Dec 28, 2024
Shouldn't the return type of __reversed__ be an Iterable[T] instead of Iterator[T]? #13218
Open
@bluenote10

bluenote10 commented Dec 28, 2024

Copy link
Copy Markdown

Note that the same was attempted in #13269 so the discussion there may apply here as well.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jorenham jorenham mentioned this pull request Jan 5, 2025
Issues related to numpy python/mypy#18343
Open
@srittau

srittau commented Feb 28, 2025

Copy link
Copy Markdown
Collaborator

At the moment, this doesn't look promising. Unless someone has another good idea, I suggest we close this PR – at least for now.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jorenham

jorenham commented Feb 28, 2025

Copy link
Copy Markdown
Contributor Author

At the moment, this doesn't look promising. Unless someone has another good idea, I suggest we close this PR – at least for now.

Yea, not much movement on the mypy front unfortunately...

Maybe it could help to avoid duplicate PR's if we keep this open? It's also fine to close it if you want, though; whatever floats for boat 🤷🏻.

All reactions

Sorry, something went wrong.

Uh oh!

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

@srittau
srittau marked this pull request as draft February 28, 2025 11:16
@srittau

srittau commented Feb 28, 2025

Copy link
Copy Markdown
Collaborator

I see that it's already marked as deferred. I've also marked it as draft, just so it doesn't stand out as much in the open PRs list.

jorenham 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.

@jorenham
jorenham force-pushed the re-versed branch from 10a8f5c to a2c717d Compare February 28, 2025 11:18
@jorenham

jorenham commented Feb 28, 2025

Copy link
Copy Markdown
Contributor Author

I also rebased onto main, pro forma I guess.

srittau 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
@tungol

tungol commented May 27, 2026

Copy link
Copy Markdown
Contributor

Mypy python/mypy#21441 appear to fix the blocker. Once that makes it into a release, this can be revisited.

jorenham and tmke8 reacted with hooray emoji
All reactions
  • 🎉 2 reactions

Sorry, something went wrong.

Uh oh!

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

jorenham and others added 3 commits July 23, 2026 01:28
@jorenham
stricter and more tests for builtins.reversed
9c678d5
@jorenham
fix builtins.reversed
1137d3a
@pre-commit-ci @jorenham
[pre-commit.ci] auto fixes from pre-commit.com hooks
274f2a8
@jorenham
jorenham force-pushed the re-versed branch from a2c717d to 274f2a8 Compare July 22, 2026 23:32
jorenham and others added 2 commits July 23, 2026 01:35
@jorenham
Restore # type: ignore[misc] on reversed.__new__
d5c645a
@pre-commit-ci
[pre-commit.ci] auto fixes from pre-commit.com hooks
6dd7d7f
@jorenham

jorenham commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Mypy python/mypy#21441 appear to fix the blocker. Once that makes it into a release, this can be revisited.

python/mypy#21441 was fixed in mypy 2.2.0 :)

All reactions

Sorry, something went wrong.

Uh oh!

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

@jorenham

jorenham commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

those blank lines were inserted by pre-commit for some reason 🤷

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

comtypes (https://github.com/enthought/comtypes)
- comtypes/server/register.py:226: error: Incompatible types in assignment (expression has type "Iterator[tuple[int, str]]", variable has type "list[tuple[int, str]]")  [assignment]
+ comtypes/server/register.py:226: error: Incompatible types in assignment (expression has type "reversed[tuple[int, str]]", variable has type "list[tuple[int, str]]")  [assignment]

discord.py (https://github.com/Rapptz/discord.py)
- discord/abc.py:1815: error: Incompatible types in assignment (expression has type "Iterator[MessagePin]", variable has type "list[MessagePin]")  [assignment]
+ discord/abc.py:1815: error: Incompatible types in assignment (expression has type "reversed[MessagePin]", variable has type "list[MessagePin]")  [assignment]

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/monkeypatch.py:417: error: Unused "type: ignore" comment  [unused-ignore]
+ src/_pytest/monkeypatch.py:422: error: Unused "type: ignore" comment  [unused-ignore]

jinja (https://github.com/pallets/jinja)
+ src/jinja2/utils.py:570: error: Returning Any from function declared to return "Iterator[Any]"  [no-any-return]
jorenham reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

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

@jorenham
jorenham marked this pull request as ready for review July 22, 2026 23:45
@srittau srittau removed the status: deferred Issue or PR deferred until some precondition is fixed label Jul 23, 2026
srittau
srittau approved these changes Jul 23, 2026
View reviewed changes

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

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.

jorenham reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@srittau
srittau merged commit 7f640ab into python:main Jul 23, 2026
72 checks passed

Uh oh!

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

@jorenham
jorenham deleted the re-versed branch July 23, 2026 09:23
@cdce8p cdce8p mentioned this pull request Aug 2, 2026
Sync typeshed python/mypy#21799
Merged
@hauntsaninja hauntsaninja mentioned this pull request Oct 7, 2026
[mypy 2.4 regression] Incorrect type narrowing for reversed list of tuples python/mypy#22174
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@srittau srittau srittau 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.

incorrect annotations for builtins.reversed

Uh oh!

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

4 participants

@jorenham @srittau @bluenote10 @tungol
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 01402775 — 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.