-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 (?)
(Also you amended an unrelated commit :) ) |
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! |
…pytest-dev#12954) * Avoid constructing a list of names * noqa a a suggestion in already nested for
09e6ee2
to
1d944c4
Compare
There was a problem hiding this 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:
- Refactoring: the type checker can easily point out the necessary changes in both code and tests, without needing to run the full test suite.
- 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 |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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).
2. Typing Test Functions | ||
----------------- |
There was a problem hiding this comment.
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:
2. Typing Test Functions | |
----------------- | |
2. Typing Test Functions | |
------------------------ |
|
||
2. Typing Test Functions | ||
----------------- | ||
Test functions in pytest check whether the code runs correctly. |
There was a problem hiding this comment.
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. 👍
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. |
There was a problem hiding this comment.
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.
* 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. |
There was a problem hiding this comment.
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.
In this function: | ||
`test_add` is typed as `-> None` because it does not return anything. |
There was a problem hiding this comment.
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.
Fix (#12842 )
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.
AUTHORS
.