Skip to content

Commit 2340c72

Browse files
authored
Accept objects that support __int__ for query vars (#1139)
1 parent 0ee0104 commit 2340c72

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGES/1139.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Loosen restriction on integers as query string values to allow classes that implement ``__int__`` -- by :user:`bdraco`.

tests/test_update_query.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,19 @@ class IntEnum(int, enum.Enum):
249249
assert str(url2) == "http://example.com/path?a=1"
250250

251251

252+
def test_with_class_that_implements__int__():
253+
"""Allow classes that implement __int__ to be used in query strings."""
254+
255+
class myint:
256+
257+
def __int__(self):
258+
return 84
259+
260+
url = URL("http://example.com/path")
261+
url2 = url.with_query(a=myint())
262+
assert str(url2) == "http://example.com/path?a=84"
263+
264+
252265
def test_with_float_enum():
253266
class FloatEnum(float, enum.Enum):
254267
A = 1.1

yarl/_url.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Iterable,
1313
Iterator,
1414
List,
15+
SupportsInt,
1516
Tuple,
1617
TypedDict,
1718
TypeVar,
@@ -1185,9 +1186,7 @@ def _query_var(v: QueryVariable) -> str:
11851186
if math.isnan(v):
11861187
raise ValueError("float('nan') is not supported")
11871188
return str(float(v))
1188-
if cls is not bool and issubclass(cls, int):
1189-
if TYPE_CHECKING:
1190-
assert isinstance(v, int)
1189+
if cls is not bool and isinstance(cls, SupportsInt):
11911190
return str(int(v))
11921191
raise TypeError(
11931192
"Invalid variable type: value "

0 commit comments

Comments
 (0)