This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: copy.deepcopy can fail with unhelpful diagnostics
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: remdragon
Priority: normal Keywords:

Created on 2022-02-20 15:57 by remdragon, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg413594 - (view) Author: Royce Mitchell (remdragon) Date: 2022-02-20 15:57
Dear devs,

I have a small change request to make to a built-in Python file.

I'm currently running python 3.9.5

The file is copy.py

I would like to propose changing line 264 (in _reconstruct) from this:

    y = func(*args)

to something like this:

    try:
        y = func(*args)
    except TypeError as e:
        raise TypeError(
            f'calling {func.__module__}.{func.__qualname__}: {e.args[0]}', *e.args[1:]
        ).with_traceback(e.__traceback__) from None

All the change does it inject the module and qualified-name of the function trying to be
created onto the front-end of the error.

It makes this:

TypeError: __init__() missing 1 required positional argument: 'delta'

into this:

TypeError: calling datetime.datetime: calling mytz.Tzoffset: __init__() missing 1 required positional argument: 'delta'

Here's a summary of the situation that led to this difficulty:

I have a project that is a couple years old and I'm no longer intimately aware of every single
thing the program is doing. I went to make some enhancements and noticed the unit tests
hadn't been touched since early in the project and decided I wanted to start using it.

I got stuck trying to prettyprint an object and getting a TypeError from the line above
because it was trying to call a function but was missing a required argument.

The traceback was unhelpful because I didn't know what object it was trying to copy,
which was very complicated with lots of data and sub-objects.

It turns out that a dataclass (named TransDetail) I was trying to prettyprint
had a list of another dataclass (named Billing) which had a datetime.datetime object
with a custom tzinfo object that I had never tried to deepcopy before.
(The custom tzinfo object was adapted from examples on StackOverflow)

Trying to google the issue, I found many others experiencing the same problem.

The fix was to define a default datetime.timedelta value for that custom tzinfo object,
but I had to make the changes to copy.py in order to efficiently figure out that this
was the problem.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90965
2022-02-20 15:57:04remdragoncreate