diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-05 16:20:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-05 16:20:58 +0000 |
commit | 5bb0bb4be543fd5eca41673696a62ed80d493591 (patch) | |
tree | ad2c464f140e86c7f178a6276d7ea4a93e3e6c92 /tests/test_util | |
parent | Adding upstream version 7.2.6. (diff) | |
download | sphinx-upstream/7.3.7.tar.xz sphinx-upstream/7.3.7.zip |
Adding upstream version 7.3.7.upstream/7.3.7
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | tests/test_util/__init__.py | 0 | ||||
-rw-r--r-- | tests/test_util/intersphinx_data.py | 52 | ||||
-rw-r--r-- | tests/test_util/test_util.py (renamed from tests/test_util.py) | 0 | ||||
-rw-r--r-- | tests/test_util/test_util_console.py | 90 | ||||
-rw-r--r-- | tests/test_util/test_util_display.py (renamed from tests/test_util_display.py) | 22 | ||||
-rw-r--r-- | tests/test_util/test_util_docstrings.py (renamed from tests/test_util_docstrings.py) | 0 | ||||
-rw-r--r-- | tests/test_util/test_util_docutils.py (renamed from tests/test_util_docutils.py) | 0 | ||||
-rw-r--r-- | tests/test_util/test_util_fileutil.py (renamed from tests/test_util_fileutil.py) | 0 | ||||
-rw-r--r-- | tests/test_util/test_util_i18n.py (renamed from tests/test_util_i18n.py) | 2 | ||||
-rw-r--r-- | tests/test_util/test_util_images.py (renamed from tests/test_util_images.py) | 0 | ||||
-rw-r--r-- | tests/test_util/test_util_inspect.py (renamed from tests/test_util_inspect.py) | 118 | ||||
-rw-r--r-- | tests/test_util/test_util_inventory.py (renamed from tests/test_util_inventory.py) | 60 | ||||
-rw-r--r-- | tests/test_util/test_util_logging.py (renamed from tests/test_util_logging.py) | 24 | ||||
-rw-r--r-- | tests/test_util/test_util_matching.py (renamed from tests/test_util_matching.py) | 0 | ||||
-rw-r--r-- | tests/test_util/test_util_nodes.py (renamed from tests/test_util_nodes.py) | 4 | ||||
-rw-r--r-- | tests/test_util/test_util_rst.py (renamed from tests/test_util_rst.py) | 0 | ||||
-rw-r--r-- | tests/test_util/test_util_template.py (renamed from tests/test_util_template.py) | 0 | ||||
-rw-r--r-- | tests/test_util/test_util_typing.py (renamed from tests/test_util_typing.py) | 243 | ||||
-rw-r--r-- | tests/test_util/typing_test_data.py (renamed from tests/typing_test_data.py) | 4 |
19 files changed, 412 insertions, 207 deletions
diff --git a/tests/test_util/__init__.py b/tests/test_util/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/test_util/__init__.py diff --git a/tests/test_util/intersphinx_data.py b/tests/test_util/intersphinx_data.py new file mode 100644 index 0000000..042ee76 --- /dev/null +++ b/tests/test_util/intersphinx_data.py @@ -0,0 +1,52 @@ +from __future__ import annotations + +import zlib +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Final + +INVENTORY_V1: Final[bytes] = b'''\ +# Sphinx inventory version 1 +# Project: foo +# Version: 1.0 +module mod foo.html +module.cls class foo.html +''' + +INVENTORY_V2: Final[bytes] = b'''\ +# Sphinx inventory version 2 +# Project: foo +# Version: 2.0 +# The remainder of this file is compressed with zlib. +''' + zlib.compress(b'''\ +module1 py:module 0 foo.html#module-module1 Long Module desc +module2 py:module 0 foo.html#module-$ - +module1.func py:function 1 sub/foo.html#$ - +module1.Foo.bar py:method 1 index.html#foo.Bar.baz - +CFunc c:function 2 cfunc.html#CFunc - +std cpp:type 1 index.html#std - +std::uint8_t cpp:type 1 index.html#std_uint8_t - +foo::Bar cpp:class 1 index.html#cpp_foo_bar - +foo::Bar::baz cpp:function 1 index.html#cpp_foo_bar_baz - +foons cpp:type 1 index.html#foons - +foons::bartype cpp:type 1 index.html#foons_bartype - +a term std:term -1 glossary.html#term-a-term - +ls.-l std:cmdoption 1 index.html#cmdoption-ls-l - +docname std:doc -1 docname.html - +foo js:module 1 index.html#foo - +foo.bar js:class 1 index.html#foo.bar - +foo.bar.baz js:method 1 index.html#foo.bar.baz - +foo.bar.qux js:data 1 index.html#foo.bar.qux - +a term including:colon std:term -1 glossary.html#term-a-term-including-colon - +The-Julia-Domain std:label -1 write_inventory/#$ The Julia Domain +''') + +INVENTORY_V2_NO_VERSION: Final[bytes] = b'''\ +# Sphinx inventory version 2 +# Project: foo +# Version: +# The remainder of this file is compressed with zlib. +''' + zlib.compress(b'''\ +module1 py:module 0 foo.html#module-module1 Long Module desc +''') diff --git a/tests/test_util.py b/tests/test_util/test_util.py index 4389894..4389894 100644 --- a/tests/test_util.py +++ b/tests/test_util/test_util.py diff --git a/tests/test_util/test_util_console.py b/tests/test_util/test_util_console.py new file mode 100644 index 0000000..b617a33 --- /dev/null +++ b/tests/test_util/test_util_console.py @@ -0,0 +1,90 @@ +from __future__ import annotations + +import itertools +import operator +from typing import TYPE_CHECKING + +import pytest + +from sphinx.util.console import blue, reset, strip_colors, strip_escape_sequences + +if TYPE_CHECKING: + from collections.abc import Callable, Sequence + from typing import Final, TypeVar + + _T = TypeVar('_T') + +CURSOR_UP: Final[str] = '\x1b[2A' # ignored ANSI code +ERASE_LINE: Final[str] = '\x1b[2K' # supported ANSI code +TEXT: Final[str] = '\x07 Hello world!' + + +@pytest.mark.parametrize( + ('strip_function', 'ansi_base_blocks', 'text_base_blocks'), + [ + ( + strip_colors, + # double ERASE_LINE so that the tested strings may have 2 of them + [TEXT, blue(TEXT), reset(TEXT), ERASE_LINE, ERASE_LINE, CURSOR_UP], + # :func:`strip_colors` removes color codes but keeps ERASE_LINE and CURSOR_UP + [TEXT, TEXT, TEXT, ERASE_LINE, ERASE_LINE, CURSOR_UP], + ), + ( + strip_escape_sequences, + # double ERASE_LINE so that the tested strings may have 2 of them + [TEXT, blue(TEXT), reset(TEXT), ERASE_LINE, ERASE_LINE, CURSOR_UP], + # :func:`strip_escape_sequences` strips ANSI codes known by Sphinx + [TEXT, TEXT, TEXT, '', '', CURSOR_UP], + ), + ], + ids=[strip_colors.__name__, strip_escape_sequences.__name__], +) +def test_strip_ansi( + strip_function: Callable[[str], str], + ansi_base_blocks: Sequence[str], + text_base_blocks: Sequence[str], +) -> None: + assert callable(strip_function) + assert len(text_base_blocks) == len(ansi_base_blocks) + N = len(ansi_base_blocks) + + def next_ansi_blocks(choices: Sequence[str], n: int) -> Sequence[str]: + # Get a list of *n* words from a cyclic sequence of *choices*. + # + # For instance ``next_ansi_blocks(['a', 'b'], 3) == ['a', 'b', 'a']``. + stream = itertools.cycle(choices) + return list(map(operator.itemgetter(0), zip(stream, range(n)))) + + # generate all permutations of length N + for sigma in itertools.permutations(range(N), N): + # apply the permutation on the blocks with ANSI codes + ansi_blocks = list(map(ansi_base_blocks.__getitem__, sigma)) + # apply the permutation on the blocks with stripped codes + text_blocks = list(map(text_base_blocks.__getitem__, sigma)) + + for glue, n in itertools.product(['.', '\n', '\r\n'], range(4 * N)): + ansi_strings = next_ansi_blocks(ansi_blocks, n) + text_strings = next_ansi_blocks(text_blocks, n) + assert len(ansi_strings) == len(text_strings) == n + + ansi_string = glue.join(ansi_strings) + text_string = glue.join(text_strings) + assert strip_function(ansi_string) == text_string + + +def test_strip_ansi_short_forms(): + # In Sphinx, we always "normalize" the color codes so that they + # match "\x1b\[(\d\d;){0,2}(\d\d)m" but it might happen that + # some messages use '\x1b[0m' instead of ``reset(s)``, so we + # test whether this alternative form is supported or not. + + for strip_function in [strip_colors, strip_escape_sequences]: + # \x1b[m and \x1b[0m are equivalent to \x1b[00m + assert strip_function('\x1b[m') == '' + assert strip_function('\x1b[0m') == '' + + # \x1b[1m is equivalent to \x1b[01m + assert strip_function('\x1b[1mbold\x1b[0m') == 'bold' + + # \x1b[K is equivalent to \x1b[0K + assert strip_escape_sequences('\x1b[K') == '' diff --git a/tests/test_util_display.py b/tests/test_util/test_util_display.py index 9ecdd6a..a18fa1e 100644 --- a/tests/test_util_display.py +++ b/tests/test_util/test_util_display.py @@ -2,8 +2,8 @@ import pytest -from sphinx.testing.util import strip_escseq from sphinx.util import logging +from sphinx.util.console import strip_colors from sphinx.util.display import ( SkipProgressMessage, display_chunk, @@ -28,13 +28,14 @@ def test_status_iterator_length_0(app, status, warning): status.seek(0) status.truncate(0) yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ')) - output = strip_escseq(status.getvalue()) + output = strip_colors(status.getvalue()) assert 'testing ... hello sphinx world \n' in output assert yields == ['hello', 'sphinx', 'world'] @pytest.mark.sphinx('dummy') -def test_status_iterator_verbosity_0(app, status, warning): +def test_status_iterator_verbosity_0(app, status, warning, monkeypatch): + monkeypatch.setenv("FORCE_COLOR", "1") logging.setup(app, status, warning) # test for status_iterator (verbosity=0) @@ -42,7 +43,7 @@ def test_status_iterator_verbosity_0(app, status, warning): status.truncate(0) yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ', length=3, verbosity=0)) - output = strip_escseq(status.getvalue()) + output = strip_colors(status.getvalue()) assert 'testing ... [ 33%] hello\r' in output assert 'testing ... [ 67%] sphinx\r' in output assert 'testing ... [100%] world\r\n' in output @@ -50,7 +51,8 @@ def test_status_iterator_verbosity_0(app, status, warning): @pytest.mark.sphinx('dummy') -def test_status_iterator_verbosity_1(app, status, warning): +def test_status_iterator_verbosity_1(app, status, warning, monkeypatch): + monkeypatch.setenv("FORCE_COLOR", "1") logging.setup(app, status, warning) # test for status_iterator (verbosity=1) @@ -58,7 +60,7 @@ def test_status_iterator_verbosity_1(app, status, warning): status.truncate(0) yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ', length=3, verbosity=1)) - output = strip_escseq(status.getvalue()) + output = strip_colors(status.getvalue()) assert 'testing ... [ 33%] hello\n' in output assert 'testing ... [ 67%] sphinx\n' in output assert 'testing ... [100%] world\n\n' in output @@ -73,14 +75,14 @@ def test_progress_message(app, status, warning): with progress_message('testing'): logger.info('blah ', nonl=True) - output = strip_escseq(status.getvalue()) + output = strip_colors(status.getvalue()) assert 'testing... blah done\n' in output # skipping case with progress_message('testing'): raise SkipProgressMessage('Reason: %s', 'error') # NoQA: EM101 - output = strip_escseq(status.getvalue()) + output = strip_colors(status.getvalue()) assert 'testing... skipped\nReason: error\n' in output # error case @@ -90,7 +92,7 @@ def test_progress_message(app, status, warning): except Exception: pass - output = strip_escseq(status.getvalue()) + output = strip_colors(status.getvalue()) assert 'testing... failed\n' in output # decorator @@ -99,5 +101,5 @@ def test_progress_message(app, status, warning): logger.info('in func ', nonl=True) func() - output = strip_escseq(status.getvalue()) + output = strip_colors(status.getvalue()) assert 'testing... in func done\n' in output diff --git a/tests/test_util_docstrings.py b/tests/test_util/test_util_docstrings.py index 813e84e..813e84e 100644 --- a/tests/test_util_docstrings.py +++ b/tests/test_util/test_util_docstrings.py diff --git a/tests/test_util_docutils.py b/tests/test_util/test_util_docutils.py index 69999eb..69999eb 100644 --- a/tests/test_util_docutils.py +++ b/tests/test_util/test_util_docutils.py diff --git a/tests/test_util_fileutil.py b/tests/test_util/test_util_fileutil.py index 9c23821..9c23821 100644 --- a/tests/test_util_fileutil.py +++ b/tests/test_util/test_util_fileutil.py diff --git a/tests/test_util_i18n.py b/tests/test_util/test_util_i18n.py index 9a1ecc5..f6baa04 100644 --- a/tests/test_util_i18n.py +++ b/tests/test_util/test_util_i18n.py @@ -160,6 +160,8 @@ def test_CatalogRepository(tmp_path): (tmp_path / 'loc2' / 'xx' / 'LC_MESSAGES').mkdir(parents=True, exist_ok=True) (tmp_path / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test1.po').write_text('#', encoding='utf8') (tmp_path / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test7.po').write_text('#', encoding='utf8') + (tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / '.dotdir2').mkdir(parents=True, exist_ok=True) + (tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / '.dotdir2' / 'test8.po').write_text('#', encoding='utf8') # for language xx repo = i18n.CatalogRepository(tmp_path, ['loc1', 'loc2'], 'xx', 'utf-8') diff --git a/tests/test_util_images.py b/tests/test_util/test_util_images.py index 15853c7..15853c7 100644 --- a/tests/test_util_images.py +++ b/tests/test_util/test_util_images.py diff --git a/tests/test_util_inspect.py b/tests/test_util/test_util_inspect.py index 73f9656..32840b8 100644 --- a/tests/test_util_inspect.py +++ b/tests/test_util/test_util_inspect.py @@ -62,6 +62,7 @@ async def coroutinefunc(): async def asyncgenerator(): yield + partial_func = functools.partial(func) partial_coroutinefunc = functools.partial(coroutinefunc) @@ -222,168 +223,140 @@ def test_signature_partialmethod(): def test_signature_annotations(): - from .typing_test_data import ( - Node, - f0, - f1, - f2, - f3, - f4, - f5, - f6, - f7, - f8, - f9, - f10, - f11, - f12, - f13, - f14, - f15, - f16, - f17, - f18, - f19, - f20, - f21, - f22, - f23, - f24, - f25, - ) + import tests.test_util.typing_test_data as mod # Class annotations - sig = inspect.signature(f0) + sig = inspect.signature(mod.f0) assert stringify_signature(sig) == '(x: int, y: numbers.Integral) -> None' # Generic types with concrete parameters - sig = inspect.signature(f1) + sig = inspect.signature(mod.f1) assert stringify_signature(sig) == '(x: list[int]) -> typing.List[int]' # TypeVars and generic types with TypeVars - sig = inspect.signature(f2) - assert stringify_signature(sig) == ('(x: typing.List[tests.typing_test_data.T],' - ' y: typing.List[tests.typing_test_data.T_co],' - ' z: tests.typing_test_data.T' - ') -> typing.List[tests.typing_test_data.T_contra]') + sig = inspect.signature(mod.f2) + assert stringify_signature(sig) == ('(x: typing.List[tests.test_util.typing_test_data.T],' + ' y: typing.List[tests.test_util.typing_test_data.T_co],' + ' z: tests.test_util.typing_test_data.T' + ') -> typing.List[tests.test_util.typing_test_data.T_contra]') # Union types - sig = inspect.signature(f3) + sig = inspect.signature(mod.f3) assert stringify_signature(sig) == '(x: str | numbers.Integral) -> None' # Quoted annotations - sig = inspect.signature(f4) + sig = inspect.signature(mod.f4) assert stringify_signature(sig) == '(x: str, y: str) -> None' # Keyword-only arguments - sig = inspect.signature(f5) + sig = inspect.signature(mod.f5) assert stringify_signature(sig) == '(x: int, *, y: str, z: str) -> None' # Keyword-only arguments with varargs - sig = inspect.signature(f6) + sig = inspect.signature(mod.f6) assert stringify_signature(sig) == '(x: int, *args, y: str, z: str) -> None' # Space around '=' for defaults - sig = inspect.signature(f7) + sig = inspect.signature(mod.f7) if sys.version_info[:2] <= (3, 10): assert stringify_signature(sig) == '(x: int | None = None, y: dict = {}) -> None' else: assert stringify_signature(sig) == '(x: int = None, y: dict = {}) -> None' # Callable types - sig = inspect.signature(f8) + sig = inspect.signature(mod.f8) assert stringify_signature(sig) == '(x: typing.Callable[[int, str], int]) -> None' - sig = inspect.signature(f9) + sig = inspect.signature(mod.f9) assert stringify_signature(sig) == '(x: typing.Callable) -> None' # Tuple types - sig = inspect.signature(f10) + sig = inspect.signature(mod.f10) assert stringify_signature(sig) == '(x: typing.Tuple[int, str], y: typing.Tuple[int, ...]) -> None' # Instance annotations - sig = inspect.signature(f11) + sig = inspect.signature(mod.f11) assert stringify_signature(sig) == '(x: CustomAnnotation, y: 123) -> None' # tuple with more than two items - sig = inspect.signature(f12) + sig = inspect.signature(mod.f12) assert stringify_signature(sig) == '() -> typing.Tuple[int, str, int]' # optional - sig = inspect.signature(f13) + sig = inspect.signature(mod.f13) assert stringify_signature(sig) == '() -> str | None' # optional union - sig = inspect.signature(f20) + sig = inspect.signature(mod.f20) assert stringify_signature(sig) in ('() -> int | str | None', '() -> str | int | None') # Any - sig = inspect.signature(f14) + sig = inspect.signature(mod.f14) assert stringify_signature(sig) == '() -> typing.Any' # ForwardRef - sig = inspect.signature(f15) + sig = inspect.signature(mod.f15) assert stringify_signature(sig) == '(x: Unknown, y: int) -> typing.Any' # keyword only arguments (1) - sig = inspect.signature(f16) + sig = inspect.signature(mod.f16) assert stringify_signature(sig) == '(arg1, arg2, *, arg3=None, arg4=None)' # keyword only arguments (2) - sig = inspect.signature(f17) + sig = inspect.signature(mod.f17) assert stringify_signature(sig) == '(*, arg3, arg4)' - sig = inspect.signature(f18) + sig = inspect.signature(mod.f18) assert stringify_signature(sig) == ('(self, arg1: int | typing.Tuple = 10) -> ' 'typing.List[typing.Dict]') # annotations for variadic and keyword parameters - sig = inspect.signature(f19) + sig = inspect.signature(mod.f19) assert stringify_signature(sig) == '(*args: int, **kwargs: str)' # default value is inspect.Signature.empty - sig = inspect.signature(f21) + sig = inspect.signature(mod.f21) assert stringify_signature(sig) == "(arg1='whatever', arg2)" # type hints by string - sig = inspect.signature(Node.children) - assert stringify_signature(sig) == '(self) -> typing.List[tests.typing_test_data.Node]' + sig = inspect.signature(mod.Node.children) + assert stringify_signature(sig) == '(self) -> typing.List[tests.test_util.typing_test_data.Node]' - sig = inspect.signature(Node.__init__) - assert stringify_signature(sig) == '(self, parent: tests.typing_test_data.Node | None) -> None' + sig = inspect.signature(mod.Node.__init__) + assert stringify_signature(sig) == '(self, parent: tests.test_util.typing_test_data.Node | None) -> None' # show_annotation is False - sig = inspect.signature(f7) + sig = inspect.signature(mod.f7) assert stringify_signature(sig, show_annotation=False) == '(x=None, y={})' # show_return_annotation is False - sig = inspect.signature(f7) + sig = inspect.signature(mod.f7) if sys.version_info[:2] <= (3, 10): assert stringify_signature(sig, show_return_annotation=False) == '(x: int | None = None, y: dict = {})' else: assert stringify_signature(sig, show_return_annotation=False) == '(x: int = None, y: dict = {})' # unqualified_typehints is True - sig = inspect.signature(f7) + sig = inspect.signature(mod.f7) if sys.version_info[:2] <= (3, 10): assert stringify_signature(sig, unqualified_typehints=True) == '(x: int | None = None, y: dict = {}) -> None' else: assert stringify_signature(sig, unqualified_typehints=True) == '(x: int = None, y: dict = {}) -> None' # case: separator at head - sig = inspect.signature(f22) + sig = inspect.signature(mod.f22) assert stringify_signature(sig) == '(*, a, b)' # case: separator in the middle - sig = inspect.signature(f23) + sig = inspect.signature(mod.f23) assert stringify_signature(sig) == '(a, b, /, c, d)' - sig = inspect.signature(f24) + sig = inspect.signature(mod.f24) assert stringify_signature(sig) == '(a, /, *, b)' # case: separator at tail - sig = inspect.signature(f25) + sig = inspect.signature(mod.f25) assert stringify_signature(sig) == '(a, b, /)' @@ -667,6 +640,17 @@ def test_object_description_enum(): assert inspect.object_description(MyEnum.FOO) == "MyEnum.FOO" +def test_object_description_enum_custom_repr(): + class MyEnum(enum.Enum): + FOO = 1 + BAR = 2 + + def __repr__(self): + return self.name + + assert inspect.object_description(MyEnum.FOO) == "FOO" + + def test_getslots(): class Foo: pass @@ -842,7 +826,7 @@ def test_getdoc_inherited_decorated_method(): """ class Bar(Foo): - @functools.lru_cache # noqa: B019 + @functools.lru_cache # NoQA: B019 def meth(self): # inherited and decorated method pass diff --git a/tests/test_util_inventory.py b/tests/test_util/test_util_inventory.py index 2c20763..81d31b0 100644 --- a/tests/test_util_inventory.py +++ b/tests/test_util/test_util_inventory.py @@ -1,59 +1,21 @@ """Test inventory util functions.""" import os import posixpath -import zlib from io import BytesIO +import sphinx.locale from sphinx.testing.util import SphinxTestApp from sphinx.util.inventory import InventoryFile -inventory_v1 = b'''\ -# Sphinx inventory version 1 -# Project: foo -# Version: 1.0 -module mod foo.html -module.cls class foo.html -''' - -inventory_v2 = b'''\ -# Sphinx inventory version 2 -# Project: foo -# Version: 2.0 -# The remainder of this file is compressed with zlib. -''' + zlib.compress(b'''\ -module1 py:module 0 foo.html#module-module1 Long Module desc -module2 py:module 0 foo.html#module-$ - -module1.func py:function 1 sub/foo.html#$ - -module1.Foo.bar py:method 1 index.html#foo.Bar.baz - -CFunc c:function 2 cfunc.html#CFunc - -std cpp:type 1 index.html#std - -std::uint8_t cpp:type 1 index.html#std_uint8_t - -foo::Bar cpp:class 1 index.html#cpp_foo_bar - -foo::Bar::baz cpp:function 1 index.html#cpp_foo_bar_baz - -foons cpp:type 1 index.html#foons - -foons::bartype cpp:type 1 index.html#foons_bartype - -a term std:term -1 glossary.html#term-a-term - -ls.-l std:cmdoption 1 index.html#cmdoption-ls-l - -docname std:doc -1 docname.html - -foo js:module 1 index.html#foo - -foo.bar js:class 1 index.html#foo.bar - -foo.bar.baz js:method 1 index.html#foo.bar.baz - -foo.bar.qux js:data 1 index.html#foo.bar.qux - -a term including:colon std:term -1 glossary.html#term-a-term-including-colon - -''') - -inventory_v2_not_having_version = b'''\ -# Sphinx inventory version 2 -# Project: foo -# Version: -# The remainder of this file is compressed with zlib. -''' + zlib.compress(b'''\ -module1 py:module 0 foo.html#module-module1 Long Module desc -''') +from tests.test_util.intersphinx_data import ( + INVENTORY_V1, + INVENTORY_V2, + INVENTORY_V2_NO_VERSION, +) def test_read_inventory_v1(): - f = BytesIO(inventory_v1) + f = BytesIO(INVENTORY_V1) invdata = InventoryFile.load(f, '/util', posixpath.join) assert invdata['py:module']['module'] == \ ('foo', '1.0', '/util/foo.html#module-module', '-') @@ -62,7 +24,7 @@ def test_read_inventory_v1(): def test_read_inventory_v2(): - f = BytesIO(inventory_v2) + f = BytesIO(INVENTORY_V2) invdata = InventoryFile.load(f, '/util', posixpath.join) assert len(invdata['py:module']) == 2 @@ -80,7 +42,7 @@ def test_read_inventory_v2(): def test_read_inventory_v2_not_having_version(): - f = BytesIO(inventory_v2_not_having_version) + f = BytesIO(INVENTORY_V2_NO_VERSION) invdata = InventoryFile.load(f, '/util', posixpath.join) assert invdata['py:module']['module1'] == \ ('foo', '', '/util/foo.html#module-module1', 'Long Module desc') @@ -99,8 +61,8 @@ def _write_appconfig(dir, language, prefix=None): def _build_inventory(srcdir): app = SphinxTestApp(srcdir=srcdir) app.build() - app.cleanup() - return (app.outdir / 'objects.inv') + sphinx.locale.translators.clear() + return app.outdir / 'objects.inv' def test_inventory_localization(tmp_path): diff --git a/tests/test_util_logging.py b/tests/test_util/test_util_logging.py index 4d506a8..4ee548a 100644 --- a/tests/test_util_logging.py +++ b/tests/test_util/test_util_logging.py @@ -8,9 +8,8 @@ import pytest from docutils import nodes from sphinx.errors import SphinxWarning -from sphinx.testing.util import strip_escseq from sphinx.util import logging, osutil -from sphinx.util.console import colorize +from sphinx.util.console import colorize, strip_colors from sphinx.util.logging import is_suppressed_warning, prefixed_warnings from sphinx.util.parallel import ParallelTasks @@ -110,7 +109,7 @@ def test_once_warning_log(app, status, warning): logger.warning('message: %d', 1, once=True) logger.warning('message: %d', 2, once=True) - assert 'WARNING: message: 1\nWARNING: message: 2\n' in strip_escseq(warning.getvalue()) + assert 'WARNING: message: 1\nWARNING: message: 2\n' in strip_colors(warning.getvalue()) def test_is_suppressed_warning(): @@ -278,7 +277,7 @@ def test_pending_warnings(app, status, warning): assert 'WARNING: message3' not in warning.getvalue() # actually logged as ordered - assert 'WARNING: message2\nWARNING: message3' in strip_escseq(warning.getvalue()) + assert 'WARNING: message2\nWARNING: message3' in strip_colors(warning.getvalue()) def test_colored_logs(app, status, warning): @@ -396,3 +395,20 @@ def test_get_node_location_abspath(): location = logging.get_node_location(n) assert location == absolute_filename + ':' + + +@pytest.mark.sphinx(confoverrides={'show_warning_types': True}) +def test_show_warning_types(app, status, warning): + logging.setup(app, status, warning) + logger = logging.getLogger(__name__) + logger.warning('message2') + logger.warning('message3', type='test') + logger.warning('message4', type='test', subtype='logging') + + warnings = strip_colors(warning.getvalue()).splitlines() + + assert warnings == [ + 'WARNING: message2', + 'WARNING: message3 [test]', + 'WARNING: message4 [test.logging]', + ] diff --git a/tests/test_util_matching.py b/tests/test_util/test_util_matching.py index 7d865ba..7d865ba 100644 --- a/tests/test_util_matching.py +++ b/tests/test_util/test_util_matching.py diff --git a/tests/test_util_nodes.py b/tests/test_util/test_util_nodes.py index 92e4dc1..ddd5974 100644 --- a/tests/test_util_nodes.py +++ b/tests/test_util/test_util_nodes.py @@ -237,8 +237,8 @@ def test_split_explicit_target(title, expected): def test_apply_source_workaround_literal_block_no_source(): """Regression test for #11091. - Test that apply_source_workaround doesn't raise. - """ + Test that apply_source_workaround doesn't raise. + """ literal_block = nodes.literal_block('', '') list_item = nodes.list_item('', literal_block) bullet_list = nodes.bullet_list('', list_item) diff --git a/tests/test_util_rst.py b/tests/test_util/test_util_rst.py index d50c90c..d50c90c 100644 --- a/tests/test_util_rst.py +++ b/tests/test_util/test_util_rst.py diff --git a/tests/test_util_template.py b/tests/test_util/test_util_template.py index 4601179..4601179 100644 --- a/tests/test_util_template.py +++ b/tests/test_util/test_util_template.py diff --git a/tests/test_util_typing.py b/tests/test_util/test_util_typing.py index d79852e..9c28029 100644 --- a/tests/test_util_typing.py +++ b/tests/test_util/test_util_typing.py @@ -1,15 +1,38 @@ """Tests util.typing functions.""" import sys +from contextvars import Context, ContextVar, Token from enum import Enum from numbers import Integral from struct import Struct -from types import TracebackType +from types import ( + AsyncGeneratorType, + BuiltinFunctionType, + BuiltinMethodType, + CellType, + ClassMethodDescriptorType, + CodeType, + CoroutineType, + FrameType, + FunctionType, + GeneratorType, + GetSetDescriptorType, + LambdaType, + MappingProxyType, + MemberDescriptorType, + MethodDescriptorType, + MethodType, + MethodWrapperType, + ModuleType, + TracebackType, + WrapperDescriptorType, +) from typing import ( Any, Callable, Dict, Generator, + Iterator, List, NewType, Optional, @@ -21,7 +44,7 @@ from typing import ( import pytest from sphinx.ext.autodoc import mock -from sphinx.util.typing import INVALID_BUILTIN_CLASSES, restify, stringify_annotation +from sphinx.util.typing import _INVALID_BUILTIN_CLASSES, restify, stringify_annotation class MyClass1: @@ -76,11 +99,55 @@ def test_restify(): def test_is_invalid_builtin_class(): # if these tests start failing, it means that the __module__ - # of one of these classes has changed, and INVALID_BUILTIN_CLASSES + # of one of these classes has changed, and _INVALID_BUILTIN_CLASSES # in sphinx.util.typing needs to be updated. - assert INVALID_BUILTIN_CLASSES.keys() == {Struct, TracebackType} + assert _INVALID_BUILTIN_CLASSES.keys() == { + Context, + ContextVar, + Token, + Struct, + AsyncGeneratorType, + BuiltinFunctionType, + BuiltinMethodType, + CellType, + ClassMethodDescriptorType, + CodeType, + CoroutineType, + FrameType, + FunctionType, + GeneratorType, + GetSetDescriptorType, + LambdaType, + MappingProxyType, + MemberDescriptorType, + MethodDescriptorType, + MethodType, + MethodWrapperType, + ModuleType, + TracebackType, + WrapperDescriptorType, + } assert Struct.__module__ == '_struct' + assert AsyncGeneratorType.__module__ == 'builtins' + assert BuiltinFunctionType.__module__ == 'builtins' + assert BuiltinMethodType.__module__ == 'builtins' + assert CellType.__module__ == 'builtins' + assert ClassMethodDescriptorType.__module__ == 'builtins' + assert CodeType.__module__ == 'builtins' + assert CoroutineType.__module__ == 'builtins' + assert FrameType.__module__ == 'builtins' + assert FunctionType.__module__ == 'builtins' + assert GeneratorType.__module__ == 'builtins' + assert GetSetDescriptorType.__module__ == 'builtins' + assert LambdaType.__module__ == 'builtins' + assert MappingProxyType.__module__ == 'builtins' + assert MemberDescriptorType.__module__ == 'builtins' + assert MethodDescriptorType.__module__ == 'builtins' + assert MethodType.__module__ == 'builtins' + assert MethodWrapperType.__module__ == 'builtins' + assert ModuleType.__module__ == 'builtins' assert TracebackType.__module__ == 'builtins' + assert WrapperDescriptorType.__module__ == 'builtins' def test_restify_type_hints_containers(): @@ -103,12 +170,14 @@ def test_restify_type_hints_containers(): assert restify(List[Dict[str, Tuple]]) == (":py:class:`~typing.List`\\ " "[:py:class:`~typing.Dict`\\ " "[:py:class:`str`, :py:class:`~typing.Tuple`]]") - assert restify(MyList[Tuple[int, int]]) == (":py:class:`tests.test_util_typing.MyList`\\ " + assert restify(MyList[Tuple[int, int]]) == (":py:class:`tests.test_util.test_util_typing.MyList`\\ " "[:py:class:`~typing.Tuple`\\ " "[:py:class:`int`, :py:class:`int`]]") assert restify(Generator[None, None, None]) == (":py:class:`~typing.Generator`\\ " "[:py:obj:`None`, :py:obj:`None`, " ":py:obj:`None`]") + assert restify(Iterator[None]) == (":py:class:`~typing.Iterator`\\ " + "[:py:obj:`None`]") def test_restify_type_hints_Callable(): @@ -121,24 +190,44 @@ def test_restify_type_hints_Callable(): def test_restify_type_hints_Union(): - assert restify(Optional[int]) == ":py:obj:`~typing.Optional`\\ [:py:class:`int`]" - assert restify(Union[str, None]) == ":py:obj:`~typing.Optional`\\ [:py:class:`str`]" - assert restify(Union[int, str]) == (":py:obj:`~typing.Union`\\ " - "[:py:class:`int`, :py:class:`str`]") - assert restify(Union[int, Integral]) == (":py:obj:`~typing.Union`\\ " - "[:py:class:`int`, :py:class:`numbers.Integral`]") - assert restify(Union[int, Integral], "smart") == (":py:obj:`~typing.Union`\\ " - "[:py:class:`int`," - " :py:class:`~numbers.Integral`]") + assert restify(Union[int]) == ":py:class:`int`" + assert restify(Union[int, str]) == ":py:class:`int` | :py:class:`str`" + assert restify(Optional[int]) == ":py:class:`int` | :py:obj:`None`" + + assert restify(Union[str, None]) == ":py:class:`str` | :py:obj:`None`" + assert restify(Union[None, str]) == ":py:obj:`None` | :py:class:`str`" + assert restify(Optional[str]) == ":py:class:`str` | :py:obj:`None`" + + assert restify(Union[int, str, None]) == ( + ":py:class:`int` | :py:class:`str` | :py:obj:`None`" + ) + assert restify(Optional[Union[int, str]]) in { + ":py:class:`str` | :py:class:`int` | :py:obj:`None`", + ":py:class:`int` | :py:class:`str` | :py:obj:`None`", + } + + assert restify(Union[int, Integral]) == ( + ":py:class:`int` | :py:class:`numbers.Integral`" + ) + assert restify(Union[int, Integral], "smart") == ( + ":py:class:`int` | :py:class:`~numbers.Integral`" + ) assert (restify(Union[MyClass1, MyClass2]) == - (":py:obj:`~typing.Union`\\ " - "[:py:class:`tests.test_util_typing.MyClass1`, " - ":py:class:`tests.test_util_typing.<MyClass2>`]")) + (":py:class:`tests.test_util.test_util_typing.MyClass1`" + " | :py:class:`tests.test_util.test_util_typing.<MyClass2>`")) assert (restify(Union[MyClass1, MyClass2], "smart") == - (":py:obj:`~typing.Union`\\ " - "[:py:class:`~tests.test_util_typing.MyClass1`," - " :py:class:`~tests.test_util_typing.<MyClass2>`]")) + (":py:class:`~tests.test_util.test_util_typing.MyClass1`" + " | :py:class:`~tests.test_util.test_util_typing.<MyClass2>`")) + + assert (restify(Optional[Union[MyClass1, MyClass2]]) == + (":py:class:`tests.test_util.test_util_typing.MyClass1`" + " | :py:class:`tests.test_util.test_util_typing.<MyClass2>`" + " | :py:obj:`None`")) + assert (restify(Optional[Union[MyClass1, MyClass2]], "smart") == + (":py:class:`~tests.test_util.test_util_typing.MyClass1`" + " | :py:class:`~tests.test_util.test_util_typing.<MyClass2>`" + " | :py:obj:`None`")) def test_restify_type_hints_typevars(): @@ -146,35 +235,35 @@ def test_restify_type_hints_typevars(): T_co = TypeVar('T_co', covariant=True) T_contra = TypeVar('T_contra', contravariant=True) - assert restify(T) == ":py:obj:`tests.test_util_typing.T`" - assert restify(T, "smart") == ":py:obj:`~tests.test_util_typing.T`" + assert restify(T) == ":py:obj:`tests.test_util.test_util_typing.T`" + assert restify(T, "smart") == ":py:obj:`~tests.test_util.test_util_typing.T`" - assert restify(T_co) == ":py:obj:`tests.test_util_typing.T_co`" - assert restify(T_co, "smart") == ":py:obj:`~tests.test_util_typing.T_co`" + assert restify(T_co) == ":py:obj:`tests.test_util.test_util_typing.T_co`" + assert restify(T_co, "smart") == ":py:obj:`~tests.test_util.test_util_typing.T_co`" - assert restify(T_contra) == ":py:obj:`tests.test_util_typing.T_contra`" - assert restify(T_contra, "smart") == ":py:obj:`~tests.test_util_typing.T_contra`" + assert restify(T_contra) == ":py:obj:`tests.test_util.test_util_typing.T_contra`" + assert restify(T_contra, "smart") == ":py:obj:`~tests.test_util.test_util_typing.T_contra`" - assert restify(List[T]) == ":py:class:`~typing.List`\\ [:py:obj:`tests.test_util_typing.T`]" - assert restify(List[T], "smart") == ":py:class:`~typing.List`\\ [:py:obj:`~tests.test_util_typing.T`]" + assert restify(List[T]) == ":py:class:`~typing.List`\\ [:py:obj:`tests.test_util.test_util_typing.T`]" + assert restify(List[T], "smart") == ":py:class:`~typing.List`\\ [:py:obj:`~tests.test_util.test_util_typing.T`]" - assert restify(list[T]) == ":py:class:`list`\\ [:py:obj:`tests.test_util_typing.T`]" - assert restify(list[T], "smart") == ":py:class:`list`\\ [:py:obj:`~tests.test_util_typing.T`]" + assert restify(list[T]) == ":py:class:`list`\\ [:py:obj:`tests.test_util.test_util_typing.T`]" + assert restify(list[T], "smart") == ":py:class:`list`\\ [:py:obj:`~tests.test_util.test_util_typing.T`]" if sys.version_info[:2] >= (3, 10): - assert restify(MyInt) == ":py:class:`tests.test_util_typing.MyInt`" - assert restify(MyInt, "smart") == ":py:class:`~tests.test_util_typing.MyInt`" + assert restify(MyInt) == ":py:class:`tests.test_util.test_util_typing.MyInt`" + assert restify(MyInt, "smart") == ":py:class:`~tests.test_util.test_util_typing.MyInt`" else: assert restify(MyInt) == ":py:class:`MyInt`" assert restify(MyInt, "smart") == ":py:class:`MyInt`" def test_restify_type_hints_custom_class(): - assert restify(MyClass1) == ":py:class:`tests.test_util_typing.MyClass1`" - assert restify(MyClass1, "smart") == ":py:class:`~tests.test_util_typing.MyClass1`" + assert restify(MyClass1) == ":py:class:`tests.test_util.test_util_typing.MyClass1`" + assert restify(MyClass1, "smart") == ":py:class:`~tests.test_util.test_util_typing.MyClass1`" - assert restify(MyClass2) == ":py:class:`tests.test_util_typing.<MyClass2>`" - assert restify(MyClass2, "smart") == ":py:class:`~tests.test_util_typing.<MyClass2>`" + assert restify(MyClass2) == ":py:class:`tests.test_util.test_util_typing.<MyClass2>`" + assert restify(MyClass2, "smart") == ":py:class:`~tests.test_util.test_util_typing.<MyClass2>`" def test_restify_type_hints_alias(): @@ -199,8 +288,8 @@ def test_restify_type_Literal(): from typing import Literal # type: ignore[attr-defined] assert restify(Literal[1, "2", "\r"]) == ":py:obj:`~typing.Literal`\\ [1, '2', '\\r']" - assert restify(Literal[MyEnum.a], 'fully-qualified-except-typing') == ':py:obj:`~typing.Literal`\\ [:py:attr:`tests.test_util_typing.MyEnum.a`]' - assert restify(Literal[MyEnum.a], 'smart') == ':py:obj:`~typing.Literal`\\ [:py:attr:`~tests.test_util_typing.MyEnum.a`]' + assert restify(Literal[MyEnum.a], 'fully-qualified-except-typing') == ':py:obj:`~typing.Literal`\\ [:py:attr:`tests.test_util.test_util_typing.MyEnum.a`]' + assert restify(Literal[MyEnum.a], 'smart') == ':py:obj:`~typing.Literal`\\ [:py:attr:`~tests.test_util.test_util_typing.MyEnum.a`]' def test_restify_pep_585(): @@ -223,7 +312,7 @@ def test_restify_pep_585(): "[:py:class:`str`, :py:class:`~typing.Tuple`\\ " "[:py:class:`str`, ...]]]") assert restify(tuple[MyList[list[int]], int]) == (":py:class:`tuple`\\ [" - ":py:class:`tests.test_util_typing.MyList`\\ " + ":py:class:`tests.test_util.test_util_typing.MyList`\\ " "[:py:class:`list`\\ [:py:class:`int`]], " ":py:class:`int`]") @@ -231,14 +320,15 @@ def test_restify_pep_585(): @pytest.mark.skipif(sys.version_info[:2] <= (3, 9), reason='python 3.10+ is required.') def test_restify_type_union_operator(): assert restify(int | None) == ":py:class:`int` | :py:obj:`None`" # type: ignore[attr-defined] + assert restify(None | int) == ":py:obj:`None` | :py:class:`int`" # type: ignore[attr-defined] assert restify(int | str) == ":py:class:`int` | :py:class:`str`" # type: ignore[attr-defined] assert restify(int | str | None) == (":py:class:`int` | :py:class:`str` | " # type: ignore[attr-defined] ":py:obj:`None`") def test_restify_broken_type_hints(): - assert restify(BrokenType) == ':py:class:`tests.test_util_typing.BrokenType`' - assert restify(BrokenType, "smart") == ':py:class:`~tests.test_util_typing.BrokenType`' + assert restify(BrokenType) == ':py:class:`tests.test_util.test_util_typing.BrokenType`' + assert restify(BrokenType, "smart") == ':py:class:`~tests.test_util.test_util_typing.BrokenType`' def test_restify_mock(): @@ -315,14 +405,18 @@ def test_stringify_type_hints_containers(): assert stringify_annotation(List[Dict[str, Tuple]], "fully-qualified") == "typing.List[typing.Dict[str, typing.Tuple]]" assert stringify_annotation(List[Dict[str, Tuple]], "smart") == "~typing.List[~typing.Dict[str, ~typing.Tuple]]" - assert stringify_annotation(MyList[Tuple[int, int]], 'fully-qualified-except-typing') == "tests.test_util_typing.MyList[Tuple[int, int]]" - assert stringify_annotation(MyList[Tuple[int, int]], "fully-qualified") == "tests.test_util_typing.MyList[typing.Tuple[int, int]]" - assert stringify_annotation(MyList[Tuple[int, int]], "smart") == "~tests.test_util_typing.MyList[~typing.Tuple[int, int]]" + assert stringify_annotation(MyList[Tuple[int, int]], 'fully-qualified-except-typing') == "tests.test_util.test_util_typing.MyList[Tuple[int, int]]" + assert stringify_annotation(MyList[Tuple[int, int]], "fully-qualified") == "tests.test_util.test_util_typing.MyList[typing.Tuple[int, int]]" + assert stringify_annotation(MyList[Tuple[int, int]], "smart") == "~tests.test_util.test_util_typing.MyList[~typing.Tuple[int, int]]" assert stringify_annotation(Generator[None, None, None], 'fully-qualified-except-typing') == "Generator[None, None, None]" assert stringify_annotation(Generator[None, None, None], "fully-qualified") == "typing.Generator[None, None, None]" assert stringify_annotation(Generator[None, None, None], "smart") == "~typing.Generator[None, None, None]" + assert stringify_annotation(Iterator[None], 'fully-qualified-except-typing') == "Iterator[None]" + assert stringify_annotation(Iterator[None], "fully-qualified") == "typing.Iterator[None]" + assert stringify_annotation(Iterator[None], "smart") == "~typing.Iterator[None]" + def test_stringify_type_hints_pep_585(): assert stringify_annotation(list[int], 'fully-qualified-except-typing') == "list[int]" @@ -346,9 +440,9 @@ def test_stringify_type_hints_pep_585(): assert stringify_annotation(list[dict[str, tuple]], 'fully-qualified-except-typing') == "list[dict[str, tuple]]" assert stringify_annotation(list[dict[str, tuple]], "smart") == "list[dict[str, tuple]]" - assert stringify_annotation(MyList[tuple[int, int]], 'fully-qualified-except-typing') == "tests.test_util_typing.MyList[tuple[int, int]]" - assert stringify_annotation(MyList[tuple[int, int]], "fully-qualified") == "tests.test_util_typing.MyList[tuple[int, int]]" - assert stringify_annotation(MyList[tuple[int, int]], "smart") == "~tests.test_util_typing.MyList[tuple[int, int]]" + assert stringify_annotation(MyList[tuple[int, int]], 'fully-qualified-except-typing') == "tests.test_util.test_util_typing.MyList[tuple[int, int]]" + assert stringify_annotation(MyList[tuple[int, int]], "fully-qualified") == "tests.test_util.test_util_typing.MyList[tuple[int, int]]" + assert stringify_annotation(MyList[tuple[int, int]], "smart") == "~tests.test_util.test_util_typing.MyList[tuple[int, int]]" assert stringify_annotation(type[int], 'fully-qualified-except-typing') == "type[int]" assert stringify_annotation(type[int], "smart") == "type[int]" @@ -413,9 +507,12 @@ def test_stringify_type_hints_Union(): assert stringify_annotation(Optional[int], "fully-qualified") == "int | None" assert stringify_annotation(Optional[int], "smart") == "int | None" - assert stringify_annotation(Union[str, None], 'fully-qualified-except-typing') == "str | None" - assert stringify_annotation(Union[str, None], "fully-qualified") == "str | None" - assert stringify_annotation(Union[str, None], "smart") == "str | None" + assert stringify_annotation(Union[int, None], 'fully-qualified-except-typing') == "int | None" + assert stringify_annotation(Union[None, int], 'fully-qualified-except-typing') == "None | int" + assert stringify_annotation(Union[int, None], "fully-qualified") == "int | None" + assert stringify_annotation(Union[None, int], "fully-qualified") == "None | int" + assert stringify_annotation(Union[int, None], "smart") == "int | None" + assert stringify_annotation(Union[None, int], "smart") == "None | int" assert stringify_annotation(Union[int, str], 'fully-qualified-except-typing') == "int | str" assert stringify_annotation(Union[int, str], "fully-qualified") == "int | str" @@ -426,11 +523,11 @@ def test_stringify_type_hints_Union(): assert stringify_annotation(Union[int, Integral], "smart") == "int | ~numbers.Integral" assert (stringify_annotation(Union[MyClass1, MyClass2], 'fully-qualified-except-typing') == - "tests.test_util_typing.MyClass1 | tests.test_util_typing.<MyClass2>") + "tests.test_util.test_util_typing.MyClass1 | tests.test_util.test_util_typing.<MyClass2>") assert (stringify_annotation(Union[MyClass1, MyClass2], "fully-qualified") == - "tests.test_util_typing.MyClass1 | tests.test_util_typing.<MyClass2>") + "tests.test_util.test_util_typing.MyClass1 | tests.test_util.test_util_typing.<MyClass2>") assert (stringify_annotation(Union[MyClass1, MyClass2], "smart") == - "~tests.test_util_typing.MyClass1 | ~tests.test_util_typing.<MyClass2>") + "~tests.test_util.test_util_typing.MyClass1 | ~tests.test_util.test_util_typing.<MyClass2>") def test_stringify_type_hints_typevars(): @@ -438,35 +535,35 @@ def test_stringify_type_hints_typevars(): T_co = TypeVar('T_co', covariant=True) T_contra = TypeVar('T_contra', contravariant=True) - assert stringify_annotation(T, 'fully-qualified-except-typing') == "tests.test_util_typing.T" - assert stringify_annotation(T, "smart") == "~tests.test_util_typing.T" + assert stringify_annotation(T, 'fully-qualified-except-typing') == "tests.test_util.test_util_typing.T" + assert stringify_annotation(T, "smart") == "~tests.test_util.test_util_typing.T" - assert stringify_annotation(T_co, 'fully-qualified-except-typing') == "tests.test_util_typing.T_co" - assert stringify_annotation(T_co, "smart") == "~tests.test_util_typing.T_co" + assert stringify_annotation(T_co, 'fully-qualified-except-typing') == "tests.test_util.test_util_typing.T_co" + assert stringify_annotation(T_co, "smart") == "~tests.test_util.test_util_typing.T_co" - assert stringify_annotation(T_contra, 'fully-qualified-except-typing') == "tests.test_util_typing.T_contra" - assert stringify_annotation(T_contra, "smart") == "~tests.test_util_typing.T_contra" + assert stringify_annotation(T_contra, 'fully-qualified-except-typing') == "tests.test_util.test_util_typing.T_contra" + assert stringify_annotation(T_contra, "smart") == "~tests.test_util.test_util_typing.T_contra" - assert stringify_annotation(List[T], 'fully-qualified-except-typing') == "List[tests.test_util_typing.T]" - assert stringify_annotation(List[T], "smart") == "~typing.List[~tests.test_util_typing.T]" + assert stringify_annotation(List[T], 'fully-qualified-except-typing') == "List[tests.test_util.test_util_typing.T]" + assert stringify_annotation(List[T], "smart") == "~typing.List[~tests.test_util.test_util_typing.T]" - assert stringify_annotation(list[T], 'fully-qualified-except-typing') == "list[tests.test_util_typing.T]" - assert stringify_annotation(list[T], "smart") == "list[~tests.test_util_typing.T]" + assert stringify_annotation(list[T], 'fully-qualified-except-typing') == "list[tests.test_util.test_util_typing.T]" + assert stringify_annotation(list[T], "smart") == "list[~tests.test_util.test_util_typing.T]" if sys.version_info[:2] >= (3, 10): - assert stringify_annotation(MyInt, 'fully-qualified-except-typing') == "tests.test_util_typing.MyInt" - assert stringify_annotation(MyInt, "smart") == "~tests.test_util_typing.MyInt" + assert stringify_annotation(MyInt, 'fully-qualified-except-typing') == "tests.test_util.test_util_typing.MyInt" + assert stringify_annotation(MyInt, "smart") == "~tests.test_util.test_util_typing.MyInt" else: assert stringify_annotation(MyInt, 'fully-qualified-except-typing') == "MyInt" assert stringify_annotation(MyInt, "smart") == "MyInt" def test_stringify_type_hints_custom_class(): - assert stringify_annotation(MyClass1, 'fully-qualified-except-typing') == "tests.test_util_typing.MyClass1" - assert stringify_annotation(MyClass1, "smart") == "~tests.test_util_typing.MyClass1" + assert stringify_annotation(MyClass1, 'fully-qualified-except-typing') == "tests.test_util.test_util_typing.MyClass1" + assert stringify_annotation(MyClass1, "smart") == "~tests.test_util.test_util_typing.MyClass1" - assert stringify_annotation(MyClass2, 'fully-qualified-except-typing') == "tests.test_util_typing.<MyClass2>" - assert stringify_annotation(MyClass2, "smart") == "~tests.test_util_typing.<MyClass2>" + assert stringify_annotation(MyClass2, 'fully-qualified-except-typing') == "tests.test_util.test_util_typing.<MyClass2>" + assert stringify_annotation(MyClass2, "smart") == "~tests.test_util.test_util_typing.<MyClass2>" def test_stringify_type_hints_alias(): @@ -486,8 +583,8 @@ def test_stringify_type_Literal(): assert stringify_annotation(Literal[1, "2", "\r"], "fully-qualified") == "typing.Literal[1, '2', '\\r']" assert stringify_annotation(Literal[1, "2", "\r"], "smart") == "~typing.Literal[1, '2', '\\r']" - assert stringify_annotation(Literal[MyEnum.a], 'fully-qualified-except-typing') == 'Literal[tests.test_util_typing.MyEnum.a]' - assert stringify_annotation(Literal[MyEnum.a], 'fully-qualified') == 'typing.Literal[tests.test_util_typing.MyEnum.a]' + assert stringify_annotation(Literal[MyEnum.a], 'fully-qualified-except-typing') == 'Literal[tests.test_util.test_util_typing.MyEnum.a]' + assert stringify_annotation(Literal[MyEnum.a], 'fully-qualified') == 'typing.Literal[tests.test_util.test_util_typing.MyEnum.a]' assert stringify_annotation(Literal[MyEnum.a], 'smart') == '~typing.Literal[MyEnum.a]' @@ -510,8 +607,8 @@ def test_stringify_type_union_operator(): def test_stringify_broken_type_hints(): - assert stringify_annotation(BrokenType, 'fully-qualified-except-typing') == 'tests.test_util_typing.BrokenType' - assert stringify_annotation(BrokenType, "smart") == '~tests.test_util_typing.BrokenType' + assert stringify_annotation(BrokenType, 'fully-qualified-except-typing') == 'tests.test_util.test_util_typing.BrokenType' + assert stringify_annotation(BrokenType, "smart") == '~tests.test_util.test_util_typing.BrokenType' def test_stringify_mock(): diff --git a/tests/typing_test_data.py b/tests/test_util/typing_test_data.py index 8a7ebc4..e29b600 100644 --- a/tests/typing_test_data.py +++ b/tests/test_util/typing_test_data.py @@ -39,7 +39,7 @@ def f6(x: int, *args, y: str, z: str) -> None: pass -def f7(x: int = None, y: dict = {}) -> None: # NoQA: B006 +def f7(x: int = None, y: dict = {}) -> None: # NoQA: B006,RUF013 pass @@ -77,7 +77,7 @@ def f14() -> Any: pass -def f15(x: "Unknown", y: "int") -> Any: # noqa: F821 # type: ignore[attr-defined] +def f15(x: "Unknown", y: "int") -> Any: # NoQA: F821 # type: ignore[attr-defined] pass |