Skip to content

Commit

Permalink
backport 3.12 OrderedDict.__repr__ to run doctest on older py versions.
Browse files Browse the repository at this point in the history
fixed #249
  • Loading branch information
gawel committed Apr 2, 2024
1 parent b0cc127 commit 60ea3ab
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions pyquery/pyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Distributed under the BSD license, see LICENSE.txt
from .cssselectpatch import JQueryTranslator
from collections import OrderedDict
from reprlib import recursive_repr
from urllib.parse import urlencode
from urllib.parse import urljoin
from .openers import url_opener
Expand All @@ -14,6 +14,24 @@
import inspect
import itertools
import types
import sys

if sys.version_info >= (3, 12, 0):
from collections import OrderedDict
else:
# backward compat. to be able to run doctest with 3.7+. see:
# https://github.com/gawel/pyquery/issues/249
# and:
# https://github.com/python/cpython/blob/3.12/Lib/collections/__init__.py#L272
from collections import OrderedDict as BaseOrderedDict

class OrderedDict(BaseOrderedDict):
@recursive_repr()
def __repr__(self):
'od.__repr__() <==> repr(od)'
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, dict(self.items()))

basestring = (str, bytes)

Expand Down Expand Up @@ -1609,9 +1627,9 @@ def serialize_dict(self):
... <input name="order2" value="ham">
... </form>''')
>>> d.serialize_dict()
OrderedDict([('order', ['spam', 'eggs']), ('order2', 'ham')])
OrderedDict({'order': ['spam', 'eggs'], 'order2': 'ham'})
>>> d.serializeDict()
OrderedDict([('order', ['spam', 'eggs']), ('order2', 'ham')])
OrderedDict({'order': ['spam', 'eggs'], 'order2': 'ham'})
"""
ret = OrderedDict()
for name, val in self.serialize_pairs():
Expand Down

0 comments on commit 60ea3ab

Please sign in to comment.