[New-bugs-announce] [issue46809] copy.deepcopy can fail with unhelpful diagnostics

Royce Mitchell report at bugs.python.org
Sun Feb 20 10:57:04 EST 2022

New submission from Royce Mitchell <royce3 at gmail.com>:

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:

        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.

components: Library (Lib)
messages: 413594
nosy: remdragon
priority: normal
severity: normal
status: open
title: copy.deepcopy can fail with unhelpful diagnostics
type: enhancement
versions: Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list