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

Added dedicated page about using types with pytest #12842 #12963

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mwychung
Copy link

Fix (#12842 )

  • Added dedicated page about using types with pytest, doc/en/how-to/types.rst.
    This guide explains how to apply typing in pytest, covering typing test functions, fixtures, and parameterized tests to improve code readability and prevent type-related errors.
  • Allow maintainers to push and squash when merging my commits.
  • Create a new changelog file.
  • Updated AUTHORS.

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 14, 2024
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Thank you for your work on this. I feel like this not something that is specific to pytest so probably best as an external article or tutorial (?)

@Pierre-Sassoulas
Copy link
Member

(Also you amended an unrelated commit :) )

@mwychung
Copy link
Author

Thank you for the feedback! I’m still a beginner with pytest and open-source contributions, and I’m eager to learn more. I appreciate your guidance. I’d be happy to focus on pytest-specific examples for typing if that would be more relevant. Would you like me to demonstrate typing with pytest’s fixtures, parameterized tests, or any specific areas you think would be most beneficial for the documentation?

Thanks again for your help!

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

@Pierre-Sassoulas actually this has been discussed in the past, and seems like the overall consensus is that having a page dedicated how to type tests would be welcome, as those topics are often brought up and unclear for many people.

However I think the docs as presented are a bit verbose, IMHO.

I think a "typing tests with pytest" page should already consider that readers know about Python typing (how to add types, what are the benefits/drawbacks, etc, perhaps only pointing to the external docs for readers that are not familiar yet), and focus mainly on the topics that pertain to pytest/tests: tests, fixtures, parametrized tests, etc.

The main benefits of add typing to tests should also be highlighted:

  1. Refactoring: the type checker can easily point out the necessary changes in both code and tests, without needing to run the full test suite.
  2. Readability: the types make the test code easier to read.

For production code, there are other benefits (such as preventing errors), but for tests, those are the main two reasons to add typing to tests IMHO.

This guide explains how to apply typing in pytest to improve code readability and prevent type-related errors.


1. Introduction to Typing
Copy link
Member

Choose a reason for hiding this comment

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

I would replace this entire section with a paragraph like "this page assumes the reader already knows how typing in Python works, its benefits, etc, for more information see PAGE".

Copy link
Member

Choose a reason for hiding this comment

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

I would add a section like "Why type tests", explaining the motivation to add types to tests (which are slightly different than the reasons to add typing to production code).

Comment on lines +29 to +30
2. Typing Test Functions
-----------------
Copy link
Member

Choose a reason for hiding this comment

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

Sections need to use the same number of characters as the title, here for example:

Suggested change
2. Typing Test Functions
-----------------
2. Typing Test Functions
------------------------


2. Typing Test Functions
-----------------
Test functions in pytest check whether the code runs correctly.
Copy link
Member

Choose a reason for hiding this comment

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

As I mentioned, the reason for a test is different: it is better to run the test in order to see if the code is correct -- but we add typing to help with refactoring and readability. 👍

Comment on lines +45 to +50
In this function:
`test_add` is typed as `-> None` because it does not return anything.
The assertion `assert result == 5` checks if the result is correct.

Example result:
If the input data type is incorrect, like passing `add("2", 3)`, a `TypeError` will occur.
Copy link
Member

Choose a reason for hiding this comment

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

These are not needed, as we assume readers already know about Python typing.

Comment on lines +80 to +83
* Typing Fixtures with Lists and Dictionaries:
Here are the examples showing how to use List and Dict types in pytest.
When you want to use complex data structures like lists or dictionaries, import `List` and `Dict` from Python's `typing` module to specify the types.
Note: From Python 3.5 or later, typing module is built-in module in Python.
Copy link
Member

Choose a reason for hiding this comment

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

We can drop this too, again we can assume the reader knows about typing and the particulars about list/List, etc.

Comment on lines +45 to +46
In this function:
`test_add` is typed as `-> None` because it does not return anything.
Copy link
Member

Choose a reason for hiding this comment

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

We can instead mention that while at first marking a function as returning -> None might seem useless, having at least a type annotation means most type checkers will now type check the function, so it is useful because of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided (automation) changelog entry is part of PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants