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

tests: conftest: Add type annotations. #1096

Merged
merged 8 commits into from
Aug 12, 2021

Conversation

prah23
Copy link
Member

@prah23 prah23 commented Jul 24, 2021

What does this PR do?
Adds type annotations to conftest.py and some relevant prior refactors. Also includes conftest.py under the list in run-mypy.

Tested?

  • Manually
  • Existing tests (adapted, if necessary)
  • Passed linting & tests (each commit)

Commit flow

  • refactor: tests: conftest: Return a Message from template factory.
    This commit changes the returned object of the msg_template_factory
    from a Dict to a Message instance, which is the appropriate type
    for a message template.

  • refactor: tests: conftest: Return Index instance from empty_index.
    This commit returns an Index instance for the empty_index fixture
    which previously returned a deepcopy of a Dict, as Index is the
    empty_index's appropriate type.

  • refactor: test: conftest: Return Index instances for index_* fixtures.
    This commit returns Index instances instead of dicts from all the
    various index fixtures present, since the appropriate type for these
    fixtures is Index.

  • api_types/helper/model/server_url: Replace stream_id type in Message.
    This commit replaces the Message class' stream_id attribute's type
    from int to Optional[int], since this attribute only has an integer
    value when the message is a stream message and None when it is a
    private message.

  • bugfix: tests: conftest: Return empty tuple when widget isn't box/flow.
    This commit fixes the else case of the _widget_size method of
    the widget_size fixture method. Previously, the `else case did not
    return anything, which has been modified to appropriately return an
    empty tuple now.

  • refactor: tests: conftest: Add type annotations.
    This commit adds parameter and return type annotations or hints to
    the conftest.py file, that contains custom pytest fixtures meant to
    be used in the tests to make mypy checks consistent and improve code
    readability.

  • tools: Include conftest.py to be checked by mypy.
    This commit adds conftest.py to the type_consistent_testfiles
    list to check for type consistency with mypy.

Notes & Questions

The request pytest fixture has no exported type, so I've typed it as Any for now. The only way to type it currently would be a private import otherwise.

@zulipbot zulipbot added the size: XL [Automatic label added by zulipbot] label Jul 24, 2021
@prah23 prah23 force-pushed the test_annotation_conftest branch 2 times, most recently from 5e24e15 to 305e864 Compare July 24, 2021 18:02
@prah23 prah23 force-pushed the test_annotation_conftest branch 2 times, most recently from 1cb93f7 to d3acd1e Compare August 6, 2021 06:48
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.

@prah23 Thanks for the small changes in the last push; I had some other notes and wanted to get them to you before I get to bed - though note I'm not sure they're all actionable!

tests/conftest.py Outdated Show resolved Hide resolved
tests/conftest.py Show resolved Hide resolved
@@ -231,7 +236,7 @@ def logged_on_user():


@pytest.fixture
def streams_fixture():
def streams_fixture() -> List[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.

This should match a type, but again we may have entries missing and be ZFL-dependent :/

Copy link
Member Author

Choose a reason for hiding this comment

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

Though there is the ZFL issue, I do think an API type for streams would be appropriate, since a "Stream" is one of our major types.

tests/conftest.py Show resolved Hide resolved
@@ -289,7 +294,7 @@ def realm_emojis():


@pytest.fixture
def realm_emojis_data():
def realm_emojis_data() -> "OrderedDict[str, 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.

We may want to check with Zeeshan, but a specific (internal) type might be good to define for this, given how much we're using it :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, there's also an instance of this in test_emoji_data.py.

tests/conftest.py Show resolved Hide resolved
tests/conftest.py Show resolved Hide resolved
tests/conftest.py Outdated Show resolved Hide resolved
@neiljp neiljp added PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback PR blocks other PR labels Aug 11, 2021
@neiljp
Copy link
Collaborator

neiljp commented Aug 11, 2021

@prah23 I believe this probably should be done before #1102 at least?

@neiljp neiljp mentioned this pull request Aug 11, 2021
3 tasks
@prah23 prah23 force-pushed the test_annotation_conftest branch 4 times, most recently from 6d4b03c to a00d17b Compare August 11, 2021 20:51
@prah23 prah23 added PR needs review PR requires feedback to proceed and removed PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback labels Aug 11, 2021
prah23 added 8 commits August 12, 2021 12:21
This commit changes the returned object of the `msg_template_factory`
from a `Dict` to a `Message` instance, which is the appropriate type
for a message template. `stream_id` is also included in the `Message`
instance only when it is a stream message.

Places where the factory function is used are also typed accordingly.
This commit returns an `Index` instance for the `empty_index` fixture
which previously returned a deepcopy of a Dict, as Index is the
`empty_index`'s appropriate type.

Other fixtures using this fixture are updated to this type at this point
too.
This commit returns `Index` instances instead of `dict`s from all the
various index fixtures present, being the more appropriate type.

The return type for these fixtures is added accordingly.
This commit fixes the `else` case of the `_widget_size` method of
the `widget_size` fixture method. Previously, the else case did not
return anything, which has been modified to appropriately return an
empty tuple now.
This commit modifies the `bot_type` attribute of the RealmUser class
to an `Optional[int]` from the previous `int` data type. This change
is motivated by the server not assigning an integer to the user when
the user isn't a bot, making None the ideal value in that case.

To maintain consistency the `bot_type` attribute of the TidiedUserInfo
class becomes an `Optional[int]` from the previous `int` data type.
This commit modifies the default `bot_type` value from the previous
value of 0 to None, since a user who isn't a bot has a null value for
this attribute.
This commit adds parameter and return type annotations or hints to
the `conftest.py` file, that contains custom pytest fixtures meant to
be used in the tests to make mypy checks consistent and improve code
readability.
This commit adds `conftest.py` to the `type_consistent_testfiles`
list to check for type consistency with mypy.
@neiljp neiljp force-pushed the test_annotation_conftest branch from a00d17b to f9e7a8e Compare August 12, 2021 20:01
@neiljp
Copy link
Collaborator

neiljp commented Aug 12, 2021

@prah23 Thanks for getting this done - this is quite large and will also be a good guide to typing other tests 🎉

@neiljp neiljp merged commit 53d13f1 into zulip:main Aug 12, 2021
@neiljp neiljp added area: refactoring area: tests and removed PR needs review PR requires feedback to proceed labels Aug 12, 2021
@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 this to the Next Release milestone Feb 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants