-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
gh-131540: Improve type checking error for args in threading.Thread objects
#131582
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
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
args in threading.Thread objects
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.
It's not terribly uncommon to arg-splat a string into a function, so I don't think it's safe to change that for threads without a deprecation period. Please change this to solely a check for __iter__.
I'm not opposed to deprecating use of str/bytes for a better error message in the future, but I don't know what kinds of use-cases people have for that right now. If you want to go that route, open a thread on DPO (under the Core Development category) so we can get some more opinions.
|
Thank you for the suggestion, but I'm going to decline for several reasons. First, this is a breaking change. Any sequence is automatically iterable even if it doesn't define Perhaps suggest this as a rule for one of the Python linters. They are better at identifying code that smells bad even though it might only be valid in a rare case. |
gh-131540 : Improve type checking error for args in threading.Thread objects
Issue: python/cpython#131540
Original Proposal
This change adds a validation check in the `threading.Thread` constructor to ensure that the args parameter is either a tuple or a list. This helps avoid confusion when a string or other non-iterable is accidentally passed.Current behaviour:
which is raised when the thread is started.
New behaviour:
Which is raised in the Thread constructor.
Compatibility note:
This introduces a stricter check and may break code that intentionally relies on passing a string or bytes as an iterable. However, this usage is not documented and it is unclear if people purposefully do this.
New Proposal
This change adds early validation in
threading.Thread.__init__to ensure that theargsparameter is an iterable. If a non-iterable value (such as anintorNone) is passed, aTypeErroris now raised at construction time instead of at thread start.This improves consistency with how other type errors are handled in Python and provides earlier feedback for developers, especially when the error would otherwise occur asynchronously at thread start.
Motivation
Previously, non-iterable values passed to
argswould raise an error only whenthread.start()was called:With this change, the error is raised immediately in
__init__:What this PR changes:
Thread.__init__to ensure args has an__iter__attributeTypeErrorimmediately if notstrorbytes(for compatibility)-Non-iterables raising
TypeError(e.g.,int,None,float)-Valid iterables (e.g.,
tuple,list, and a custom iterable) being acceptedLet me know if anything else is needed.