Skip to content

Commit

Permalink
Merge pull request #100 from ValueRaider/pickling
Browse files Browse the repository at this point in the history
Fix pickling
  • Loading branch information
JWCook authored Jul 2, 2024
2 parents 65a1055 + c2f32c4 commit fa4848c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions requests_ratelimiter/requests_ratelimiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ class LimiterSession(LimiterMixin, Session):
per_host: Track request rate limits separately for each host
limit_statuses: Alternative HTTP status codes that indicate a rate limit was exceeded
"""

__attrs__ = Session.__attrs__ + [
'limiter',
'limit_statuses',
'max_delay',
'per_host',
'bucket_name',
'_default_bucket'
]


class LimiterAdapter(LimiterMixin, HTTPAdapter): # type: ignore # send signature accepts **kwargs
Expand Down
27 changes: 27 additions & 0 deletions test/test_requests_ratelimiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from time import sleep
from unittest.mock import patch

import pickle

import pytest
from pyrate_limiter import Duration, Limiter, RequestRate, SQLiteBucket
from requests import Response, Session
Expand Down Expand Up @@ -244,3 +246,28 @@ class CachedLimiterSession(CacheMixin, LimiterMixin, Session):
for _ in range(10):
session.get(MOCKED_URL)
assert mock_sleep.called is False


def test_inherited_session_attributes():
# Test that inherited Session attributes are preserved
session = LimiterSession(per_second=5)
assert hasattr(session, 'headers')
assert hasattr(session, 'cookies')
assert hasattr(session, 'auth')
assert hasattr(session, 'hooks')


def test_pickling_and_unpickling():
# Test pickling and unpickling of LimiterSession instance
session = LimiterSession(per_second=5)
pickled_session = pickle.dumps(session)
assert pickled_session is not None
unpickled_session = pickle.loads(pickled_session)
assert unpickled_session is not None

# Check that the unpickled instance has the same attributes
assert unpickled_session.per_host == session.per_host
assert unpickled_session.max_delay == session.max_delay
assert unpickled_session.bucket_name == session.bucket_name
assert unpickled_session.limit_statuses == session.limit_statuses
assert unpickled_session._default_bucket == session._default_bucket

0 comments on commit fa4848c

Please sign in to comment.