-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Should stubs contain default values? #8988
Comments
I agree, a couple of small comments:
Other values that we could accept would include floats and types (e.g., for params like
It should be easy for stubtest to check defaults for correctness on Python functions (it's in the signature). For C functions, it's not necessarily possible to infer the default though, and indeed there may not be a default in a useful sense. What other tools would need updating? I think we'd have to change flake8-pyi and hopefully nothing else. |
It would be useful - but not necessary - to update stubgen as well. |
I like the idea of adding simple default values to stubs. I agree that it's very useful in IDEs, and I think it'll be easier to maintain than adding docstrings to stubs. The
Pyright also has a lint that enforces only using |
I have some concerns about it. First of all, this will require more branching just for the sake of default values if it is different for different python versions. Or we can show incorrect default values for this specific python version. Secondly, default values are not a part of the signature. So, they can be changed in bugfix releases. It will make it much harder to maintain. Next, stubs are even harder to maintain with default values. Moreover, there are quite ugly defaults values like I think that IDEs can just use |
I like @hauntsaninja's idea of only including "simple" defaults, e.g. "anything that's valid to go in a |
I'll honestly expect 95% of all default values to be |
That will happen sometimes, but my sense is that it's not common.
In theory, sure, but I'd be surprised if that happens with any frequency. If maintainability concerns like these do come up frequently, we can revisit our decision, but I'd be surprised if it is a major issue. The stdlib doesn't change its defaults that much, and for third-party libraries we only support one version at a time. |
I think that IDEs can just use inspect to get the function and its
defaults. It is much easier and it will always show correct results.
Easy in Jupyter with a running kernel; not as simple as it sounds in other
IDEs that may not be written in Python, need to be responsive, and don't
want to do a bunch of indexing up front.
…On Thu, Oct 27, 2022 at 8:22 AM Jelle Zijlstra ***@***.***> wrote:
First of all, this will require more branching just for the sake of
default values if it is different for different python versions.
That will happen sometimes, but my sense is that it's not common.
Secondly, default values are not a part of the signature. So, they can be
changed in bugfix releases. It will make it much harder to maintain.
In theory, sure, but I'd be surprised if that happens with any frequency.
If maintainability concerns like these do come up frequently, we can
revisit our decision, but I'd be surprised if it is a major issue. The
stdlib doesn't change its defaults that much, and for third-party libraries
we only support one version at a time.
—
Reply to this email directly, view it on GitHub
<#8988 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAVCPCAEIXNELUUVHAVZ2WLWFKM47ANCNFSM6AAAAAAROQJKTU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
IDEs will also probably want docstrings. |
Docstrings provide significantly more maintainability challenges. It's worth discussing whether it's worth changing our stance on docstrings too, but let's do that in a separate issue. |
Please don't discuss docstrings here. We have the following issue for that discussion: |
This comment was marked as off-topic.
This comment was marked as off-topic.
As a user of both libraries typed through Typeshed and inline-typed ones, I've noticed that's information lost in these third-party stubs, which is unfortunate. I really like when my IDE shows me default values of function parameters.
I share the same feeling for constants. (Relates to #7258 ?) Similarly to my comment here (#4881 (comment)) and as gramster mentionned: Is this something editors could inspect when they come across What's a good compromise between maintainability and not hiding information that would otherwise be available? On the idea of only doing it for simple types: stubtest could help validate and maintain accuracy. By knowing when a default is both known and considered "simple". |
It seems like there is agreement among the maintainers here, so I think we can move forward with changing our policy and allowing defaults. I wrote a little tool to help infer defaults from the runtime: https://github.com/JelleZijlstra/stubdefaulter. I can also work on adding support to stubtest for checking that defaults are right. |
https://github.com/microsoft/pyright/releases/tag/1.1.284
|
We've had great progress on this recently:
TODO, if anybody feels like taking these on:
|
Do you wanna use this issue to track running the codemod on existing third-party stubs? |
My tool has a couple of problems, opened issues in https://github.com/JelleZijlstra/stubdefaulter. Hopefully I'll have time to work on those soon. |
#9501 added a couple of defaults that are set to typeshed/stdlib/collections/__init__.pyi Line 166 in 7e40d70
The runtime source for this method is here: https://github.com/python/cpython/blob/3847a6c64b96bb2cb93be394a590d4df2c35e876/Lib/collections/__init__.py#L1439 The precise value of We could also consider adding a lint to flake8-pyi that forbids numeric defaults >6 digits (we can bikeshed the precise number of digits we want to permit) — thoughts? |
Alternatively we could add some special-casing to flake8-pyi around def count(self, sub: str | UserString, start: int = 0, end: int = sys.maxsize) -> int: ... |
Sorry for missing that! For now let's just remove those defaults.
|
In general I'm wary of allowing defaults that can't be easily verified by stubtest. I'd want most things that can't be easily verified by stubtest to be disallowed by flake8-pyi. But I'd be happy to special-case a small number of |
The default value here is set to typeshed/stdlib/multiprocessing/heap.pyi Line 30 in 7e40d70
|
Here's a few funny ones I found using stubtest with mypy Line 41 in 7e40d70
Line 61 in 7e40d70
The first one is The second is dynamically determined using a function call at runtime: https://github.com/python/cpython/blob/3847a6c64b96bb2cb93be394a590d4df2c35e876/Lib/pydoc.py#L495 |
I just found a nice side-effect of default parameter values: def foo(bar=None):...
# Is now equivalent to
def foo(bar: Incomplete | None = None): ... But the former keeps |
Continuing work towards #8988. The first five commits were created using stubdefaulter on various Python versions; the following commits were all created manually by me to fix various problems. The main things this adds that weren't present in #9501 are: - Defaults in Windows-only modules and Windows-only branches (because I'm running a Windows machine) - Defaults in non-py311 branches - Defaults for float parameters - Defaults for overloads
Continuing work towards python#8988. The first five commits were created using stubdefaulter on various Python versions; the following commits were all created manually by me to fix various problems. The main things this adds that weren't present in python#9501 are: - Defaults in Windows-only modules and Windows-only branches (because I'm running a Windows machine) - Defaults in non-py311 branches - Defaults for float parameters - Defaults for overloads
Is there anything left to do or discuss here? Seems like we're been adding defaults to parameters for a while. |
Alex's TODO list above says stubgen needs updating, but that's not something we can fix in this repo. |
True, but as far as typeshed's concerned, I think #10127 does the same until then 😃 |
This has come up recently, for similar reasons as #4881 and discussed a little there
From @gramster
I'm overall fine with this, though I'd like to see the following:
...
at author's discretion (useful in overloads or cases where value doesn't actually matter or to avoid awfulx: Literal["x"] = "x"
things)The case for:
The case against:
...
and terseness may be more valuable than the additional informationA similar thing could apply to constants.
The text was updated successfully, but these errors were encountered: