# Buggy code
def _openssh_public_key_bytes(self, key):
    if isinstance(key, rsa.RSAPublicKey):
        public_numbers = key.public_numbers()
        return b"ssh-rsa " + base64.b64encode(
            ssh._ssh_write_string(b"ssh-rsa")
            + ssh._ssh_write_mpint(public_numbers.e)
            + ssh._ssh_write_mpint(public_numbers.n)
        )
    elif isinstance(key, dsa.DSAPublicKey):
        public_numbers = key.public_numbers()
        parameter_numbers = public_numbers.parameter_numbers
        return b"ssh-dss " + base64.b64encode(
            ssh._ssh_write_string(b"ssh-dss")
            + ssh._ssh_write_mpint(parameter_numbers.p)
            + ssh._ssh_write_mpint(parameter_numbers.q)
            + ssh._ssh_write_mpint(parameter_numbers.g)
            + ssh._ssh_write_mpint(public_numbers.y)
        )
    else:
        assert isinstance(key, ec.EllipticCurvePublicKey)
        public_numbers = key.public_numbers()
        try:
            curve_name = {
                ec.SECP256R1: b"nistp256",
                ec.SECP384R1: b"nistp384",
                ec.SECP521R1: b"nistp521",
            }[type(public_numbers.curve)]
        except KeyError:
            raise ValueError(
                "Only SECP256R1, SECP384R1, and SECP521R1 curves are "
                "supported by the SSH public key format"
            )

        point = key.public_bytes(
            serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint
        )
        return (
            b"ecdsa-sha2-"
            + curve_name
            + b" "
            + base64.b64encode(
                ssh._ssh_write_string(b"ecdsa-sha2-" + curve_name)
                + ssh._ssh_write_string(curve_name)
                + ssh._ssh_write_string(point)
            )
        )


# Traceback (AssertionError)
print(key.__class__)
<class 'cryptography.hazmat.backends.openssl.ed25519._Ed25519PublicKey'>

key.public_bytes(Encoding.OpenSSH, PublicFormat.OpenSSH)
Traceback (most recent call last):
File "/usr/lib/python3.6/code.py", line 91, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/home/richard/.local/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/ed25519.py", line 45, in public_bytes
encoding, format, self, self._evp_pkey, None
File "/home/richard/.local/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1838, in _public_key_bytes
return self._openssh_public_key_bytes(key)
File "/home/richard/.local/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1886, in _openssh_public_key_bytes
assert isinstance(key, ec.EllipticCurvePublicKey)
AssertionError

# Fixed code
def _openssh_public_key_bytes(self, key):
    if isinstance(key, rsa.RSAPublicKey):
        public_numbers = key.public_numbers()
        return b"ssh-rsa " + base64.b64encode(
            ssh._ssh_write_string(b"ssh-rsa")
            + ssh._ssh_write_mpint(public_numbers.e)
            + ssh._ssh_write_mpint(public_numbers.n)
        )
    elif isinstance(key, dsa.DSAPublicKey):
        public_numbers = key.public_numbers()
        parameter_numbers = public_numbers.parameter_numbers
        return b"ssh-dss " + base64.b64encode(
            ssh._ssh_write_string(b"ssh-dss")
            + ssh._ssh_write_mpint(parameter_numbers.p)
            + ssh._ssh_write_mpint(parameter_numbers.q)
            + ssh._ssh_write_mpint(parameter_numbers.g)
            + ssh._ssh_write_mpint(public_numbers.y)
        )
    elif isinstance(key, ed25519.Ed25519PublicKey):
        raw_bytes = key.public_bytes(
            serialization.Encoding.Raw, serialization.PublicFormat.Raw
        )
        return b"ssh-ed25519 " + base64.b64encode(
            ssh._ssh_write_string(b"ssh-ed25519") + ssh._ssh_write_string(raw_bytes)
        )
    else:
        assert isinstance(key, ec.EllipticCurvePublicKey)
        public_numbers = key.public_numbers()
        try:
            curve_name = {
                ec.SECP256R1: b"nistp256",
                ec.SECP384R1: b"nistp384",
                ec.SECP521R1: b"nistp521",
            }[type(public_numbers.curve)]
        except KeyError:
            raise ValueError(
                "Only SECP256R1, SECP384R1, and SECP521R1 curves are "
                "supported by the SSH public key format"
            )

        point = key.public_bytes(
            serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint
        )
        return (
            b"ecdsa-sha2-"
            + curve_name
            + b" "
            + base64.b64encode(
                ssh._ssh_write_string(b"ecdsa-sha2-" + curve_name)
                + ssh._ssh_write_string(curve_name)
                + ssh._ssh_write_string(point)
            )
        )