Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: Incompatible types in assignment (expression has type "AbstractKey", variable has type "PrivateKey") #185

Merged
merged 1 commit into from
Jan 11, 2022

Conversation

ikeikeikeike
Copy link
Contributor

Tiny fix to Incompatible types in assignment error

For example.

class AbstractKey:
    @classmethod
    def _load_pkcs1_pem(cls) -> 'AbstractKey':
        pass

    @classmethod
    def load_pkcs1(cls) -> 'AbstractKey':
        return cls()


class PrivateKey(AbstractKey):
    @classmethod
    def _load_pkcs1_pem(cls) -> 'PrivateKey':
        return cls()


class PublicKey(AbstractKey):
    @classmethod
    def _load_pkcs1_pem(cls) -> 'PublicKey':
        return cls()


def main() -> None:
    akey = PrivateKey.load_pkcs1()
    pkey: PrivateKey = akey
    print(pkey)


if __name__ == '__main__':
    main()

mypy output

$ mypy main.py
main.py:25: error: Incompatible types in assignment (expression has type "AbstractKey", variable has type "PrivateKey")
Found 1 error in 1 file (checked 1 source file)

Supposedly, the python-rsa is used often like below
and then we will encounter like above an error message main.py:25 due to PrivateKey.load_pkcs1 and rsa.sign methods have incompatible types.

def func(message: bytes, private_key_string: bytes) -> bytes:
    key = rsa.PrivateKey.load_pkcs1(private_key_string)
    return rsa.sign(message, key, "SHA-1")

Might be useful to mypy option --strict if you have never seen the same message.

@sybrenstuvel sybrenstuvel merged commit 32bfe40 into sybrenstuvel:main Jan 11, 2022
@sybrenstuvel
Copy link
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants