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

Type annotations for canonicaljson #49

Merged
merged 5 commits into from
Apr 25, 2022
Merged

Type annotations for canonicaljson #49

merged 5 commits into from
Apr 25, 2022

Conversation

DMRobertson
Copy link
Contributor

tox passes locally for me.

David Robertson added 3 commits April 21, 2022 15:20
@DMRobertson DMRobertson marked this pull request as ready for review April 21, 2022 14:38
@DMRobertson DMRobertson requested a review from a team as a code owner April 21, 2022 14:38
canonicaljson.py Outdated
@@ -95,7 +115,7 @@ def iterencode_canonical_json(json_object):
yield chunk.encode("utf-8")


def encode_pretty_printed_json(json_object):
def encode_pretty_printed_json(json_object: object) -> bytes:
Copy link
Member

Choose a reason for hiding this comment

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

Should we update the docstrings to remove the types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I was being lazy.

The docstrings all suggest that the encoding functions are designed to take dictionaries, but the tests have examples of encoding other things e.g. one character strings:

for c, expected in escaped.items():
self.assertEqual(encode_canonical_json(chr(c)), expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in ba77219.

pyproject.toml Outdated
exclude = "setup.py"
#mypy_path = "stubs"

#[[tool.mypy.overrides]]
Copy link
Member

Choose a reason for hiding this comment

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

Should this be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks. (This was copied from Sydent and I didn't go back and tidy this up!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And done in 2411f2f.

# Declare these in the module scope, but they get configured in
# set_json_library.
_canonical_encoder = None
_pretty_encoder = None
_canonical_encoder: Encoder = None # type: ignore[assignment]
Copy link
Member

Choose a reason for hiding this comment

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

Could you do something silly like import json as _json and then set these types to _json.JSONEncoder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe---but that might cause further pain having to wrangle with simplejson.

(I was trying to avoid touching any of the import stuff---feels a bit icky.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Being completely honest a lot of this is overkill: the main benefit to Synapse is the existence of -> bytes and -> Generator[bytes, None, None] return types.

@@ -132,7 +152,7 @@ def iterencode_pretty_printed_json(json_object):
#
# Note that it seems performance is on par or better using json from the
# standard library as of Python 3.7.
import simplejson as json
import simplejson as json # type: ignore[no-redef]
Copy link
Member

Choose a reason for hiding this comment

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

Arg, I wish we could figure out a way to kill this, but I don't think we can due to old Synapse versions which unconditionally install it...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, I hadn't realised that Synapse imports and uses set_json_library.

IMO the dependencies that we control (canonicaljson, signedjson, matrix-common?) should be specified in Synapse's metadata with semver bounds to avoid this sort of thing.

Copy link
Member

Choose a reason for hiding this comment

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

IMO the dependencies that we control (canonicaljson, signedjson, matrix-common?) should be specified in Synapse's metadata with semver bounds to avoid this sort of thing.

Absolutely, but we didn't set an upper bound previously and there isn't really a way to go back and fix that (unless maybe once the Python's from those versions are unsupported?) 🤷

David Robertson added 2 commits April 22, 2022 12:12
The data to be encoded doesn't have to be a dictionary.
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.

Seems good to me if you're happy.

@DMRobertson DMRobertson merged commit fdb88b9 into main Apr 25, 2022
@DMRobertson DMRobertson deleted the dmr/typing branch April 25, 2022 11:54
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.

2 participants