1414
1515from pathlib import Path
1616from types import SimpleNamespace
17- from typing import TYPE_CHECKING , Dict , List , Optional , Pattern , Sequence , Union , cast
17+ from typing import (
18+ TYPE_CHECKING ,
19+ Dict ,
20+ List ,
21+ Optional ,
22+ Pattern ,
23+ Sequence ,
24+ Set ,
25+ Union ,
26+ cast ,
27+ )
1828
1929from playwright ._impl ._api_structures import (
2030 ClientCertificate ,
@@ -60,7 +70,7 @@ def __init__(
6070 self ._should_close_connection_on_close = False
6171 self ._cr_tracing_path : Optional [str ] = None
6272
63- self ._contexts : List [BrowserContext ] = []
73+ self ._contexts : Set [BrowserContext ] = set ()
6474 self ._traces_dir : Optional [str ] = None
6575 self ._channel .on (
6676 "context" ,
@@ -88,7 +98,7 @@ def _connect_to_browser_type(
8898
8999 def _did_create_context (self , context : BrowserContext ) -> None :
90100 context ._browser = self
91- self ._contexts .append (context )
101+ self ._contexts .add (context )
92102 # Note: when connecting to a browser, initial contexts arrive before `_browserType` is set,
93103 # and will be configured later in `ConnectToBrowserType`.
94104 if self ._browser_type :
@@ -97,15 +107,15 @@ def _did_create_context(self, context: BrowserContext) -> None:
97107 def _setup_browser_context (self , context : BrowserContext ) -> None :
98108 context ._tracing ._traces_dir = self ._traces_dir
99109 print ("Appending context to selectors" )
100- self ._browser_type ._playwright .selectors ._contextsForSelectors .append (context )
110+ self ._browser_type ._playwright .selectors ._contextsForSelectors .add (context )
101111
102112 def _on_close (self ) -> None :
103113 self ._is_connected = False
104114 self .emit (Browser .Events .Disconnected , self )
105115
106116 @property
107117 def contexts (self ) -> List [BrowserContext ]:
108- return self ._contexts . copy ( )
118+ return list ( self ._contexts )
109119
110120 @property
111121 def browser_type (self ) -> "BrowserType" :
0 commit comments