Skip to content

test_typing: test_many_weakrefs takes many seconds #110178

Closed
@AlexWaygood

Description

@AlexWaygood

Bug report

Bug description:

TypeVarTests.test_many_weakrefs in test_typing takes far longer than any other test in test_typing if you're using a debug build of CPython, slowing down test_typing considerably:

>python Lib/test/test_typing.py --durations 5
Running Debug|x64 interpreter...
.............................................................................................................................................................................................................................................s.....................................................................................................................................................................................................................................................................................................................................................................
Slowest test durations
----------------------------------------------------------------------
5.090s     test_many_weakrefs (__main__.TypeVarTests.test_many_weakrefs)
0.206s     test_variadic_parameters (__main__.GenericAliasSubstitutionTests.test_variadic_parameters)
0.082s     test_two_parameters (__main__.GenericAliasSubstitutionTests.test_two_parameters)
0.057s     test_special_attrs (__main__.SpecialAttrsTests.test_special_attrs)
0.044s     test_var_substitution (__main__.TypeVarTupleTests.test_var_substitution)

----------------------------------------------------------------------
Ran 594 tests in 6.714s

OK (skipped=1)

The test was added in #108517, as a regression test for #108295.

The slowdown caused by this test is much less pronounced if you use a PGO-optimised non-debug build, but the test still takes much longer than any other test in test_typing on my machine:

>python Lib/test/test_typing.py --durations 5
Running PGUpdate|x64 interpreter...
.............................................................................................................................................................................................................................................s.....................................................................................................................................................................................................................................................................................................................................................................
Slowest test durations
----------------------------------------------------------------------
0.379s     test_many_weakrefs (__main__.TypeVarTests.test_many_weakrefs)
0.026s     test_variadic_parameters (__main__.GenericAliasSubstitutionTests.test_variadic_parameters)
0.014s     test_two_parameters (__main__.GenericAliasSubstitutionTests.test_two_parameters)
0.007s     test_etree (__main__.UnionTests.test_etree)
0.004s     test_var_substitution (__main__.TypeVarTupleTests.test_var_substitution)

----------------------------------------------------------------------
Ran 594 tests in 0.610s

OK (skipped=1)

The test currently attempts to create 100,000 TypeVar weakrefs, 100,000 ParamSpec weakrefs and 100,000 TypeVarTuple weakrefs:

def test_many_weakrefs(self):
# gh-108295: this used to segfault
for cls in (ParamSpec, TypeVarTuple, TypeVar):
with self.subTest(cls=cls):
vals = weakref.WeakValueDictionary()
for x in range(100000):
vals[x] = cls(str(x))
del vals

@JelleZijlstra, reckon we could maybe make that number a little smaller? :)

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

Labels

testsTests in the Lib/test dirtopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions