@@ -339,6 +339,15 @@ def validate_timeout_or_none_or_zero(option: Any, value: Any) -> Optional[float]
339339 return validate_positive_float (option , value ) / 1000.0
340340
341341
342+ def validate_timeoutms (option : Any , value : Any ) -> Optional [float ]:
343+ """Validates a timeout specified in milliseconds returning
344+ a value in floating point seconds.
345+ """
346+ if value is None :
347+ return None
348+ return validate_positive_float_or_zero (option , value ) / 1000.0
349+
350+
342351def validate_max_staleness (option : str , value : Any ) -> int :
343352 """Validates maxStalenessSeconds according to the Max Staleness Spec."""
344353 if value == - 1 or value == "-1" :
@@ -658,6 +667,7 @@ def validate_auto_encryption_opts_or_none(option: Any, value: Any) -> Optional[A
658667 "zlibcompressionlevel" : validate_zlib_compression_level ,
659668 "srvservicename" : validate_string ,
660669 "srvmaxhosts" : validate_non_negative_integer ,
670+ "timeoutms" : validate_timeoutms ,
661671}
662672
663673# Dictionary where keys are the names of URI options specific to pymongo,
@@ -821,8 +831,8 @@ def __init__(
821831 read_preference : _ServerMode ,
822832 write_concern : WriteConcern ,
823833 read_concern : ReadConcern ,
834+ timeout : Optional [float ],
824835 ) -> None :
825-
826836 if not isinstance (codec_options , CodecOptions ):
827837 raise TypeError ("codec_options must be an instance of bson.codec_options.CodecOptions" )
828838 self .__codec_options = codec_options
@@ -845,6 +855,12 @@ def __init__(
845855 raise TypeError ("read_concern must be an instance of pymongo.read_concern.ReadConcern" )
846856 self .__read_concern = read_concern
847857
858+ if not isinstance (timeout , (int , float , type (None ))):
859+ raise TypeError ("timeout must be None, an int, or a float" )
860+ if timeout and timeout < 0 :
861+ raise TypeError ("timeout cannot be negative" )
862+ self .__timeout = float (timeout ) if timeout else None
863+
848864 @property
849865 def codec_options (self ) -> CodecOptions :
850866 """Read only access to the :class:`~bson.codec_options.CodecOptions`
@@ -894,6 +910,14 @@ def read_concern(self) -> ReadConcern:
894910 """
895911 return self .__read_concern
896912
913+ @property
914+ def timeout (self ) -> Optional [float ]:
915+ """Read only access to the timeout of this instance.
916+
917+ .. versionadded:: 4.2
918+ """
919+ return self .__timeout
920+
897921
898922class _CaseInsensitiveDictionary (abc .MutableMapping ):
899923 def __init__ (self , * args , ** kwargs ):
0 commit comments