-
-
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
Add a typeshed alias to the type accepted by int
constructor
#10707
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Not sure we ever came up with a general stance on this, but it's risky to immediately use this in third-party libraries, because we'll release those immediately, while the |
@JelleZijlstra, it looks to me like this PR is safe from that perspective, since it only adds TODO comments to the third-party stubs; it doesn't make any substantive changes except to the stdlib |
Sorry for that! Should have actually looked at the changes instead of just the list of changed files. |
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 is a pretty complicated alias that seems pretty commonly used in typeshed, so I'm persuaded that it makes sense to have a common definition in _typeshed
.
I'm not wildly keen on the name. It's descriptive, but, idk... feels slightly clunky. But, not sure I can think of anything better. (ConvertibleToInt
? Intable
?)
I dislike that Also why not add an alias for |
I think both In general I like the idea of the alias and I don't care much about the name. If you, the maintainers, see this alias useful, I am happy to go with whatever name you choose
Types are about preventing
Good question, I happen to have stumbled on the int case but not the float one. If the maintainers support this idea I can add it as well (or feel free to add in a separate PR) Some stats (on the current main branch):
|
File | Alias name | Nb of uses |
---|---|---|
stdlib/multiprocessing/util.pyi | no alias, just inline comment | 1 |
stubs/openpyxl/openpyxl/descriptors/base.pyi | _ConvertibleToInt |
674 |
stubs/psycopg2/psycopg2/_psycopg.pyi | _AcceptedByInt |
3 |
stubs/pyasn1/pyasn1/type/univ.pyi | _SizedIntegerable |
3 |
stubs/python-xlib/Xlib/protocol/rq.pyi | _IntNew |
1 |
In typeshed, _typeshed.SupportsTrunc
is only ever used to define this type
float
aliases (3 definitions + float.__new__
):
File | Alias name | Nb of uses |
---|---|---|
stubs/libsass/sass.pyi | _ConvertsToFloat |
5 |
stubs/openpyxl/openpyxl/descriptors/base.pyi | _ConvertibleToFloat |
165 |
stubs/python-xlib/Xlib/ext/xinput.pyi | _Floatable |
3 |
I don't disagree with you or have an issue with the signature containing The original signature did not have a name for the type union, so it does not convey any expectations about the nature of this type, while You could imagine a type union of this name containing only the types that can't fail when passed into When you're given an error in a type checker you only get the name of the type alias, so it should be as unambiguous as possible, in order to be helpful, otherwise writing out the individual types in the union is preferrable, the name should be descriptive enough that you shouldn't have to look up it's definition. But it is a very minor nitpick and unlikely to actually trip up anyone in a harmful way, since what is accepted by
I have a feeling either type alias could be used a lot more often, than they currently are, even within the stdlib, it's just that most of the time when someone sees |
I'm partial to "ConvertibleToInt" given I added the alias to openpyxl, but won't care too much either way. I support the addition of the alias and is something I've raised in the past as well due to Python's type system inability to reference other methods' parameters' type. (for example in typescript you can use that to ensure wrapper methods don't fall out of sync) I think you missed adding a comment to the one in https://github.com/python/typeshed/blob/main/stubs/psycopg2/psycopg2/_psycopg.pyi#L11 though Should an equivalent for float be made as well? I can see 4 stubs using a "convertible to float" alias (libsass, openpyxl, python-xlib and pyscreeze). And made me realize the pyscreeze one is missing To @JelleZijlstra 's point, to do the actual change we'll have to wait at least until the next mypy release. And then there's no way to enforce it for users who don't pin their type dependencies (see #6843 (comment) and typeshed-internal/stub_uploader#96 , at least it's documented in the distribution's description). See #5835 for a possible proper solution |
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Ref #10630 (comment)