Subclassing threading Lock/RLock: better Exception messages

Open
#94,077 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python

Research direction

Start with the minimal Python reproduction in the issue and inspect the threading documentation entry for Lock and RLock. Determine how unsupported subclassing is currently exposed, then make the behavior clearer through a friendlier error message and documentation stating the subclassing limitation. Re-run both subclass definitions to verify the resulting messages.

Written by the indexing model from the issue text.

Description

stdlib type-bug

Bug report

Please see the below minimal repro:

from threading import Lock, RLock

class SubclassLock(Lock):
    pass

class SubclassRLock(RLock):
    pass

The subclass of Lock emits:

Traceback (most recent call last):
  File "/path/to/file.py", line 3, in <module>
    class SubclassLock(Lock):
TypeError: cannot create 'builtin_function_or_method' instances

The subclass of RLock emits:

Traceback (most recent call last):
  File "/path/to/file.py", line 6, in <module>
    class SubclassRLock(RLock):
TypeError: function() argument 'code' must be code, not str

Both of these messages are relatively cryptic. I think if Python doesn't support subclassing Lock/RLock, it should be documented in threading, and this error message should be more friendly/clear.

Your environment

  • CPython versions tested on: 3.10.3
  • Operating system and architecture: macOS Big Sur 11.5.1
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.