Skip to content

Add type hints to test_message.py #1407

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SBNetto01
Copy link
Collaborator

What does this PR do, and why?

This PR adds the adequate type hints in each function from test_message.py, solving issue #1337

External discussion & connections

  • Discussed in #zulip-terminal in topic
  • Fully fixes #
  • Partially fixes issue #
  • Builds upon previous unmerged work in PR #
  • Is a follow-up to work in PR #
  • Requires merge of PR #
  • Merge will enable work on #

How did you test this?

  • Manually - Behavioral changes
  • Manually - Visual changes
  • Adapting existing automated tests
  • Adding automated tests for new behavior (or missing tests)
  • Existing automated tests should already cover this (only a refactor of tested code)

Self-review checklist for each commit

  • It is a minimal coherent idea
  • It has a commit summary following the documented style (title & body)
  • It has a commit summary describing the motivation and reasoning for the change
  • It individually passes linting and tests
  • It contains test additions for any new behavior
  • It flows clearly from a previous branch commit, and/or prepares for the next commit

@zulipbot zulipbot added the size: XL [Automatic label added by zulipbot] label May 31, 2023
@neiljp
Copy link
Collaborator

neiljp commented Jun 15, 2023

@SBNetto01 Thanks for looking into this 👍

Did you see the other PR for this? I think there is only one other?

Were you wanting feedback at this point, or unsure how to proceed given the failing linting/tests?

Do you understand why the linting and tests are failing? I'd be happy to discuss briefly here, or chat in more depth on chat.zulip.org if you need help with this.

@zulipbot
Copy link
Member

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

@neiljp neiljp added the PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback label Jun 15, 2023
@SBNetto01
Copy link
Collaborator Author

Thank you for your reply and sorry about the delay! I was waiting on some feedback, since the failed linting/tests are a bit weird. I wasn't able to use the --fix recommended on a few of the documentations, and all of them are related to "empty spaces" if I'm not mistaken, but it's not related to my modifications whatsoever. Would love to know how to proceed with this.

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.

@SBNetto01 I've unfortunately only just got back to this, but here are some details if you're still interested!

I left some details inline - generally you seem to have a good grasp of where the typing goes in the code and what some of the types are, but others appear to not match at all - did you use a tool for this? As I mention inline, if you can understand how pytest uses parametrize better then I suspect you'll be able to determine a lot of the types more fully.


One challenge that I forgot from the previous change where we typed other tests was moving from a simple Dict to a TypedDict (like Message). The main challenge with those is that historically we used an approach of base data that is updated in various different ways in each test case, but mypy doesn't like using certain general dict operations where literal keys are not involved.

So, you may wish to keep some types looser (eg. Dict[str, Any] for what may become Message) in a first pass if that helps you get further, and then we can refine from there.

For reference, the general solution for the latter case generally involves updating the tests:
a) to refer to specific literal strings as keys into the dict (where possible), or
b) using factory functions/fixtures where the desired values are passed in (constructed as required, rather than modified later)

If you're on chat.zulip.org, do start a topic in #zulip-terminal if you're interested :)

@@ -25,7 +29,7 @@

class TestMessageBox:
@pytest.fixture(autouse=True)
def mock_external_classes(self, mocker, initial_index):
def mock_external_classes(self, mocker: MockerFixture, initial_index: int) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here initial_index is a fixture, not an int. Please see #1337 for some details on fixtures and where they're often defined. If it's not obvious where a parameter comes from, it's often a fixture!

def test_init(
self,
mocker: MockerFixture,
message_type: str,
Copy link
Collaborator

Choose a reason for hiding this comment

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

For message_type, we likely want the new MessageType in api_types.py; that's also a string, but a more constrained type.

def test_soup2markup(self, content, expected_markup, mocker):
def test_soup2markup(
self, content: str,
expected_markup: str,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please ensure you understand how parametrize works, so that you can determine a good type for expected_markup.

Comment on lines +761 to +762
message: Dict[str, Any],
last_message: Optional[Dict[str, Any]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

These dicts, and those in other changes, should be replaced by Message, again in api_types.py.

self,
mocker: MockerFixture,
message: Dict[str, Any],
to_vary_in_last_message: bool
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are another case of you needing to understand parametrize first.

):
mocker: MockerFixture,
messages_successful_response: Dict[str, Any],
msg_type: MessageType,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I see you used MessageType here - but note that the variable type is different here, which I can tell from reading the parametrize (as per other comments).

mocker: MockerFixture,
messages_successful_response: Dict[str, Any],
msg_type: MessageType,
msg_narrow: list,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think mypy will complain if you use a list with no typing, even if that typing is Any (which we may need here) - and with our current setup we likely need List too.

Comment on lines +1877 to +1880
message_links: List[str],
expected_text: Optional[str],
expected_attrib: Optional[Dict[str, Any]],
expected_footlinks_width: Optional[int]
Copy link
Collaborator

Choose a reason for hiding this comment

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

These need you to understand parametrize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: refactoring area: tests PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback size: XL [Automatic label added by zulipbot]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants