Skip to content

Mypy annotation zerver.lib.event_queue and zerver.lib.queue #958

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

Closed

Conversation

cpdean
Copy link
Contributor

@cpdean cpdean commented Jun 6, 2016

I ran into some problems with send_event taking either a list of ints or a list of dicts, so i figured i would change the caller's code.

Had problems with events either being a Dict with str keys and one caller who put an int key in there.

Additionally, needed to create some placeholder callbacks where mypy claimed it couldn't infer the type of lambdas.

@smarx
Copy link

smarx commented Jun 6, 2016

Automated message from Dropbox CLA bot

@cpdean, it looks like you've already signed the Dropbox CLA. Thanks!

@cpdean cpdean force-pushed the mypy-annotation-zerver.lib.event_queue branch 2 times, most recently from e73c2eb to abca3fd Compare June 7, 2016 01:58
@timabbott
Copy link
Member

Cool, thanks for doing this @cpdean! I merged versions of the first 3 commits (mostly just clarifying commit messages, plus a bit of cleanup to a27e50c70d5d6d675cdaafbd438cf69258ff211d to make it pass mypy checks and remove annotations not directly related to the change).

Can you rebase this branch to squash the fixes to bugs introduced in this branch into the original commit that introduced them? Then it'll be a lot easier to incrementally merge the remaining pieces of this.
check out the Zulip coding style guidelines here for details on our commit style: https://zulip.readthedocs.org/en/latest/code-style.html#version-control

Also, I'm curious about the need for nop_callback -- is that working around a mypy bug? It seems like mypy should be able to handle normal usage of lambda x: None, so it might make sense to report the bug to upstream mypy and get it fixed there...

@timabbott
Copy link
Member

@cpdean just wanted to check in on this. If you're busy, it'd be valuable to at least have an answer to the nop_callback question...

@cpdean
Copy link
Contributor Author

cpdean commented Jun 13, 2016

woops, hey I'll dig into the lambda thing tonight. making commits better
will take me a bit - my git powers are pretty much just limited to squashing

On Mon, Jun 13, 2016, 12:42 Tim Abbott [email protected] wrote:

@cpdean https://github.com/cpdean just wanted to check in on this. If
you're busy, it'd be valuable to at least have an answer to the
nop_callback question...


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#958 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AAJxYw62xU0gfdV94g58i7pN9vsVppiPks5qLYhngaJpZM4Iue-E
.

@cpdean
Copy link
Contributor Author

cpdean commented Jun 15, 2016

@timabbott okay, i've ripped out the nop_callback type things and I'm able to reproduce the error I saw earlier this month.

I boiled it down to a minimal example and reported the error here: python/mypy#1710

Should we have the lambas in place and just add # type: ignore # https://github.com/python/mypy/issues/1710 for now?

@cpdean
Copy link
Contributor Author

cpdean commented Jun 15, 2016

woops, my bad. made a duplicate of python/mypy#1425

@timabbott
Copy link
Member

No worries! The # type: ignore approach linked to python/mypy#1425 sounds good.

@cpdean cpdean force-pushed the mypy-annotation-zerver.lib.event_queue branch from ebe3089 to 21c66f6 Compare June 22, 2016 00:30
@cpdean
Copy link
Contributor Author

cpdean commented Jun 22, 2016

added the ignore notes on inline lambdas and cleaned out a third of the commits

it'll be a couple days before i can take another pass but let me know what I should focus on. thanks @timabbott !

@cpdean cpdean force-pushed the mypy-annotation-zerver.lib.event_queue branch from 21c66f6 to efbcfd1 Compare June 22, 2016 02:54
@cpdean
Copy link
Contributor Author

cpdean commented Jun 22, 2016

found some more stuff to clean up and make more clear. ready for feedback, thanks again @timabbott !

@timabbott
Copy link
Member

@sharmaeklavya2 can you do a review pass on this?

