11import os
22import sys
33from dataclasses import dataclass
4- from typing import Optional
4+ from typing import Optional , Union
55
6+ import requests
67from socketsecurity import USER_AGENT
78from socketsecurity .core import log
89from socketsecurity .core .classes import Comment
@@ -128,9 +129,9 @@ def _request_with_fallback(self, **kwargs):
128129 try :
129130 # Try the initial request with the configured headers
130131 return self .client .request (** kwargs )
131- except Exception as e :
132+ except requests . exceptions . HTTPError as e :
132133 # Check if this is an authentication error (401)
133- if hasattr ( e , 'response' ) and e .response and e .response .status_code == 401 :
134+ if e .response and e .response .status_code == 401 :
134135 log .debug (f"Authentication failed with initial headers, trying fallback method" )
135136
136137 # Determine the fallback headers
@@ -144,6 +145,9 @@ def _request_with_fallback(self, **kwargs):
144145
145146 # Re-raise the original exception if it's not an auth error or fallback failed
146147 raise
148+ except Exception as e :
149+ # Handle other types of exceptions that don't have response attribute
150+ raise
147151
148152 def _get_fallback_headers (self , original_headers : dict ) -> dict :
149153 """
@@ -235,13 +239,13 @@ def add_socket_comments(
235239 new_security_comment : bool = True ,
236240 new_overview_comment : bool = True
237241 ) -> None :
238- existing_overview_comment = comments .get ("overview" , "" )
239- existing_security_comment = comments .get ("security" , "" )
242+ existing_overview_comment = comments .get ("overview" )
243+ existing_security_comment = comments .get ("security" )
240244 if new_overview_comment :
241245 log .debug ("New Dependency Overview comment" )
242246 if existing_overview_comment is not None :
243247 log .debug ("Previous version of Dependency Overview, updating" )
244- existing_overview_comment : Comment
248+ # Type narrowing: after None check, mypy knows this is Comment
245249 self .update_comment (overview_comment , str (existing_overview_comment .id ))
246250 else :
247251 log .debug ("No previous version of Dependency Overview, posting" )
@@ -250,15 +254,15 @@ def add_socket_comments(
250254 log .debug ("New Security Issue Comment" )
251255 if existing_security_comment is not None :
252256 log .debug ("Previous version of Security Issue comment, updating" )
253- existing_security_comment : Comment
257+ # Type narrowing: after None check, mypy knows this is Comment
254258 self .update_comment (security_comment , str (existing_security_comment .id ))
255259 else :
256260 log .debug ("No Previous version of Security Issue comment, posting" )
257261 self .post_comment (security_comment )
258262
259263 def remove_comment_alerts (self , comments : dict ):
260- security_alert = comments .get ("security" , "" )
264+ security_alert = comments .get ("security" )
261265 if security_alert is not None :
262- security_alert : Comment
266+ # Type narrowing: after None check, mypy knows this is Comment
263267 new_body = Comments .process_security_comment (security_alert , comments )
264268 self .update_comment (new_body , str (security_alert .id ))
0 commit comments