diff --git a/asyncssh/agent.py b/asyncssh/agent.py index 74b69b5..acdad35 100644 --- a/asyncssh/agent.py +++ b/asyncssh/agent.py @@ -252,7 +252,7 @@ async def _make_request(self, msgtype: int, *args: bytes) -> \ resplen = int.from_bytes((await reader.readexactly(4)), 'big') - resp = SSHPacket((await reader.readexactly(resplen))) + resp = SSHPacket(await reader.readexactly(resplen)) resptype = resp.get_byte() return resptype, resp diff --git a/asyncssh/auth.py b/asyncssh/auth.py index 377b255..2457689 100644 --- a/asyncssh/auth.py +++ b/asyncssh/auth.py @@ -181,7 +181,7 @@ async def _start(self) -> None: self._gss = self._conn.get_gss_context() self._gss.reset() - mechs = b''.join((String(mech) for mech in self._gss.mechs)) + mechs = b''.join(String(mech) for mech in self._gss.mechs) await self.send_request(UInt32(len(self._gss.mechs)), mechs) else: self._conn.try_next_auth() diff --git a/asyncssh/connection.py b/asyncssh/connection.py index df25aef..d6cb52d 100644 --- a/asyncssh/connection.py +++ b/asyncssh/connection.py @@ -725,7 +725,7 @@ def get_port(self) -> int: if hasattr(self._server, 'get_port'): return self._server.get_port() else: - ports = set(addr[1] for addr in self.get_addresses()) + ports = {addr[1] for addr in self.get_addresses()} return ports.pop() if len(ports) == 1 else 0 def close(self) -> None: diff --git a/asyncssh/crypto/x509.py b/asyncssh/crypto/x509.py index cded74f..14f91ae 100644 --- a/asyncssh/crypto/x509.py +++ b/asyncssh/crypto/x509.py @@ -94,8 +94,8 @@ def _to_purpose_oids(purposes: _Purposes) -> _PurposeOIDs: if not purposes or 'any' in purposes or _purpose_any in purposes: purpose_oids = None else: - purpose_oids = set(_purpose_to_oid.get(p) or x509.ObjectIdentifier(p) - for p in purposes) + purpose_oids = {_purpose_to_oid.get(p) or x509.ObjectIdentifier(p) + for p in purposes} return purpose_oids @@ -143,8 +143,8 @@ class X509Name(x509.Name): ('CN', x509.NameOID.COMMON_NAME), ('DC', x509.NameOID.DOMAIN_COMPONENT)) - _to_oid = dict((k, v) for k, v in _attrs) - _from_oid = dict((v, k) for k, v in _attrs) + _to_oid = dict(_attrs) + _from_oid = {v: k for k, v in _attrs} def __init__(self, name: _NameInit): if isinstance(name, str): diff --git a/asyncssh/known_hosts.py b/asyncssh/known_hosts.py index d80ac60..9ab6a09 100644 --- a/asyncssh/known_hosts.py +++ b/asyncssh/known_hosts.py @@ -208,8 +208,8 @@ def _match(self, host: str, addr: str, ip = None if port: - host = '[{}]:{}'.format(host, port) if host else '' - addr = '[{}]:{}'.format(addr, port) if addr else '' + host = f'[{host}]:{port}' if host else '' + addr = f'[{addr}]:{port}' if addr else '' matches = [] matches += self._exact_entries.get(host, []) diff --git a/asyncssh/public_key.py b/asyncssh/public_key.py index fd1c412..b3d7138 100644 --- a/asyncssh/public_key.py +++ b/asyncssh/public_key.py @@ -1941,8 +1941,8 @@ def validate_chain(self, trust_chain: Sequence['SSHX509Certificate'], host_principal: str = '') -> None: """Validate an X.509 certificate chain""" - trust_store = set(c for c in trust_chain if c.subject != c.issuer) | \ - set(c for c in trusted_certs) + trust_store = {c for c in trust_chain if c.subject != c.issuer} | \ + set(trusted_certs) if trusted_cert_paths: self._expand_trust_store(self, trusted_cert_paths, trust_store) diff --git a/asyncssh/sftp.py b/asyncssh/sftp.py index 71cce08..6d29767 100644 --- a/asyncssh/sftp.py +++ b/asyncssh/sftp.py @@ -1628,7 +1628,7 @@ def _format(self, k: str, v: object) -> Optional[str]: return _file_types.get(cast(int, v), str(v)) \ if v != FILEXFER_TYPE_UNKNOWN else None elif k == 'permissions': - return '{:04o}'.format(cast(int, v)) + return f'{cast(int, v):04o}' elif k in ('atime', 'crtime', 'mtime', 'ctime'): return self._format_ns(k) elif k in ('atime_ns', 'crtime_ns', 'mtime_ns', 'ctime_ns'): diff --git a/examples/simple_keyed_server.py b/examples/simple_keyed_server.py index 0da91ab..754d5f3 100755 --- a/examples/simple_keyed_server.py +++ b/examples/simple_keyed_server.py @@ -42,7 +42,7 @@ def connection_made(self, conn: asyncssh.SSHServerConnection) -> None: def begin_auth(self, username: str) -> bool: try: self._conn.set_authorized_keys('authorized_keys/%s' % username) - except IOError: + except OSError: pass return True diff --git a/tests/test_channel.py b/tests/test_channel.py index e535679..201704d 100644 --- a/tests/test_channel.py +++ b/tests/test_channel.py @@ -271,7 +271,7 @@ async def _begin_session(self, stdin, stdout, stderr): elif action == 'agent': try: async with asyncssh.connect_agent(self._conn) as agent: - stdout.write(str(len((await agent.get_keys()))) + '\n') + stdout.write(str(len(await agent.get_keys())) + '\n') except (OSError, asyncssh.ChannelOpenError): stdout.channel.exit(1) elif action == 'agent_sock': @@ -280,7 +280,7 @@ async def _begin_session(self, stdin, stdout, stderr): if agent_path: async with asyncssh.connect_agent(agent_path) as agent: await asyncio.sleep(0.1) - stdout.write(str(len((await agent.get_keys()))) + '\n') + stdout.write(str(len(await agent.get_keys())) + '\n') else: stdout.channel.exit(1) elif action == 'rejected_agent': @@ -428,11 +428,11 @@ async def _begin_session(self, stdin, stdout, stderr): elif action == 'empty_data': stdin.channel.send_packet(MSG_CHANNEL_DATA, String('')) elif action == 'partial_unicode': - data = '\xff\xff'.encode('utf-8') + data = '\xff\xff'.encode() stdin.channel.send_packet(MSG_CHANNEL_DATA, String(data[:3])) stdin.channel.send_packet(MSG_CHANNEL_DATA, String(data[3:])) elif action == 'partial_unicode_at_eof': - data = '\xff\xff'.encode('utf-8') + data = '\xff\xff'.encode() stdin.channel.send_packet(MSG_CHANNEL_DATA, String(data[:3])) elif action == 'unicode_error': stdin.channel.send_packet(MSG_CHANNEL_DATA, String(b'\xff')) @@ -767,7 +767,7 @@ async def test_unknown_channel_request(self): async with self.connect() as conn: chan, _ = await _create_session(conn) - self.assertFalse((await chan.make_request('unknown'))) + self.assertFalse(await chan.make_request('unknown')) @asynctest async def test_invalid_channel_request(self): diff --git a/tests/test_connection.py b/tests/test_connection.py index 1afc6bd..9eadf45 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -798,7 +798,7 @@ async def test_import_known_hosts(self): known_hosts_path = os.path.join('.ssh', 'known_hosts') - with open(known_hosts_path, 'r') as f: + with open(known_hosts_path) as f: known_hosts = asyncssh.import_known_hosts(f.read()) async with self.connect(known_hosts=known_hosts): diff --git a/tests/test_process.py b/tests/test_process.py index 32f11c1..b0c5e5a 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -460,7 +460,7 @@ async def test_ignoring_invalid_unicode(self): async def test_incomplete_unicode(self): """Test incomplete Unicode data""" - data = '\u2000'.encode('utf-8')[:2] + data = '\u2000'.encode()[:2] with open('stdin', 'wb') as file: file.write(data) @@ -586,7 +586,7 @@ async def test_stdin_open_file(self): with open('stdin', 'w') as file: file.write(data) - file = open('stdin', 'r') + file = open('stdin') async with self.connect() as conn: result = await conn.run('echo', stdin=file) @@ -773,7 +773,7 @@ async def test_stdout_file(self): async with self.connect() as conn: result = await conn.run('echo', input=data, stdout='stdout') - with open('stdout', 'r') as file: + with open('stdout') as file: stdout_data = file.read() self.assertEqual(stdout_data, data) @@ -806,7 +806,7 @@ async def test_stdout_pathlib(self): async with self.connect() as conn: result = await conn.run('echo', input=data, stdout=Path('stdout')) - with open('stdout', 'r') as file: + with open('stdout') as file: stdout_data = file.read() self.assertEqual(stdout_data, data) @@ -824,7 +824,7 @@ async def test_stdout_open_file(self): async with self.connect() as conn: result = await conn.run('echo', input=data, stdout=file) - with open('stdout', 'r') as file: + with open('stdout') as file: stdout_data = file.read() self.assertEqual(stdout_data, data) @@ -842,7 +842,7 @@ async def test_stdout_open_file_keep_open(self): await conn.run('echo', input=data, stdout=file, recv_eof=False) await conn.run('echo', input=data, stdout=file, recv_eof=False) - with open('stdout', 'r') as file: + with open('stdout') as file: stdout_data = file.read() self.assertEqual(stdout_data, 2*data) @@ -995,7 +995,7 @@ async def test_change_stdout(self): result = await process.wait() - with open('stdout', 'r') as file: + with open('stdout') as file: stdout_data = file.read() self.assertEqual(stdout_data, 'xxx') @@ -1246,7 +1246,7 @@ async def test_stdout_aiofile(self): async with self.connect() as conn: result = await conn.run('echo', input=data, stdout=file) - with open('stdout', 'r') as file: + with open('stdout') as file: stdout_data = file.read() self.assertEqual(stdout_data, data) @@ -1264,7 +1264,7 @@ async def test_stdout_aiofile_keep_open(self): await conn.run('echo', input=data, stdout=file, recv_eof=False) await conn.run('echo', input=data, stdout=file, recv_eof=False) - with open('stdout', 'r') as file: + with open('stdout') as file: stdout_data = file.read() self.assertEqual(stdout_data, 2*data) @@ -1316,7 +1316,7 @@ async def test_pause_async_file_writer(self): await conn.run('delay', input=data, stdout=file, stderr=asyncssh.DEVNULL) - with open('stdout', 'r') as file: + with open('stdout') as file: self.assertEqual(file.read(), data) diff --git a/tests/test_public_key.py b/tests/test_public_key.py index 70dbf73..ddb50ba 100644 --- a/tests/test_public_key.py +++ b/tests/test_public_key.py @@ -180,7 +180,7 @@ def select_passphrase(cipher, pbe_version=0): 'rc4-40', 'rc4-128'): return 'passphrase'.encode('utf-16-be') else: - return 'passphrase'.encode('utf-8') + return b'passphrase' class _TestPublicKey(TempDirTestCase): diff --git a/tests/test_sftp.py b/tests/test_sftp.py index fc3ef30..13068d5 100644 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -1182,7 +1182,7 @@ async def test_glob(self, sftp): for pattern, matches in glob_tests: with self.subTest(pattern=pattern): - self.assertEqual(sorted((await sftp.glob(pattern))), + self.assertEqual(sorted(await sftp.glob(pattern)), matches) self.assertEqual((await sftp.glob([b'fil*1', 'fil*dir'])), @@ -1264,29 +1264,29 @@ async def test_stat(self, sftp): with self.assertRaises(SFTPNoSuchFile): await sftp.stat('badlink') - self.assertTrue((await sftp.isdir('dir'))) - self.assertFalse((await sftp.isdir('file'))) + self.assertTrue(await sftp.isdir('dir')) + self.assertFalse(await sftp.isdir('file')) if self._symlink_supported: # pragma: no branch - self.assertFalse((await sftp.isdir('badlink'))) - self.assertTrue((await sftp.isdir('dirlink'))) - self.assertFalse((await sftp.isdir('filelink'))) + self.assertFalse(await sftp.isdir('badlink')) + self.assertTrue(await sftp.isdir('dirlink')) + self.assertFalse(await sftp.isdir('filelink')) - self.assertFalse((await sftp.isfile('dir'))) - self.assertTrue((await sftp.isfile('file'))) + self.assertFalse(await sftp.isfile('dir')) + self.assertTrue(await sftp.isfile('file')) if self._symlink_supported: # pragma: no branch - self.assertFalse((await sftp.isfile('badlink'))) - self.assertFalse((await sftp.isfile('dirlink'))) - self.assertTrue((await sftp.isfile('filelink'))) + self.assertFalse(await sftp.isfile('badlink')) + self.assertFalse(await sftp.isfile('dirlink')) + self.assertTrue(await sftp.isfile('filelink')) - self.assertFalse((await sftp.islink('dir'))) - self.assertFalse((await sftp.islink('file'))) + self.assertFalse(await sftp.islink('dir')) + self.assertFalse(await sftp.islink('file')) if self._symlink_supported: # pragma: no branch - self.assertTrue((await sftp.islink('badlink'))) - self.assertTrue((await sftp.islink('dirlink'))) - self.assertTrue((await sftp.islink('filelink'))) + self.assertTrue(await sftp.islink('badlink')) + self.assertTrue(await sftp.islink('dirlink')) + self.assertTrue(await sftp.islink('filelink')) finally: remove('dir file badlink dirlink filelink') @@ -1605,8 +1605,8 @@ async def test_utime_v4(self, sftp): self.assertEqual(stat_result.st_mtime_ns, 2250000000) self.assertEqual((await sftp.getatime('file')), 1.0) self.assertEqual((await sftp.getatime_ns('file')), 1000000000) - self.assertIsNotNone((await sftp.getcrtime('file'))) - self.assertIsNotNone((await sftp.getcrtime_ns('file'))) + self.assertIsNotNone(await sftp.getcrtime('file')) + self.assertIsNotNone(await sftp.getcrtime_ns('file')) self.assertEqual((await sftp.getmtime('file')), 2.25) self.assertEqual((await sftp.getmtime_ns('file')), 2250000000) @@ -1619,8 +1619,8 @@ async def test_utime_v4(self, sftp): self.assertEqual(stat_result.st_mtime_ns, 4750000000) self.assertEqual((await sftp.getatime('file')), 3.5) self.assertEqual((await sftp.getatime_ns('file')), 3500000000) - self.assertIsNotNone((await sftp.getcrtime('file'))) - self.assertIsNotNone((await sftp.getcrtime_ns('file'))) + self.assertIsNotNone(await sftp.getcrtime('file')) + self.assertIsNotNone(await sftp.getcrtime_ns('file')) self.assertEqual((await sftp.getmtime('file')), 4.75) self.assertEqual((await sftp.getmtime_ns('file')), 4750000000) finally: @@ -1633,8 +1633,8 @@ async def test_exists(self, sftp): try: self._create_file('file1') - self.assertTrue((await sftp.exists('file1'))) - self.assertFalse((await sftp.exists('file2'))) + self.assertTrue(await sftp.exists('file1')) + self.assertFalse(await sftp.exists('file2')) finally: remove('file1') @@ -1648,8 +1648,8 @@ async def test_lexists(self, sftp): try: os.symlink('file', 'link1') - self.assertTrue((await sftp.lexists('link1'))) - self.assertFalse((await sftp.lexists('link2'))) + self.assertTrue(await sftp.lexists('link1')) + self.assertFalse(await sftp.lexists('link2')) finally: remove('link1') @@ -1769,7 +1769,7 @@ async def test_listdir(self, sftp): os.mkdir('dir') self._create_file('dir/file1') self._create_file('dir/file2') - self.assertEqual(sorted((await sftp.listdir('dir'))), + self.assertEqual(sorted(await sftp.listdir('dir')), ['.', '..', 'file1', 'file2']) finally: remove('dir') @@ -1782,7 +1782,7 @@ async def test_listdir_v4(self, sftp): os.mkdir('dir') self._create_file('dir/file1') self._create_file('dir/file2') - self.assertEqual(sorted((await sftp.listdir('dir'))), + self.assertEqual(sorted(await sftp.listdir('dir')), ['.', '..', 'file1', 'file2']) finally: remove('dir') @@ -2204,7 +2204,7 @@ async def test_open_read_parallel(self, sftp): self._create_file('file', 40*1024*'\0') f = await sftp.open('file') - self.assertEqual(len((await f.read(64*1024))), 40*1024) + self.assertEqual(len(await f.read(64*1024)), 40*1024) finally: if f: # pragma: no branch await f.close() @@ -2978,8 +2978,8 @@ async def test_file_utime_v4(self, sftp): self.assertEqual(stat_result.st_mtime_ns, 2250000000) self.assertEqual((await sftp.getatime('file')), 1.0) self.assertEqual((await sftp.getatime_ns('file')), 1000000000) - self.assertIsNotNone((await sftp.getcrtime('file'))) - self.assertIsNotNone((await sftp.getcrtime_ns('file'))) + self.assertIsNotNone(await sftp.getcrtime('file')) + self.assertIsNotNone(await sftp.getcrtime_ns('file')) self.assertEqual((await sftp.getmtime('file')), 2.25) self.assertEqual((await sftp.getmtime_ns('file')), 2250000000) @@ -2992,8 +2992,8 @@ async def test_file_utime_v4(self, sftp): self.assertEqual(stat_result.st_mtime_ns, 4750000000) self.assertEqual((await sftp.getatime('file')), 3.5) self.assertEqual((await sftp.getatime_ns('file')), 3500000000) - self.assertIsNotNone((await sftp.getcrtime('file'))) - self.assertIsNotNone((await sftp.getcrtime_ns('file'))) + self.assertIsNotNone(await sftp.getcrtime('file')) + self.assertIsNotNone(await sftp.getcrtime_ns('file')) self.assertEqual((await sftp.getmtime('file')), 4.75) self.assertEqual((await sftp.getmtime_ns('file')), 4750000000) finally: @@ -3073,7 +3073,7 @@ async def test_file_sync(self, sftp): try: f = await sftp.open('file', 'w') - self.assertIsNone((await f.fsync())) + self.assertIsNone(await f.fsync()) finally: if f: # pragma: no branch await f.close() @@ -3598,7 +3598,7 @@ async def _short_ok_response(self, pkttype, pktid, packet): with patch('asyncssh.sftp.SFTPServerHandler._process_packet', _short_ok_response): - self.assertIsNone((await sftp.mkdir('dir'))) + self.assertIsNone(await sftp.mkdir('dir')) @sftp_test async def test_malformed_realpath_response(self, sftp): @@ -4078,7 +4078,7 @@ async def test_chroot_glob(self, sftp): try: self._create_file('chroot/file1') self._create_file('chroot/file2') - self.assertEqual(sorted((await sftp.glob('/file*'))), + self.assertEqual(sorted(await sftp.glob('/file*')), ['/file1', '/file2']) finally: remove('chroot/file1 chroot/file2') @@ -4515,7 +4515,7 @@ async def start_server(cls): async def test_large_listdir(self, sftp): """Test large listdir result""" - self.assertEqual(len((await sftp.readdir('/'))), 100000) + self.assertEqual(len(await sftp.readdir('/')), 100000) @unittest.skipIf(sys.platform == 'win32', 'skip statvfs tests on Windows') @@ -4551,7 +4551,7 @@ def _check_statvfs(self, sftp_statvfs): async def test_statvfs(self, sftp): """Test getting attributes on a filesystem""" - self._check_statvfs((await sftp.statvfs('.'))) + self._check_statvfs(await sftp.statvfs('.')) @sftp_test async def test_file_statvfs(self, sftp): @@ -4563,7 +4563,7 @@ async def test_file_statvfs(self, sftp): self._create_file('file') f = await sftp.open('file') - self._check_statvfs((await f.statvfs())) + self._check_statvfs(await f.statvfs()) finally: if f: # pragma: no branch await f.close() @@ -5280,7 +5280,7 @@ async def test_source_bytes(self): """Test passing a byte string to SCP""" with self.assertRaises(OSError): - await scp('\xff:xxx'.encode('utf-8'), '.') + await scp('\xff:xxx'.encode(), '.') @asynctest async def test_source_open_connection(self): diff --git a/tests/test_stream.py b/tests/test_stream.py index 63dfbc6..e4a5915 100644 --- a/tests/test_stream.py +++ b/tests/test_stream.py @@ -49,7 +49,7 @@ async def _begin_session(self, stdin, stdout, stderr): await stdin.read(1) stdout.write('\n') elif action == 'disconnect': - stdout.write((await stdin.read(1))) + stdout.write(await stdin.read(1)) raise asyncssh.ConnectionLost('Connection lost') elif action == 'custom_disconnect': await stdin.read(1) diff --git a/tests/test_x509.py b/tests/test_x509.py index 7ae1e55..d923dd6 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -79,7 +79,7 @@ def test_generate(self): cert = self.generate_certificate(purposes='secureShellClient') - self.assertEqual(cert.purposes, set((_purpose_secureShellClient,))) + self.assertEqual(cert.purposes, {_purpose_secureShellClient}) def test_generate_ca(self): """Test X.509 CA certificate generation""" diff --git a/tests/util.py b/tests/util.py index 2c016e8..0c364d8 100644 --- a/tests/util.py +++ b/tests/util.py @@ -216,7 +216,7 @@ def make_certificate(cert_version, cert_type, key, signing_key, principals, """Construct an SSH certificate""" keydata = key.encode_ssh_public() - principals = b''.join((String(p) for p in principals)) + principals = b''.join(String(p) for p in principals) options = _encode_options(options) if options else b'' extensions = _encode_options(extensions) if extensions else b'' signing_keydata = b''.join((String(signing_key.algorithm),