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

refactor: Move from type comments to inline type annotations. #910

Merged
merged 1 commit into from
Feb 7, 2021

Conversation

ahmedabuamra
Copy link
Contributor

@ahmedabuamra ahmedabuamra commented Feb 5, 2021

Ran com2ann in the project folder to auto re-write the files.

Fixes: #901

@zulipbot zulipbot added the size: XL [Automatic label added by zulipbot] label Feb 5, 2021
@ahmedabuamra ahmedabuamra force-pushed the abuamra-inline-type-annotations branch 2 times, most recently from 9730596 to 50641de Compare February 6, 2021 02:06
Copy link
Collaborator

@neiljp neiljp left a comment

Choose a reason for hiding this comment

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

@ahmedabuamra Welcome to the project and thanks for looking into this 👍

The com2ann tool is automated so I've not looked in fine detail, but noticed a few cases we may want to look into in terms of formatting, checking the state of lister/run-mypy compared to zulip/zulip, and OrderedDict (as discussed on chat.zulip.org).

This does point to various cases where we could improve the typing - while equivalent I'd prefer Optional[X] to Union[None, X], for example - but we can address those in another PR.

tools/lister.py Outdated
Comment on lines 72 to 73
result_dict: Dict[str, List[str]] = defaultdict(list)
result_list: List[str] = []
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd suggest we don't update tools at this point, at least not this file, since this file was ported from zulip/zulip and it would be useful to check separately if other updates are in order - and that repo has likely updated to inline types which we could pull across for comparison in any case. The same applies to run-mypy, which uses lister.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand. tools/lister.py is the only changed file in tools.

@@ -11,7 +11,7 @@ class KeyBinding(TypedDict, total=False):
key_category: str


KEY_BINDINGS = OrderedDict([
KEY_BINDINGS: Dict[str, KeyBinding] = OrderedDict([
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's stick with OrderedDict cases being in type comments for now. We can move over when we shift to python3.7+, if we still have OrderedDict around then.

Comment on lines 463 to 469
custom_emojis: NamedEmojiData = {
emoji['name']: {
'code': emoji_code,
'type': 'realm_emoji'
}
for emoji_code, emoji in response['emoji'].items()
if not emoji['deactivated']}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did com2ann generate this new layout? We're moving towards a style where large bracketed code have the closing one on the starting line, though the original version isn't ideal in this respect either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Meaning to be like that, right?

        custom_emojis: NamedEmojiData = {
            emoji['name']: {
                'code': emoji_code,
                'type': 'realm_emoji'
            }
            for emoji_code, emoji in response['emoji'].items()
            if not emoji['deactivated']
        }

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right 👍 Also trailing commas, but that's simpler to quickly fix :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@neiljp Done :)

Comment on lines 97 to 98
footer_text: List[Any] = [' ' + s + ' '
for s in suggestions]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Fits on one line?

@neiljp neiljp added area: refactoring PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback labels Feb 6, 2021
@zulipbot
Copy link
Member

zulipbot commented Feb 6, 2021

Hello @zulip/server-refactoring members, this pull request was labeled with the "area: refactoring" label, so you may want to check it out!

@ahmedabuamra ahmedabuamra force-pushed the abuamra-inline-type-annotations branch 2 times, most recently from 3289791 to 78148ee Compare February 6, 2021 12:47
@ahmedabuamra
Copy link
Contributor Author

@zulipbot remove "PR needs update"

@zulipbot zulipbot removed the PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback label Feb 6, 2021
@andersk
Copy link
Member

andersk commented Feb 6, 2021

To deal with types like OrderedDict[K, V] that parse in mypy but don’t evaluate in Python at runtime, you can quote them:

KEY_BINDINGS: "OrderedDict[str, KeyBinding]" = OrderedDict([
    …
])

@ahmedabuamra ahmedabuamra force-pushed the abuamra-inline-type-annotations branch from 78148ee to 819cb39 Compare February 6, 2021 19:25
@ahmedabuamra
Copy link
Contributor Author

Thanks @andersk, I updated the PR as you suggested and waiting for @neiljp 's confirmation too.

Ran `com2ann` in the project folder to auto re-write the files.

Note:
* tools/lister.py file has not been modified as imported from zulip/zulip

Fixes: zulip#901
@ahmedabuamra ahmedabuamra force-pushed the abuamra-inline-type-annotations branch from 819cb39 to efd4d0b Compare February 6, 2021 20:54
@neiljp
Copy link
Collaborator

neiljp commented Feb 7, 2021

@ahmedabuamra Thanks for getting this done - I'm merging this now 🎉

@neiljp neiljp merged commit 54c9737 into zulip:main Feb 7, 2021
@neiljp neiljp added this to the Next Release milestone Feb 7, 2021
@ahmedabuamra ahmedabuamra deleted the abuamra-inline-type-annotations branch February 7, 2021 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: refactoring size: XL [Automatic label added by zulipbot]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move from type comments to inline type annotations
4 participants