@@ -54,6 +56,7 @@ class ClientDescriptor(object):
def __init__(self, user_profile_id, user_profile_email, realm_id, event_queue,
event_types, client_type_name, apply_markdown=True,
all_public_streams=False, lifespan_secs=0, narrow=[]):
# type: (int, str, int, EventQueue, List[str], str, Optional[bool], Optional[bool], Optional[int], Iterable[Sequence[text_type]]) -> None
Copy link
Member

Choose a reason for hiding this comment

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

user_profile_email should be text_type, isn't it?

Copy link
Member

Choose a reason for hiding this comment

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

Are you sure None is a valid value for apply_markdown, all_public_streams and lifespan_secs?

@@ -233,13 +254,15 @@ def to_dict(self):

@classmethod
def from_dict(cls, d):
# type: (Dict[str, Any]) -> EventQueue
Copy link
Member

Choose a reason for hiding this comment

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

d should be Mapping[str, Any].

@cpdean
Copy link
Contributor Author

cpdean commented Jun 24, 2016

@sharmaeklavya2 this is great thank you. admittedly I hadn't read the cheat sheet before starting this. I'll be able to clean this up this weekend.

@sharmaeklavya2
Copy link
Member

sharmaeklavya2 commented Jun 24, 2016

Well I have suggested using Mapping or MutableMapping at some places, but there is a slight chance that might not work. For e.g., if an object has a property that is typed as a Dict, assigning a Mapping to it will fail. I have not checked your annotations very deeply so I might miss out on things like that.

After annotating all this code, you probably know it well enough that you can figure it out yourself what should be the correct type for a variable among Mapping, MutableMapping and Dict. But if you face any problems in doing so, just mention me and I'll try to help.


def receiver_is_idle(user_profile_id, realm_presences):
# type: (int, Dict[int, Dict]) -> bool
Copy link
Member

Choose a reason for hiding this comment

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

realm_presences should be Optional[Mapping[int, Mapping]].

@sharmaeklavya2
Copy link
Member

@timabbott @cpdean I have reviewed this pull request.

@cpdean cpdean force-pushed the mypy-annotation-zerver.lib.event_queue branch from 0196eee to b61e052 Compare June 25, 2016 16:56
@cpdean
Copy link
Contributor Author

cpdean commented Jun 26, 2016

@sharmaeklavya2 i had missed your comment about pika.BasicProperties earlier but have fixed it. Also I fixed the annotation on event_types to set it to List[str] since the other parts of Zulip seem to agree on that type.

@@ -56,7 +56,7 @@ class ClientDescriptor(object):
def __init__(self, user_profile_id, user_profile_email, realm_id, event_queue,
event_types, client_type_name, apply_markdown=True,
all_public_streams=False, lifespan_secs=0, narrow=[]):
# type: (int, text_type, int, EventQueue, List[text_type], text_type, bool, bool, int, Iterable[Sequence[text_type]]) -> None
# type: (int, text_type, int, EventQueue, List[str], text_type, bool, bool, int, Iterable[Sequence[text_type]]) -> None
Copy link
Member

@sharmaeklavya2 sharmaeklavya2 Jun 26, 2016

Choose a reason for hiding this comment

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

I see that self.event_types is never modified once it is set (not just in this function, but also outside it), so maybe Sequence[str] would be better here.
Actually there are some places where self.event_types is being compared to None, like in ClientDescriptor.accepts_event. It should be Optional[Sequence[str]].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh good catch. Fixed it.

@@ -781,7 +781,7 @@ def test_successful_subscriptions_add(self):
are generated.
"""
self.assertNotEqual(len(self.streams), 0) # necessary for full test coverage
add_streams = [u"Verona2", u"Denmark5"]
add_streams = [u"Verona2", u"Denmark5"] # type: List[text_type]
Copy link
Member

Choose a reason for hiding this comment

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

Is this really needed? Isn't mypy able to deduce this? (I haven't checked)

@timabbott
Copy link
Member

Merged via #1171. Thanks @cpdean for working on this!!

@timabbott timabbott closed this Jul 4, 2016
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