From 838cf65b1c181b7b4f00ae712166065bad0fbf54 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Thu, 5 Mar 2026 21:05:36 +0000 Subject: remote: T8347: fix variable assignment for try/except block Pylint is pointing out that because sftp.put(location, path) is located inside a finally block, it is guaranteed to execute regardless of whether the try block succeeds or fails. If an exception other than an IOError is raised inside the try block, "except IOError" block gets skipped, Python then jumps straight to the finally block. When it executes the finally block, it attempts to read the variable path, but path was never assigned! This triggers UnboundLocalError, local variable 'path' referenced before assignment. --- python/vyos/remote.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'python') diff --git a/python/vyos/remote.py b/python/vyos/remote.py index d4efac0d9..417f9d42c 100644 --- a/python/vyos/remote.py +++ b/python/vyos/remote.py @@ -220,23 +220,21 @@ class SshC: def upload(self, location: str): with self._establish() as ssh, ssh.open_sftp() as sftp: + # A file exists at this destination. We're simply going to clobber it. + # This is our default fallback + path = self.path try: # If the remote path is a directory, use the original filename. if stat.S_ISDIR(sftp.stat(self.path).st_mode): path = os.path.join(self.path, os.path.basename(location)) - # A file exists at this destination. We're simply going to clobber it. - else: - path = self.path - # This path doesn't point at any existing file. We can freely use this filename. except IOError: - path = self.path - finally: - if self.progressbar: - with Progressbar() as p: - sftp.put(location, path, callback=p.progress) - else: - sftp.put(location, path) + pass + if self.progressbar: + with Progressbar() as p: + sftp.put(location, path, callback=p.progress) + else: + sftp.put(location, path) class HttpC: def __init__(self, -- cgit v1.2.3