Initialize Stream.session within Stream.__init__
authorHarmon <Harmon758@gmail.com>
Tue, 23 Nov 2021 13:41:24 +0000 (07:41 -0600)
committerHarmon <Harmon758@gmail.com>
Sun, 20 Feb 2022 06:07:36 +0000 (00:07 -0600)
Update the user agent based on Stream.user_agent even if Stream.session is already initialized

tweepy/streaming.py

index f18b28f6f4a36de2199f233fa4ad248353e6aaf7..1bbc2f9027c3719980000d839ca430090dcc7e2d 100644 (file)
@@ -58,7 +58,7 @@ class Stream:
     ----------
     running : bool
         Whether there's currently a stream running
-    session : Optional[:class:`requests.Session`]
+    session : :class:`requests.Session`
         Requests Session used to connect to the stream
     thread : Optional[:class:`threading.Thread`]
         Thread used to run the stream
@@ -80,7 +80,7 @@ class Stream:
         self.verify = verify
 
         self.running = False
-        self.session = None
+        self.session = requests.Session()
         self.thread = None
         self.user_agent = (
             f"Python/{python_version()} "
@@ -103,9 +103,7 @@ class Stream:
         auth = OAuth1(self.consumer_key, self.consumer_secret,
                       self.access_token, self.access_token_secret)
 
-        if self.session is None:
-            self.session = requests.Session()
-            self.session.headers["User-Agent"] = self.user_agent
+        self.session.headers["User-Agent"] = self.user_agent
 
         url = f"https://stream.twitter.com/1.1/{endpoint}.json"