Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.1k
stubtest: get better signatures for __init__ of C classes#18259
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
stubtest: get better signatures for __init__ of C classes #18259
Uh oh!
There was an error while loading. Please reload this page.
Conversation
tungol commented Dec 6, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
When an __init__ method has the generic C-class signature, check the underlying class for a better signature.
Uh oh!
There was an error while loading. Please reload this page.
tungol commented Dec 6, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Now handles when Technically the implementation of the lookup makes the assumption that the class name is only one element in the |
Uh oh!
There was an error while loading. Please reload this page.
tungol commented Dec 7, 2024
While investigating what to do about The meaning of which is that this check should not attempt to get a better signature by working around getting passed from __future__ importannotationsfromzoneinfoimportZoneInfoKEY="America/Los_Angeles"classFoo(ZoneInfo): def__init__(self) ->None: super().__init__(KEY) def__new__(cls) ->Foo: returnsuper().__new__(cls) Foo()While this is correct at runtime but fails type check: from __future__ importannotationsfromzoneinfoimportZoneInfoKEY="America/Los_Angeles"classFoo(ZoneInfo): def__init__(self) ->None: super().__init__() # error: Missing positional argument "key" in call to "__init__" of "ZoneInfo" [call-arg]def__new__(cls) ->Foo: returnsuper().__new__(cls, KEY) # error: Too many arguments for "__new__" of "object" [call-arg]Foo()This is good, actually, because it means that this check is a lot simpler, and I don't have to worry about duplicating |
tungol commented Dec 7, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
I'm working through the errors flagged this way. A couple things that are coming up:
a function like @overloaddef__init__(self) ->None: ... @overloaddef__init__(self, arg, /) ->None: ...And it's causing stubtest to prompt to make the first overload positional-only, and normally typeshed wouldn't do that.
We also have overloads where the runtime definition is This is from Inferred signature with multiple copies of |
tungol commented Dec 7, 2024
I worked out the issues. "self" was special cased in two places, and I needed to add special-casing in the same way for "cls" to deal with the sudden glut of complicated overloaded I pushed up the fixes that this MR surfaced to python/typeshed#13211 but pyright is unhappy with some of them, so I need to work through that still. |
tungol commented Dec 7, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
I think this is basically good now. See python/typeshed#13211 for the changes that this suggests for the stdlib stubs. Some tests would be good still. |
tungol commented Dec 8, 2024
Having looked at how stubtest tests are run, I'm not sure if I can create a test for this? I can't see a way to mimic a C-implemented class as a test case in |
13fa6c3 into python:masterUh oh!
There was an error while loading. Please reload this page.
JelleZijlstra commented Aug 22, 2025
Hm this seems to be causing some issues in typeshed, I got some new errors in python/typeshed#14599 when I pinned mypy to the commit from this PR. I pinned it to an earlier commit for now so that PR can focus on disjoint base changes, but we'll have to fix this. |
JelleZijlstra commented Aug 22, 2025
The runs with problems were in https://github.com/python/typeshed/actions/runs/17144606835/job/48638239781, e.g. the complaint about |
tungol commented Aug 22, 2025
This shows on 3.9 and 3.10, where _random.Random uses
I don't have a linux machine handy (which is also why I didn't catch this on my earlier typeshed MR), but it looks like select.epoll also uses |
tungol commented Aug 22, 2025
These appear on 3.13 and 3.14; string was added by python/cpython@c6e63d9#diff-c7440bf2e8c27b17a856c469b7839a7c0bfd93008e3c4f553616df6f1c33d854L678 There's a corresponding error for |
tungol commented Aug 22, 2025
on 3.14:
|
a few issues exposed after python/mypy#18259 was merged
a few issues exposed after python/mypy#18259 was merged
When an
__init__method has the generic C-class signature, check the underlying class for a better signature.I was looking at
asyncio.futures.Future.__init__and wanted to take advantage of the better__text_signature__onasyncio.futures.Future.The upside is that this clears several allowlist entries and surfaced several other cases where typeshed is currently incorrect, with no false positives.