4
4
5
5
Objects for controlling the configuration of the HTTP/2 stack.
6
6
"""
7
+ from __future__ import annotations
7
8
8
9
import sys
9
- from typing import Any , Optional , Union
10
+ from typing import Any
10
11
11
12
12
13
class _BooleanConfigOption :
13
14
"""
14
15
Descriptor for handling a boolean config option. This will block
15
16
attempts to set boolean config options to non-bools.
16
17
"""
18
+
17
19
def __init__ (self , name : str ) -> None :
18
20
self .name = name
19
- self .attr_name = '_%s' % self .name
21
+ self .attr_name = f"_ { self .name } "
20
22
21
23
def __get__ (self , instance : Any , owner : Any ) -> bool :
22
24
return getattr (instance , self .attr_name ) # type: ignore
23
25
24
26
def __set__ (self , instance : Any , value : bool ) -> None :
25
27
if not isinstance (value , bool ):
26
- raise ValueError ("%s must be a bool" % self .name )
28
+ msg = f"{ self .name } must be a bool"
29
+ raise ValueError (msg ) # noqa: TRY004
27
30
setattr (instance , self .attr_name , value )
28
31
29
32
@@ -35,20 +38,19 @@ class DummyLogger:
35
38
conditionals being sprinkled throughout the h2 code for calls to
36
39
logging functions when no logger is passed into the corresponding object.
37
40
"""
41
+
38
42
def __init__ (self , * vargs ) -> None : # type: ignore
39
43
pass
40
44
41
45
def debug (self , * vargs , ** kwargs ) -> None : # type: ignore
42
46
"""
43
47
No-op logging. Only level needed for now.
44
48
"""
45
- pass
46
49
47
50
def trace (self , * vargs , ** kwargs ) -> None : # type: ignore
48
51
"""
49
52
No-op logging. Only level needed for now.
50
53
"""
51
- pass
52
54
53
55
54
56
class OutputLogger :
@@ -61,15 +63,16 @@ class OutputLogger:
61
63
Defaults to ``sys.stderr``.
62
64
:param trace: Enables trace-level output. Defaults to ``False``.
63
65
"""
64
- def __init__ (self , file = None , trace_level = False ): # type: ignore
66
+
67
+ def __init__ (self , file = None , trace_level = False ) -> None : # type: ignore
65
68
super ().__init__ ()
66
69
self .file = file or sys .stderr
67
70
self .trace_level = trace_level
68
71
69
- def debug (self , fmtstr , * args ): # type: ignore
72
+ def debug (self , fmtstr , * args ) -> None : # type: ignore
70
73
print (f"h2 (debug): { fmtstr % args } " , file = self .file )
71
74
72
- def trace (self , fmtstr , * args ): # type: ignore
75
+ def trace (self , fmtstr , * args ) -> None : # type: ignore
73
76
if self .trace_level :
74
77
print (f"h2 (trace): { fmtstr % args } " , file = self .file )
75
78
@@ -147,32 +150,33 @@ class H2Configuration:
147
150
148
151
:type logger: ``logging.Logger``
149
152
"""
150
- client_side = _BooleanConfigOption ('client_side' )
153
+
154
+ client_side = _BooleanConfigOption ("client_side" )
151
155
validate_outbound_headers = _BooleanConfigOption (
152
- ' validate_outbound_headers'
156
+ " validate_outbound_headers" ,
153
157
)
154
158
normalize_outbound_headers = _BooleanConfigOption (
155
- ' normalize_outbound_headers'
159
+ " normalize_outbound_headers" ,
156
160
)
157
161
split_outbound_cookies = _BooleanConfigOption (
158
- ' split_outbound_cookies'
162
+ " split_outbound_cookies" ,
159
163
)
160
164
validate_inbound_headers = _BooleanConfigOption (
161
- ' validate_inbound_headers'
165
+ " validate_inbound_headers" ,
162
166
)
163
167
normalize_inbound_headers = _BooleanConfigOption (
164
- ' normalize_inbound_headers'
168
+ " normalize_inbound_headers" ,
165
169
)
166
170
167
171
def __init__ (self ,
168
172
client_side : bool = True ,
169
- header_encoding : Optional [ Union [ bool , str ]] = None ,
173
+ header_encoding : bool | str | None = None ,
170
174
validate_outbound_headers : bool = True ,
171
175
normalize_outbound_headers : bool = True ,
172
176
split_outbound_cookies : bool = False ,
173
177
validate_inbound_headers : bool = True ,
174
178
normalize_inbound_headers : bool = True ,
175
- logger : Optional [ Union [ DummyLogger , OutputLogger ]] = None ) -> None :
179
+ logger : DummyLogger | OutputLogger | None = None ) -> None :
176
180
self .client_side = client_side
177
181
self .header_encoding = header_encoding
178
182
self .validate_outbound_headers = validate_outbound_headers
@@ -183,7 +187,7 @@ def __init__(self,
183
187
self .logger = logger or DummyLogger (__name__ )
184
188
185
189
@property
186
- def header_encoding (self ) -> Optional [ Union [ bool , str ]] :
190
+ def header_encoding (self ) -> bool | str | None :
187
191
"""
188
192
Controls whether the headers emitted by this object in events are
189
193
transparently decoded to ``unicode`` strings, and what encoding is used
@@ -195,12 +199,14 @@ def header_encoding(self) -> Optional[Union[bool, str]]:
195
199
return self ._header_encoding
196
200
197
201
@header_encoding .setter
198
- def header_encoding (self , value : Optional [ Union [ bool , str ]] ) -> None :
202
+ def header_encoding (self , value : bool | str | None ) -> None :
199
203
"""
200
204
Enforces constraints on the value of header encoding.
201
205
"""
202
206
if not isinstance (value , (bool , str , type (None ))):
203
- raise ValueError ("header_encoding must be bool, string, or None" )
207
+ msg = "header_encoding must be bool, string, or None"
208
+ raise ValueError (msg ) # noqa: TRY004
204
209
if value is True :
205
- raise ValueError ("header_encoding cannot be True" )
210
+ msg = "header_encoding cannot be True"
211
+ raise ValueError (msg )
206
212
self ._header_encoding = value
0 commit comments