Skip to content

Commit

Permalink
just fix the stray None issue, remove the comment
Browse files Browse the repository at this point in the history
  • Loading branch information
glyph committed Mar 16, 2023
1 parent 528b323 commit ae172de
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
8 changes: 1 addition & 7 deletions src/twisted/conch/openssh_compat/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,8 @@ def getPrivateKeys(self):
privateKeys[key.sshType()] = key
return privateKeys

def getPrimes(
self,
) -> Optional[Dict[int, List[Tuple[int, int]]]]: # type:ignore[override]
def getPrimes(self) -> Optional[Dict[int, List[Tuple[int, int]]]]:
try:
return primes.parseModuliFile(self.moduliRoot + "/moduli")
except OSError:
# As seen in the type:ignore[override] above, this historically
# handled errors by (invalidly) returning None, and we should
# probably fix this at some point, since callers don't need to
# handle None given the contract of the superclass.
return None
15 changes: 7 additions & 8 deletions src/twisted/conch/ssh/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import random
from itertools import chain
from typing import Dict, List, Tuple
from typing import Dict, List, Optional, Tuple

from twisted.conch import error
from twisted.conch.ssh import _kex, connection, transport, userauth
Expand All @@ -25,7 +25,7 @@ class SSHFactory(protocol.Factory):
A Factory for SSH servers.
"""

primes: Dict[int, List[Tuple[int, int]]]
primes: Optional[Dict[int, List[Tuple[int, int]]]]

_log = Logger()
protocol = transport.SSHServerTransport
Expand All @@ -35,7 +35,7 @@ class SSHFactory(protocol.Factory):
b"ssh-connection": connection.SSHConnection,
}

def startFactory(self):
def startFactory(self) -> None:
"""
Check for public and private keys.
"""
Expand Down Expand Up @@ -96,13 +96,11 @@ def getPrivateKeys(self):
"""
raise NotImplementedError("getPrivateKeys unimplemented")

def getPrimes(self) -> Dict[int, List[Tuple[int, int]]]:
def getPrimes(self) -> Optional[Dict[int, List[Tuple[int, int]]]]:
"""
Called when the factory is started to get Diffie-Hellman generators and
primes to use. Returns a dictionary mapping number of bits to lists
of tuple of (generator, prime).
@rtype: L{dict}
primes to use. Returns a dictionary mapping number of bits to lists of
tuple of (generator, prime).
"""

def getDHPrime(self, bits: int) -> Tuple[int, int]:
Expand All @@ -114,6 +112,7 @@ def getDHPrime(self, bits: int) -> Tuple[int, int]:
def keyfunc(i: int) -> int:
return abs(i - bits)

assert self.primes is not None, "Factory should have been started by now."
primesKeys = sorted(self.primes.keys(), key=keyfunc)
realBits = primesKeys[0]
return random.choice(self.primes[realBits])
Expand Down
2 changes: 1 addition & 1 deletion src/twisted/conch/test/test_openssh_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def seteuid(euid):
self.assertEqual(self.mockos.seteuidCalls, [0, os.geteuid()])
self.assertEqual(self.mockos.setegidCalls, [0, os.getegid()])

def test_getPrimes(self):
def test_getPrimes(self) -> None:
"""
L{OpenSSHFactory.getPrimes} should return the available primes
in the moduli directory.
Expand Down

0 comments on commit ae172de

Please sign in to comment.