Skip to content

Test deepcopy() memo behavior without inspecting internals #142254

@Bobronium

Description

@Bobronium

Tests enhancement

Currently there are two tests in Lib/test/test_copy.py that inspect memo's internal structure directly:

test_deepcopy_keepalive:

self.assertIs(memo[id(memo)][0], x)

test_deepcopy_dont_memo_immutable:
# There's the entry for the new list, and the keep alive.
self.assertEqual(len(memo), 2)

This constrains implementations to maintain that specific structure, creating friction for both improvements to Lib/copy.py and alternative implementations aiming to pass the test suite.

Docs explicitly state:

object.__deepcopy__(self, memo)

..., it should call the deepcopy() function with the component as first argument and the memo dictionary as second argument. The memo dictionary should be treated as an opaque object.

Proposal

I propose rewriting these tests to verify observable behavior rather than internal structure.

This will make tests somewhat less straightforward, but will solidify what deepcopy actually guarantees in terms of externally observable effects, without relying on implementation details.

No changes to Lib/copy.py are proposed.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions