Skip to content

Commit c105afb

Browse files
mwychungnicoddemus
authored andcommitted
Modified formatting of types documentation to types.rst
1 parent 335327c commit c105afb

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

doc/en/how-to/types.rst

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ For example:
4040
result = add(2, 3)
4141
assert result == 5
4242
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.
4545

4646

4747
Typing Fixtures
@@ -64,7 +64,7 @@ Adding type annotations to fixtures makes it clear what data they return, which
6464
def test_sample_fixture(sample_fixture: int) -> None:
6565
assert sample_fixture == 38
6666
67-
- Here, `sample_fixture()` is typed to return an `int`. This ensures consistency and helps identify mismatch types during refactoring.
67+
Here, `sample_fixture()` is typed to return an `int`. This ensures consistency and helps identify mismatch types during refactoring.
6868

6969

7070
* Typing Fixtures with Lists and Dictionaries
@@ -93,7 +93,7 @@ This example shows how to use List and Dict types in pytest.
9393
def test_sample_dict(sample_dict: Dict[str, int]) -> None:
9494
assert sample_dict["a"] == 50
9595
96-
- 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.
9797
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.
9898

9999
Typing Parameterized Tests
@@ -111,7 +111,7 @@ For example, you are testing if adding 1 to `input_value` results in `expected_o
111111
def test_increment(input_value: int, expected_output: int) -> None:
112112
assert input_value + 1 == expected_output
113113
114-
- 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.
115115
While parameterized tests can involve varied data types and that annotations simplify maintenance when datasets grow.
116116

117117

@@ -120,7 +120,7 @@ Typing for Monkeypatching
120120
Monkeypatching modifies functions or environment variables during runtime.
121121
Adding typing, such as `monkeypatch: pytest.MonkeyPatch`, clarifies the expected patching behaviour and reduces the risk of errors.
122122

123-
* Example of Typing Monkeypatching Environment Variables
123+
* Example of Typing Monkeypatching Environment Variables
124124

125125
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.
126126

@@ -163,10 +163,10 @@ This example is based on the pytest documentation for `Monkeypatching <https://g
163163
164164
Here:
165165

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

171171
Typing annotations can also be extended to `monkeypatch` usage in pytest for class methods, instance attributes, or standalone functions.
172172
This enhances type safety and clarity when patching the test environment.
@@ -179,9 +179,9 @@ The `tmp_path` and `tmpdir` fixtures provide these capabilities.
179179
Adding typing annotations enhances clarity about the types of objects these fixtures return, which is particularly useful when performing file operations.
180180
It also prevents misuse of monkeypatch by clarifies its API and expected inputs.
181181

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

184-
* Typing with `tmp_path` for File Creation
184+
* Typing with `tmp_path` for File Creation
185185

186186
.. code-block:: python
187187
@@ -200,9 +200,9 @@ Below examples are based on the pytest documentation for `Temporary Directories
200200
assert p.read_text(encoding="utf-8") == CONTENT
201201
assert len(list(tmp_path.iterdir())) == 1
202202
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.
204204

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
206206

207207
.. code-block:: python
208208
@@ -224,10 +224,11 @@ Below examples are based on the pytest documentation for `Temporary Directories
224224
img = load_image(image_file)
225225
# compute and test histogram
226226
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`.
231232

232233
Conclusion
233234
----------

0 commit comments

Comments
 (0)