You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/en/how-to/types.rst
+19-18Lines changed: 19 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,8 +40,8 @@ For example:
40
40
result = add(2, 3)
41
41
assert result ==5
42
42
43
-
- Here, `test_add` is annotated with `-> None`, as it does not return a value.
44
-
- While `-> None` typing may seem unnecessary, it ensures type checkers validate the function and helps identifying potential issues during refactoring.
43
+
Here, `test_add` is annotated with `-> None`, as it does not return a value.
44
+
While `-> None` typing may seem unnecessary, it ensures type checkers validate the function and helps identifying potential issues during refactoring.
45
45
46
46
47
47
Typing Fixtures
@@ -64,7 +64,7 @@ Adding type annotations to fixtures makes it clear what data they return, which
- Annotating fixtures with types like List[int] and Dict[str, int] ensures data consistency and helps prevent runtime errors when performing operations.
96
+
Annotating fixtures with types like List[int] and Dict[str, int] ensures data consistency and helps prevent runtime errors when performing operations.
97
97
This ensures that only `int` values are allowed in the list and that `str` keys map to `int` values in the dictionary, helping avoid type-related issues.
98
98
99
99
Typing Parameterized Tests
@@ -111,7 +111,7 @@ For example, you are testing if adding 1 to `input_value` results in `expected_o
- Here, typing clarifies that both `input_value` and `expected_output` are expected as integers, promoting consistency.
114
+
Here, typing clarifies that both `input_value` and `expected_output` are expected as integers, promoting consistency.
115
115
While parameterized tests can involve varied data types and that annotations simplify maintenance when datasets grow.
116
116
117
117
@@ -120,7 +120,7 @@ Typing for Monkeypatching
120
120
Monkeypatching modifies functions or environment variables during runtime.
121
121
Adding typing, such as `monkeypatch: pytest.MonkeyPatch`, clarifies the expected patching behaviour and reduces the risk of errors.
122
122
123
-
* Example of Typing Monkeypatching Environment Variables
123
+
* Example of Typing Monkeypatching Environment Variables
124
124
125
125
This example is based on the pytest documentation for `Monkeypatching <https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/monkeypatch.rst>`_, with the addition of typing annotations.
126
126
@@ -163,10 +163,10 @@ This example is based on the pytest documentation for `Monkeypatching <https://g
163
163
164
164
Here:
165
165
166
-
- **`username: Optional[str]`:** Indicates the variable `username` may either be a string or `None`.
167
-
- **`get_os_user_lower() -> str`:** Specifies this function will return a string, providing explicit return value type.
168
-
- **`monkeypatch` fixture is typed as `pytest.MonkeyPatch`:** Shows that it will provide an object for patching environment variables during the test. This clarifies the intended use of the fixture and helps developers to use it correctly.
169
-
- **Fixture return `-> None`, like `mock_env_user`:** Specifies they do not return any value, but instead modify the test environment.
166
+
- **username: Optional[str]:** Indicates the variable `username` may either be a string or `None`.
167
+
- **get_os_user_lower() -> str:** Specifies this function will return a string, providing explicit return value type.
168
+
- **monkeypatch fixture is typed as pytest.MonkeyPatch:** Shows that it will provide an object for patching environment variables during the test. This clarifies the intended use of the fixture and helps developers to use it correctly.
169
+
- **Fixture return -> None, like mock_env_user:** Specifies they do not return any value, but instead modify the test environment.
170
170
171
171
Typing annotations can also be extended to `monkeypatch` usage in pytest for class methods, instance attributes, or standalone functions.
172
172
This enhances type safety and clarity when patching the test environment.
@@ -179,9 +179,9 @@ The `tmp_path` and `tmpdir` fixtures provide these capabilities.
179
179
Adding typing annotations enhances clarity about the types of objects these fixtures return, which is particularly useful when performing file operations.
180
180
It also prevents misuse of monkeypatch by clarifies its API and expected inputs.
181
181
182
-
Below examples are based on the pytest documentation for `Temporary Directories and Files in tests <https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/tmp_path.rst>`, with the addition of typing annotations.
182
+
Below examples are based on the pytest documentation for `Temporary Directories and Files in tests <https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/tmp_path.rst>`_, with the addition of typing annotations.
183
183
184
-
* Typing with `tmp_path` for File Creation
184
+
* Typing with `tmp_path` for File Creation
185
185
186
186
.. code-block:: python
187
187
@@ -200,9 +200,9 @@ Below examples are based on the pytest documentation for `Temporary Directories
200
200
assert p.read_text(encoding="utf-8") ==CONTENT
201
201
assertlen(list(tmp_path.iterdir())) ==1
202
202
203
-
- Typing `tmp_path: Path` explicitly defines it as a Path object, improving code readability and catching type issues early.
203
+
Typing `tmp_path: Path` explicitly defines it as a Path object, improving code readability and catching type issues early.
204
204
205
-
* Typing with `tmp_path_factory` fixture for creating temporary files during a session
205
+
* Typing with `tmp_path_factory` fixture for creating temporary files during a session
206
206
207
207
.. code-block:: python
208
208
@@ -224,10 +224,11 @@ Below examples are based on the pytest documentation for `Temporary Directories
224
224
img = load_image(image_file)
225
225
# compute and test histogram
226
226
227
-
- **`tmp_path_factory: pytest.TempPathFactory`:** Indicates that `tmp_path_factory` is an instance of pytest’s `TempPathFactory`, responsible for creating temporary directories and paths during testing.
228
-
- **`fn: Path`:** Identifies that `fn` is a `Path` object, emphasizing its role as a file path and clarifying the expected file operations.
229
-
- ** Return type `-> Path`:** Specifies the fixture returns a `Path` object, clarifying its expected structure.
230
-
- **`image_file: Path`:** Defines `image_file` as a Path object, ensuring compatibility with `load_image`.
227
+
Here:
228
+
- **tmp_path_factory: pytest.TempPathFactory:** Indicates that `tmp_path_factory` is an instance of pytest’s `TempPathFactory`, responsible for creating temporary directories and paths during testing.
229
+
- **fn: Path:** Identifies that `fn` is a `Path` object, emphasizing its role as a file path and clarifying the expected file operations.
230
+
- **Return type -> Path:** Specifies the fixture returns a `Path` object, clarifying its expected structure.
231
+
- **image_file: Path:** Defines `image_file` as a Path object, ensuring compatibility with `load_image`.
0 commit comments