-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
base: master
Are you sure you want to change the base?
Conversation
When an __init__ method has the generic C-class signature, check the underlying class for a better signature.
Now handles when Technically the implementation of the lookup makes the assumption that the class name is only one element in the |
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__ import annotations
from zoneinfo import ZoneInfo
KEY = "America/Los_Angeles"
class Foo(ZoneInfo):
def __init__(self) -> None:
super().__init__(KEY)
def __new__(cls) -> Foo:
return super().__new__(cls)
Foo()
While this is correct at runtime but fails type check: from __future__ import annotations
from zoneinfo import ZoneInfo
KEY = "America/Los_Angeles"
class Foo(ZoneInfo):
def __init__(self) -> None:
super().__init__() # error: Missing positional argument "key" in call to "__init__" of "ZoneInfo" [call-arg]
def __new__(cls) -> Foo:
return super().__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 |
I'm working through the errors flagged this way. A couple things that are coming up:
a function like @overload
def __init__(self) -> None: ...
@overload
def __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 |
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. |
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. |
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 |
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.