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

Make sydent.hs_federation pass mypy --strict #432

Merged
merged 16 commits into from
Oct 21, 2021
Merged

Conversation

DMRobertson
Copy link
Contributor

This was a bit more involved!

@DMRobertson DMRobertson force-pushed the dmr/mypy-hs-federation branch from 09eebea to 38c20e2 Compare October 19, 2021 16:54
Turn and face `git blame`
@DMRobertson DMRobertson requested a review from a team October 19, 2021 17:01
@clokep clokep self-assigned this Oct 20, 2021
Copy link
Member

@clokep clokep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a step in the right direction, but I'm not sure about all the TypedDicts -- see my comments!

sydent/hs_federation/types.py Outdated Show resolved Hide resolved
Comment on lines 22 to 23
class VerifyKey(TypedDict):
key: str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it easier if we define these classes in the order that lets them build up so we can remove all the quotes? (Personally I find reading it that way easier too.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal preference I think. No strong opinions here. I'll take another look at this once I've considered where we want to use attrs.

sydent/hs_federation/verifier.py Show resolved Hide resolved
For example, `X-Matrix origin=origin.example.com,key="ed25519:key1",sig="ABCDEF..."`
taken from
https://spec.matrix.org/unstable/server-server-api/#request-authentication .
TODO Is there a more authoritative reference for the X-Matrix scheme?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is more authoritative than the spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That particular link gives an example of the X-Matrix auth scheme, but it's not formally defined there. I couldn't see anywhere else in the server-server API that defined it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a small paragraph that vaguely describes it. I think that is the definition of it, it just could be improved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best to link to the stable spec, otherwise the links can go out of date: https://matrix.org/docs/spec/server_server/r0.1.4#request-authentication

but yes, that is the authoritative reference, such as it is.

sydent/hs_federation/verifier.py Outdated Show resolved Hide resolved
sydent/hs_federation/types.py Outdated Show resolved Hide resolved
@@ -171,65 +182,77 @@ async def authenticate_request(

:return: The origin of the server whose signature was validated
"""
json_request = {
Copy link
Contributor Author

@DMRobertson DMRobertson Oct 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the type annotation for content in this function is wrong.

I think it should be a JsonDict, judging by the callsite.

Unsure if this is a net improvement
@DMRobertson DMRobertson requested a review from clokep October 20, 2021 17:32
@@ -0,0 +1,34 @@
from typing import Dict
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License header please.

Comment on lines -136 to -138
if "key" not in server_keys[key_name]:
logger.warning("Ignoring key %s with no 'key'")
continue
Copy link
Member

@clokep clokep Oct 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This got moved into _getKeysForServer, essentially. (I think?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. On lines 100--109 of verifier.py, I wanted to ensure that I only had something of type VerifyKeys after verifying that it was of the correct type and structure.

(attrs and cattrs might make this easier one day?)

Copy link
Member

@clokep clokep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is OK.

from sydent.types import JsonDict


class VerifyKey(TypedDict):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just sort of curious why this class doesn't require the @attr decorator to use what looks like attr-powered class creation. I am sure there is something terribly simple I am missing here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The syntax is similar (much like stdlib's dataclasses) but this is mainly a fancy type annotation rather than a class with actual runtime behaviour.

PEP 589 has the motivation, but in a nutshell: TypedDict is a means to add some level of type checking to code that passes dictionaries around with a fixed set of keys. I think the goal is to add type checking without having to rewrite your application or library to use attr.s, dataclasses, namedtuples.

Note that TypedDict (more strictly: the subclass of TypedDict that I create) is just used as annotation here: there's no runtime effect. (I think you can call VerifyKey(...), but it behaves as if you called dict(...) directly.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that make sense? Happy to expand or reword either way!

@DMRobertson DMRobertson merged commit 46417df into main Oct 21, 2021
@DMRobertson DMRobertson deleted the dmr/mypy-hs-federation branch October 21, 2021 09:32
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.

4 participants