Skip to content

Commit a365c4b

Browse files
authored
Relax all getrefcount tests to allow lower numbers (#854)
1 parent 0f347bf commit a365c4b

File tree

5 files changed

+34
-34
lines changed

5 files changed

+34
-34
lines changed

tests/test_common.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,14 +1370,14 @@ class Ex(Struct, Generic[T]):
13701370
dec = proto.Decoder(typ)
13711371
info = typ.__msgspec_cache__
13721372
assert info is not None
1373-
assert sys.getrefcount(info) == 4 # info + attr + decoder + func call
1373+
assert sys.getrefcount(info) <= 4 # info + attr + decoder + func call
13741374
dec2 = proto.Decoder(typ)
13751375
assert typ.__msgspec_cache__ is info
1376-
assert sys.getrefcount(info) == 5
1376+
assert sys.getrefcount(info) <= 5
13771377

13781378
del dec
13791379
del dec2
1380-
assert sys.getrefcount(info) == 3
1380+
assert sys.getrefcount(info) <= 3
13811381

13821382
def test_generic_struct_invalid_types_not_cached(self, proto):
13831383
class Ex(Struct, Generic[T]):
@@ -1545,7 +1545,7 @@ class Ex2(Struct, array_like=array_like, tag=True):
15451545
res = proto.decode(buf, type=typ)
15461546
assert res == msg
15471547
assert count == 2 # 1 for Ex(), 1 for decode
1548-
assert sys.getrefcount(singleton) == 2 # 1 for ref, 1 for call
1548+
assert sys.getrefcount(singleton) <= 2 # 1 for ref, 1 for call
15491549

15501550
@pytest.mark.parametrize("array_like", [False, True])
15511551
@pytest.mark.parametrize("union", [False, True])
@@ -1606,14 +1606,14 @@ class Ex(Generic[T]):
16061606
dec = proto.Decoder(typ)
16071607
info = typ.__msgspec_cache__
16081608
assert info is not None
1609-
assert sys.getrefcount(info) == 4 # info + attr + decoder + func call
1609+
assert sys.getrefcount(info) <= 4 # info + attr + decoder + func call
16101610
dec2 = proto.Decoder(typ)
16111611
assert typ.__msgspec_cache__ is info
1612-
assert sys.getrefcount(info) == 5
1612+
assert sys.getrefcount(info) <= 5
16131613

16141614
del dec
16151615
del dec2
1616-
assert sys.getrefcount(info) == 3
1616+
assert sys.getrefcount(info) <= 3
16171617

16181618
def test_generic_invalid_types_not_cached(self, decorator, proto):
16191619
@decorator
@@ -2197,14 +2197,14 @@ class Ex(TypedDict, Generic[T]):
21972197
dec = proto.Decoder(typ)
21982198
info = typ.__msgspec_cache__
21992199
assert info is not None
2200-
assert sys.getrefcount(info) == 4 # info + attr + decoder + func call
2200+
assert sys.getrefcount(info) <= 4 # info + attr + decoder + func call
22012201
dec2 = proto.Decoder(typ)
22022202
assert typ.__msgspec_cache__ is info
2203-
assert sys.getrefcount(info) == 5
2203+
assert sys.getrefcount(info) <= 5
22042204

22052205
del dec
22062206
del dec2
2207-
assert sys.getrefcount(info) == 3
2207+
assert sys.getrefcount(info) <= 3
22082208

22092209
def test_generic_typeddict_invalid_types_not_cached(self, proto):
22102210
TypedDict = pytest.importorskip("typing_extensions").TypedDict
@@ -2416,14 +2416,14 @@ class Ex(NamedTuple, Generic[T]):
24162416
dec = proto.Decoder(typ)
24172417
info = typ.__msgspec_cache__
24182418
assert info is not None
2419-
assert sys.getrefcount(info) == 4 # info + attr + decoder + func call
2419+
assert sys.getrefcount(info) <= 4 # info + attr + decoder + func call
24202420
dec2 = proto.Decoder(typ)
24212421
assert typ.__msgspec_cache__ is info
2422-
assert sys.getrefcount(info) == 5
2422+
assert sys.getrefcount(info) <= 5
24232423

24242424
del dec
24252425
del dec2
2426-
assert sys.getrefcount(info) == 3
2426+
assert sys.getrefcount(info) <= 3
24272427

24282428
def test_generic_namedtuple_invalid_types_not_cached(self, proto):
24292429
NamedTuple = pytest.importorskip("typing_extensions").NamedTuple

tests/test_convert.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class Custom:
220220
x = Custom()
221221
res = convert(x, Any)
222222
assert res is x
223-
assert sys.getrefcount(x) == 3 # x + res + 1
223+
assert sys.getrefcount(x) <= 3 # x + res + 1
224224

225225
def test_custom_input_type_works_with_custom(self):
226226
class Custom:
@@ -229,7 +229,7 @@ class Custom:
229229
x = Custom()
230230
res = convert(x, Custom)
231231
assert res is x
232-
assert sys.getrefcount(x) == 3 # x + res + 1
232+
assert sys.getrefcount(x) <= 3 # x + res + 1
233233

234234
def test_custom_input_type_works_with_dec_hook(self):
235235
class Custom:
@@ -247,8 +247,8 @@ def dec_hook(typ, x):
247247
x = Custom()
248248
res = convert(x, Custom2, dec_hook=dec_hook)
249249
assert isinstance(res, Custom2)
250-
assert sys.getrefcount(res) == 2 # res + 1
251-
assert sys.getrefcount(x) == 2 # x + 1
250+
assert sys.getrefcount(res) <= 2 # res + 1
251+
assert sys.getrefcount(x) <= 2 # x + 1
252252

253253
def test_unsupported_output_type(self):
254254
with pytest.raises(TypeError, match="more than one array-like"):
@@ -397,7 +397,7 @@ class MyInt(int):
397397
x = MyInt(100)
398398
sol = convert(x, MyInt)
399399
assert sol is x
400-
assert sys.getrefcount(x) == 3 # x + sol + 1
400+
assert sys.getrefcount(x) <= 3 # x + sol + 1
401401

402402

403403
class TestFloat:
@@ -535,10 +535,10 @@ class MyBytes(bytes):
535535

536536
del sol
537537

538-
assert sys.getrefcount(msg) == 2 # msg + 1
538+
assert sys.getrefcount(msg) <= 2 # msg + 1
539539
sol = convert(msg, MyBytes)
540540
assert sol is msg
541-
assert sys.getrefcount(msg) == 3 # msg + sol + 1
541+
assert sys.getrefcount(msg) <= 3 # msg + sol + 1
542542

543543

544544
class TestDateTime:
@@ -828,7 +828,7 @@ class Ex(enum.IntEnum):
828828

829829
msg = MyInt(1)
830830
assert convert(msg, Ex) is Ex.x
831-
assert sys.getrefcount(msg) == 2 # msg + 1
831+
assert sys.getrefcount(msg) <= 2 # msg + 1
832832
assert convert(MyInt(2), Ex) is Ex.y
833833

834834
def test_enum_missing(self):
@@ -2223,7 +2223,7 @@ class Ex2(Struct, array_like=array_like, tag=True):
22232223
res = convert(msg, type=typ, from_attributes=from_attributes)
22242224
assert type(res) is Ex
22252225
assert called
2226-
assert sys.getrefcount(singleton) == 2 # 1 for ref, 1 for call
2226+
assert sys.getrefcount(singleton) <= 2 # 1 for ref, 1 for call
22272227

22282228
@pytest.mark.parametrize("union", [False, True])
22292229
@pytest.mark.parametrize("exc_class", [ValueError, TypeError, OSError])

tests/test_json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ def test_decode_timezone_cache(self):
898898
tz2 = msgspec.json.decode(msg, type=datetime.datetime).tzinfo
899899
assert tz is tz2
900900
del tz2
901-
assert sys.getrefcount(tz) == 3 # 1 tz, 1 cache, 1 func call
901+
assert sys.getrefcount(tz) <= 3 # 1 tz, 1 cache, 1 func call
902902
for _ in range(10):
903903
gc.collect() # cache is cleared every 10 full collections
904904

@@ -2293,7 +2293,7 @@ def test_decode_struct(self):
22932293
assert x == Person("harry", "potter", 13, False)
22942294

22952295
# one for struct, one for output of getattr, and one for getrefcount
2296-
assert sys.getrefcount(x.first) == 3
2296+
assert sys.getrefcount(x.first) <= 3
22972297

22982298
with pytest.raises(
22992299
msgspec.ValidationError, match="Expected `object`, got `int`"

tests/test_msgpack.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -685,13 +685,13 @@ def test_decode_memoryview_zerocopy(self, input_type):
685685
assert bytes(res) == b"abcde"
686686
assert len(res) == 5
687687
if input_type is memoryview:
688-
assert sys.getrefcount(ref) == 3
688+
assert sys.getrefcount(ref) <= 3
689689
del msg
690-
assert sys.getrefcount(ref) == 3
690+
assert sys.getrefcount(ref) <= 3
691691
del res
692-
assert sys.getrefcount(ref) == 2
692+
assert sys.getrefcount(ref) <= 2
693693
elif input_type is bytes:
694-
assert sys.getrefcount(msg) == 3
694+
assert sys.getrefcount(msg) <= 3
695695

696696
def test_datetime_aware_ext(self):
697697
dec = msgspec.msgpack.Decoder(datetime.datetime)
@@ -816,7 +816,7 @@ def test_vartuple_lengths(self, size):
816816
res = dec.decode(enc.encode(x))
817817
assert res == x
818818
if res:
819-
assert sys.getrefcount(res[0]) == 3 # 1 tuple, 1 index, 1 func call
819+
assert sys.getrefcount(res[0]) <= 3 # 1 tuple, 1 index, 1 func call
820820

821821
@pytest.mark.parametrize("typ", [tuple, Tuple, Tuple[Any, ...]])
822822
def test_vartuple_any(self, typ):

tests/test_struct.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -931,16 +931,16 @@ class Test(Struct):
931931
data = [1, 2, 3]
932932

933933
t = Test(data)
934-
assert sys.getrefcount(data) == 3
934+
assert sys.getrefcount(data) <= 3
935935

936936
repr(t)
937-
assert sys.getrefcount(data) == 3
937+
assert sys.getrefcount(data) <= 3
938938

939939
t2 = t.__copy__()
940-
assert sys.getrefcount(data) == 4
940+
assert sys.getrefcount(data) <= 4
941941

942942
assert t == t2
943-
assert sys.getrefcount(data) == 4
943+
assert sys.getrefcount(data) <= 4
944944

945945

946946
def test_struct_gc_not_added_if_not_needed():
@@ -2581,7 +2581,7 @@ def __post_init__(self):
25812581
Ex(1)
25822582
assert called
25832583
# Return value is decref'd
2584-
assert sys.getrefcount(singleton) == 2 # 1 for ref, 1 for call
2584+
assert sys.getrefcount(singleton) <= 2 # 1 for ref, 1 for call
25852585

25862586
def test_post_init_errors(self):
25872587
class Ex(Struct):

0 commit comments

Comments
 (0)