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 5 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!

@mwychung mwychung force-pushed the feature/doc-typing-12842 branch from 09e6ee2 to 1d944c4 Compare November 15, 2024 14:22
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).

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I added "Why Type tests" to focus on the specific benefits of typing in tests, like improved refactoring and readability. Let me know if further changes are needed!

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
------------------------

Copy link
Author

Choose a reason for hiding this comment

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

Thank you so much for your helpful guidance. I matched all the section titles with same number of characters.


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.

@mwychung mwychung closed this Nov 29, 2024
@mwychung mwychung force-pushed the feature/doc-typing-12842 branch from 310b7d5 to b938e70 Compare November 29, 2024 08:33
@mwychung mwychung reopened this Nov 29, 2024
@mwychung
Copy link
Author

Thank you so much for all the helpful feedback and guidance! I’ve removed the unnecessary sections as suggested and have added a few more focused areas specific to pytest, such as typing with monkeypatch, parametrize, and tmp_path. I believe these changes better align with the needs of the readers and improve the relevance of the documentation. If any further adjustments are needed, please feel free to let me know—I’m happy to make additional updates. I truly appreciate your insights throughout this process, and I’m excited to continue learning and contributing!

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