|
6 | 6 | from typing import (
|
7 | 7 | IO, Any, AnyStr, Callable, Dict, Generic, Mapping, Match, NewType, Optional, Pattern, Tuple,
|
8 | 8 | Type, TypeVar, Union)
|
| 9 | +from unittest.mock import patch |
9 | 10 |
|
10 | 11 | import pytest
|
11 | 12 | import typing_extensions
|
@@ -227,17 +228,40 @@ def test_process_docstring_slot_wrapper():
|
227 | 228 | assert not lines
|
228 | 229 |
|
229 | 230 |
|
230 |
| -@pytest.mark.parametrize('always_document_param_types', [True, False]) |
231 |
| -@pytest.mark.sphinx('text', testroot='dummy') |
232 |
| -def test_sphinx_output(app, status, warning, always_document_param_types): |
| 231 | +def set_python_path(): |
233 | 232 | test_path = pathlib.Path(__file__).parent
|
234 | 233 |
|
235 | 234 | # Add test directory to sys.path to allow imports of dummy module.
|
236 | 235 | if str(test_path) not in sys.path:
|
237 | 236 | sys.path.insert(0, str(test_path))
|
238 | 237 |
|
| 238 | + |
| 239 | +def maybe_fix_py310(expected_contents): |
| 240 | + if sys.version_info[:2] >= (3, 10): |
| 241 | + for old, new in [ |
| 242 | + ("*str** | **None*", '"Optional"["str"]'), |
| 243 | + ("(*bool*)", '("bool")'), |
| 244 | + ("(*int*)", '("int")'), |
| 245 | + (" str", ' "str"'), |
| 246 | + ('"Optional"["str"]', '"Optional"["str"]'), |
| 247 | + ('"Optional"["Callable"[["int", "bytes"], "int"]]', |
| 248 | + '"Optional"["Callable"[["int", "bytes"], "int"]]'), |
| 249 | + ]: |
| 250 | + expected_contents = expected_contents.replace(old, new) |
| 251 | + return expected_contents |
| 252 | + |
| 253 | + |
| 254 | +@pytest.mark.parametrize('always_document_param_types', [True, False], |
| 255 | + ids=['doc_param_type', 'no_doc_param_type']) |
| 256 | +@pytest.mark.sphinx('text', testroot='dummy') |
| 257 | +@patch('sphinx.writers.text.MAXWIDTH', 2000) |
| 258 | +def test_sphinx_output(app, status, warning, always_document_param_types): |
| 259 | + set_python_path() |
| 260 | + |
239 | 261 | app.config.always_document_param_types = always_document_param_types
|
240 | 262 | app.config.autodoc_mock_imports = ['mailbox']
|
| 263 | + if sys.version_info < (3, 7): |
| 264 | + app.config.autodoc_mock_imports.append('dummy_module_future_annotations') |
241 | 265 | app.build()
|
242 | 266 |
|
243 | 267 | assert 'build succeeded' in status.getvalue() # Build succeeded
|
@@ -493,8 +517,7 @@ class dummy_module.ClassWithTypehintsNotInline(x=None)
|
493 | 517 | Method docstring.
|
494 | 518 |
|
495 | 519 | Parameters:
|
496 |
| - **x** ("Optional"["Callable"[["int", "bytes"], "int"]]) -- |
497 |
| - foo |
| 520 | + **x** ("Optional"["Callable"[["int", "bytes"], "int"]]) -- foo |
498 | 521 |
|
499 | 522 | Return type:
|
500 | 523 | "ClassWithTypehintsNotInline"
|
@@ -527,7 +550,43 @@ class dummy_module.DataClass(x)
|
527 | 550 | **x** ("Mailbox") -- function
|
528 | 551 | ''')
|
529 | 552 | expected_contents = expected_contents.format(**format_args).replace('–', '--')
|
530 |
| - assert text_contents == expected_contents |
| 553 | + assert text_contents == maybe_fix_py310(expected_contents) |
| 554 | + |
| 555 | + |
| 556 | +@pytest.mark.skipif(sys.version_info < (3, 7), |
| 557 | + reason="Future annotations are not implemented in Python < 3.7") |
| 558 | +@pytest.mark.sphinx('text', testroot='dummy') |
| 559 | +@patch('sphinx.writers.text.MAXWIDTH', 2000) |
| 560 | +def test_sphinx_output_future_annotations(app, status, warning): |
| 561 | + set_python_path() |
| 562 | + |
| 563 | + app.config.master_doc = "future_annotations" |
| 564 | + app.build() |
| 565 | + |
| 566 | + assert 'build succeeded' in status.getvalue() # Build succeeded |
| 567 | + |
| 568 | + text_path = pathlib.Path(app.srcdir) / '_build' / 'text' / 'future_annotations.txt' |
| 569 | + with text_path.open('r') as f: |
| 570 | + text_contents = f.read().replace('–', '--') |
| 571 | + expected_contents = textwrap.dedent('''\ |
| 572 | + Dummy Module |
| 573 | + ************ |
| 574 | +
|
| 575 | + dummy_module_future_annotations.function_with_py310_annotations(self, x, y, z=None) |
| 576 | +
|
| 577 | + Method docstring. |
| 578 | +
|
| 579 | + Parameters: |
| 580 | + * **x** (*bool*) -- foo |
| 581 | +
|
| 582 | + * **y** (*int*) -- bar |
| 583 | +
|
| 584 | + * **z** (*str** | **None*) -- baz |
| 585 | +
|
| 586 | + Return type: |
| 587 | + str |
| 588 | + ''') |
| 589 | + assert text_contents == maybe_fix_py310(expected_contents) |
531 | 590 |
|
532 | 591 |
|
533 | 592 | def test_normalize_source_lines_async_def():
|
|
0 commit comments