Skip to content

Commit e85ac65

Browse files
authored
feat(http1/http2): implement __str__ for types (#559)
* feat(http1/http2): implement __str__ for types * fix build * fix maturin 1.13.x build
1 parent 3c55295 commit e85ac65

15 files changed

Lines changed: 107 additions & 89 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ permissions:
2828
contents: write
2929
packages: write
3030

31+
env:
32+
MATURIN_VERSION: ${{ vars.MATURIN_VERSION }}
33+
3134
jobs:
3235
style:
3336
name: style
@@ -220,7 +223,7 @@ jobs:
220223
with:
221224
maturin-version: ${{ env.MATURIN_VERSION }}
222225
target: ${{ matrix.platform.target }}
223-
args: --release --out dist ${{ matrix.build_type.maturin_args }} --features ${{ matrix.platform.allocator || '' }}
226+
args: --release --out dist ${{ matrix.platform.target != 'aarch64' && matrix.build_type.maturin_args || '' }} --features ${{ matrix.platform.allocator || '' }}
224227
sccache: "false"
225228
- name: Upload wheels
226229
uses: actions/upload-artifact@v7

python/wreq/http1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ def __init__(self, **kwargs: Unpack[Params]) -> None:
6666
Crate a new Http1Options instance.
6767
"""
6868
...
69+
70+
def __str__(self) -> str:
71+
"""
72+
Return a string representation of the type.
73+
"""
74+
...

python/wreq/http2.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def __init__(self, src: int) -> None:
123123
"""
124124
...
125125

126+
def __str__(self) -> str:
127+
"""
128+
Return a string representation of the type.
129+
"""
130+
...
131+
126132

127133
@final
128134
class StreamDependency:
@@ -149,6 +155,12 @@ def __init__(
149155
"""
150156
...
151157

158+
def __str__(self) -> str:
159+
"""
160+
Return a string representation of the type.
161+
"""
162+
...
163+
152164

153165
@final
154166
class Priority:
@@ -172,6 +184,12 @@ def __init__(self, stream_id: StreamId, dependency: StreamDependency) -> None:
172184
"""
173185
...
174186

187+
def __str__(self) -> str:
188+
"""
189+
Return a string representation of the type.
190+
"""
191+
...
192+
175193

176194
@final
177195
class Priorities:
@@ -191,6 +209,12 @@ def __init__(self, *priority: Priority) -> None:
191209
"""
192210
...
193211

212+
def __str__(self) -> str:
213+
"""
214+
Return a string representation of the type.
215+
"""
216+
...
217+
194218

195219
@final
196220
class PseudoOrder:
@@ -209,6 +233,12 @@ def __init__(self, *pseudo_id: PseudoId) -> None:
209233
"""
210234
...
211235

236+
def __str__(self) -> str:
237+
"""
238+
Return a string representation of the type.
239+
"""
240+
...
241+
212242

213243
@final
214244
class SettingsOrder:
@@ -228,6 +258,12 @@ def __init__(self, *setting_id: SettingId) -> None:
228258
"""
229259
...
230260

261+
def __str__(self) -> str:
262+
"""
263+
Return a string representation of the type.
264+
"""
265+
...
266+
231267

232268
class Params(TypedDict):
233269
"""
@@ -359,3 +395,9 @@ def __init__(self, **kwargs: Unpack[Params]) -> None:
359395
Create a new Http2Options instance.
360396
"""
361397
...
398+
399+
def __str__(self) -> str:
400+
"""
401+
Return a string representation of the type.
402+
"""
403+
...

src/client.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ mod param;
77
mod query;
88

99
use std::{
10-
fmt,
1110
net::{IpAddr, Ipv4Addr, Ipv6Addr},
1211
sync::Arc,
1312
time::Duration,
@@ -55,11 +54,7 @@ impl SocketAddr {
5554
}
5655
}
5756

58-
impl fmt::Display for SocketAddr {
59-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60-
self.0.fmt(f)
61-
}
62-
}
57+
define_display!(SocketAddr);
6358

6459
/// A builder for `Client`.
6560
#[derive(Default)]

src/client/resp/http.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl Response {
181181
.extensions()
182182
.get::<wreq::redirect::History>()
183183
.map_or_else(Vec::new, |history| {
184-
history.into_iter().cloned().map(History::from).collect()
184+
history.into_iter().cloned().map(History).collect()
185185
})
186186
})
187187
}

src/client/resp/ws.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl WebSocket {
5656
pub async fn new(response: WebSocketResponse) -> wreq::Result<WebSocket> {
5757
let (version, status, remote_addr, local_addr, headers) = (
5858
Version::from_ffi(response.version()),
59-
StatusCode::from(response.status()),
59+
StatusCode(response.status()),
6060
response.remote_addr().map(SocketAddr),
6161
response.local_addr().map(SocketAddr),
6262
HeaderMap(response.headers().clone()),

src/client/resp/ws/msg.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//!
88
//! The `Message` type is used for sending and receiving WebSocket messages in a unified way.
99
10-
use std::fmt::{self, Debug};
10+
use std::fmt::Debug;
1111

1212
use bytes::Bytes;
1313
use pyo3::{
@@ -173,9 +173,4 @@ impl Message {
173173
}
174174
}
175175

176-
impl fmt::Display for Message {
177-
#[inline]
178-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179-
self.0.fmt(f)
180-
}
181-
}
176+
define_display!(Message);

src/cookie.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt, sync::Arc, time::SystemTime};
1+
use std::{sync::Arc, time::SystemTime};
22

33
use bytes::Bytes;
44
use cookie::{Cookie as RawCookie, Expiration, ParseError, time::Duration};
@@ -181,11 +181,7 @@ impl Cookie {
181181
}
182182
}
183183

184-
impl fmt::Display for Cookie {
185-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186-
self.0.fmt(f)
187-
}
188-
}
184+
define_display!(Cookie);
189185

190186
// ===== impl Cookies =====
191187

src/header.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::fmt;
2-
31
use bytes::Bytes;
42
use pyo3::{
53
prelude::*,
@@ -234,11 +232,7 @@ impl HeaderMap {
234232
}
235233
}
236234

237-
impl fmt::Display for HeaderMap {
238-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
239-
write!(f, "{:?}", self.0)
240-
}
241-
}
235+
define_display!(HeaderMap);
242236

243237
impl FromPyObject<'_, '_> for HeaderMap {
244238
type Error = PyErr;
@@ -344,11 +338,7 @@ impl OrigHeaderMap {
344338
}
345339
}
346340

347-
impl fmt::Display for OrigHeaderMap {
348-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
349-
write!(f, "{:?}", self.0)
350-
}
351-
}
341+
define_display!(OrigHeaderMap);
352342

353343
impl FromPyObject<'_, '_> for OrigHeaderMap {
354344
type Error = PyErr;

src/http.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::fmt;
2-
31
use pyo3::{class::basic::CompareOp, prelude::*};
42

53
define_enum!(
@@ -88,14 +86,4 @@ impl StatusCode {
8886
}
8987
}
9088

91-
impl From<wreq::StatusCode> for StatusCode {
92-
fn from(status: wreq::StatusCode) -> Self {
93-
Self(status)
94-
}
95-
}
96-
97-
impl fmt::Display for StatusCode {
98-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99-
self.0.fmt(f)
100-
}
101-
}
89+
define_display!(StatusCode);

0 commit comments

Comments
 (0)