44
55Objects for controlling the configuration of the HTTP/2 stack.
66"""
7+ from __future__ import annotations
78
89import sys
9- from typing import Any , Optional , Union
10+ from typing import Any
1011
1112
1213class _BooleanConfigOption :
1314 """
1415 Descriptor for handling a boolean config option. This will block
1516 attempts to set boolean config options to non-bools.
1617 """
18+
1719 def __init__ (self , name : str ) -> None :
1820 self .name = name
19- self .attr_name = '_%s' % self .name
21+ self .attr_name = f"_ { self .name } "
2022
2123 def __get__ (self , instance : Any , owner : Any ) -> bool :
2224 return getattr (instance , self .attr_name ) # type: ignore
2325
2426 def __set__ (self , instance : Any , value : bool ) -> None :
2527 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
2730 setattr (instance , self .attr_name , value )
2831
2932
@@ -35,20 +38,19 @@ class DummyLogger:
3538 conditionals being sprinkled throughout the h2 code for calls to
3639 logging functions when no logger is passed into the corresponding object.
3740 """
41+
3842 def __init__ (self , * vargs ) -> None : # type: ignore
3943 pass
4044
4145 def debug (self , * vargs , ** kwargs ) -> None : # type: ignore
4246 """
4347 No-op logging. Only level needed for now.
4448 """
45- pass
4649
4750 def trace (self , * vargs , ** kwargs ) -> None : # type: ignore
4851 """
4952 No-op logging. Only level needed for now.
5053 """
51- pass
5254
5355
5456class OutputLogger :
@@ -61,15 +63,16 @@ class OutputLogger:
6163 Defaults to ``sys.stderr``.
6264 :param trace: Enables trace-level output. Defaults to ``False``.
6365 """
64- def __init__ (self , file = None , trace_level = False ): # type: ignore
66+
67+ def __init__ (self , file = None , trace_level = False ) -> None : # type: ignore
6568 super ().__init__ ()
6669 self .file = file or sys .stderr
6770 self .trace_level = trace_level
6871
69- def debug (self , fmtstr , * args ): # type: ignore
72+ def debug (self , fmtstr , * args ) -> None : # type: ignore
7073 print (f"h2 (debug): { fmtstr % args } " , file = self .file )
7174
72- def trace (self , fmtstr , * args ): # type: ignore
75+ def trace (self , fmtstr , * args ) -> None : # type: ignore
7376 if self .trace_level :
7477 print (f"h2 (trace): { fmtstr % args } " , file = self .file )
7578
@@ -147,32 +150,33 @@ class H2Configuration:
147150
148151 :type logger: ``logging.Logger``
149152 """
150- client_side = _BooleanConfigOption ('client_side' )
153+
154+ client_side = _BooleanConfigOption ("client_side" )
151155 validate_outbound_headers = _BooleanConfigOption (
152- ' validate_outbound_headers'
156+ " validate_outbound_headers" ,
153157 )
154158 normalize_outbound_headers = _BooleanConfigOption (
155- ' normalize_outbound_headers'
159+ " normalize_outbound_headers" ,
156160 )
157161 split_outbound_cookies = _BooleanConfigOption (
158- ' split_outbound_cookies'
162+ " split_outbound_cookies" ,
159163 )
160164 validate_inbound_headers = _BooleanConfigOption (
161- ' validate_inbound_headers'
165+ " validate_inbound_headers" ,
162166 )
163167 normalize_inbound_headers = _BooleanConfigOption (
164- ' normalize_inbound_headers'
168+ " normalize_inbound_headers" ,
165169 )
166170
167171 def __init__ (self ,
168172 client_side : bool = True ,
169- header_encoding : Optional [ Union [ bool , str ]] = None ,
173+ header_encoding : bool | str | None = None ,
170174 validate_outbound_headers : bool = True ,
171175 normalize_outbound_headers : bool = True ,
172176 split_outbound_cookies : bool = False ,
173177 validate_inbound_headers : bool = True ,
174178 normalize_inbound_headers : bool = True ,
175- logger : Optional [ Union [ DummyLogger , OutputLogger ]] = None ) -> None :
179+ logger : DummyLogger | OutputLogger | None = None ) -> None :
176180 self .client_side = client_side
177181 self .header_encoding = header_encoding
178182 self .validate_outbound_headers = validate_outbound_headers
@@ -183,7 +187,7 @@ def __init__(self,
183187 self .logger = logger or DummyLogger (__name__ )
184188
185189 @property
186- def header_encoding (self ) -> Optional [ Union [ bool , str ]] :
190+ def header_encoding (self ) -> bool | str | None :
187191 """
188192 Controls whether the headers emitted by this object in events are
189193 transparently decoded to ``unicode`` strings, and what encoding is used
@@ -195,12 +199,14 @@ def header_encoding(self) -> Optional[Union[bool, str]]:
195199 return self ._header_encoding
196200
197201 @header_encoding .setter
198- def header_encoding (self , value : Optional [ Union [ bool , str ]] ) -> None :
202+ def header_encoding (self , value : bool | str | None ) -> None :
199203 """
200204 Enforces constraints on the value of header encoding.
201205 """
202206 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
204209 if value is True :
205- raise ValueError ("header_encoding cannot be True" )
210+ msg = "header_encoding cannot be True"
211+ raise ValueError (msg )
206212 self ._header_encoding = value
0 commit comments