| 1 |
"""Tests util.typing functions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import ctypes |
| 6 |
import dataclasses |
| 7 |
import sys |
| 8 |
import typing as t |
| 9 |
import zipfile |
| 10 |
from collections import abc |
| 11 |
from contextvars import Context, ContextVar, Token |
| 12 |
from enum import Enum |
| 13 |
from io import ( |
| 14 |
BufferedRandom, |
| 15 |
BufferedReader, |
| 16 |
BufferedRWPair, |
| 17 |
BufferedWriter, |
| 18 |
BytesIO, |
| 19 |
FileIO, |
| 20 |
StringIO, |
| 21 |
TextIOWrapper, |
| 22 |
) |
| 23 |
from json import JSONDecoder, JSONEncoder |
| 24 |
from lzma import LZMACompressor, LZMADecompressor |
| 25 |
from multiprocessing import Process |
| 26 |
from numbers import Integral |
| 27 |
from pathlib import ( |
| 28 |
Path, |
| 29 |
PosixPath, |
| 30 |
PurePath, |
| 31 |
PurePosixPath, |
| 32 |
PureWindowsPath, |
| 33 |
WindowsPath, |
| 34 |
) |
| 35 |
from pickle import Pickler, Unpickler |
| 36 |
from struct import Struct |
| 37 |
from types import ( |
| 38 |
AsyncGeneratorType, |
| 39 |
BuiltinFunctionType, |
| 40 |
BuiltinMethodType, |
| 41 |
CellType, |
| 42 |
ClassMethodDescriptorType, |
| 43 |
CodeType, |
| 44 |
CoroutineType, |
| 45 |
EllipsisType, |
| 46 |
FrameType, |
| 47 |
FunctionType, |
| 48 |
GeneratorType, |
| 49 |
GetSetDescriptorType, |
| 50 |
LambdaType, |
| 51 |
MappingProxyType, |
| 52 |
MemberDescriptorType, |
| 53 |
MethodDescriptorType, |
| 54 |
MethodType, |
| 55 |
MethodWrapperType, |
| 56 |
ModuleType, |
| 57 |
NoneType, |
| 58 |
NotImplementedType, |
| 59 |
TracebackType, |
| 60 |
WrapperDescriptorType, |
| 61 |
) |
| 62 |
from typing import ( |
| 63 |
Annotated, |
| 64 |
Any, |
| 65 |
Dict, |
| 66 |
ForwardRef, |
| 67 |
List, |
| 68 |
Literal, |
| 69 |
NewType, |
| 70 |
Optional, |
| 71 |
ParamSpec, |
| 72 |
Tuple, |
| 73 |
TypeVar, |
| 74 |
Union, |
| 75 |
) |
| 76 |
from weakref import WeakSet |
| 77 |
|
| 78 |
from sphinx.ext.autodoc._dynamic._mock import mock |
| 79 |
from sphinx.util.typing import _INVALID_BUILTIN_CLASSES, restify, stringify_annotation |
| 80 |
|
| 81 |
|
| 82 |
class MyClass1: |
| 83 |
pass |
| 84 |
|
| 85 |
|
| 86 |
class MyClass2(MyClass1): |
| 87 |
__qualname__ = '<MyClass2>' |
| 88 |
|
| 89 |
|
| 90 |
class MyEnum(Enum): |
| 91 |
a = 1 |
| 92 |
|
| 93 |
|
| 94 |
T = TypeVar('T') |
| 95 |
MyInt = NewType('MyInt', int) |
| 96 |
|
| 97 |
|
| 98 |
class MyList(List[T]): |
| 99 |
pass |
| 100 |
|
| 101 |
|
| 102 |
class BrokenType: |
| 103 |
__args__ = int |
| 104 |
|
| 105 |
|
| 106 |
@dataclasses.dataclass(frozen=True) |
| 107 |
class Gt: |
| 108 |
gt: float |
| 109 |
|
| 110 |
|
| 111 |
def test_restify() -> None: |
| 112 |
assert restify(int) == ':py:class:`int`' |
| 113 |
assert restify(int, 'smart') == ':py:class:`int`' |
| 114 |
|
| 115 |
assert restify(str) == ':py:class:`str`' |
| 116 |
assert restify(str, 'smart') == ':py:class:`str`' |
| 117 |
|
| 118 |
assert restify(None) == ':py:obj:`None`' |
| 119 |
assert restify(None, 'smart') == ':py:obj:`None`' |
| 120 |
|
| 121 |
assert restify(Integral) == ':py:class:`numbers.Integral`' |
| 122 |
assert restify(Integral, 'smart') == ':py:class:`~numbers.Integral`' |
| 123 |
|
| 124 |
assert restify(Struct) == ':py:class:`struct.Struct`' |
| 125 |
assert restify(Struct, 'smart') == ':py:class:`~struct.Struct`' |
| 126 |
|
| 127 |
assert restify(TracebackType) == ':py:class:`types.TracebackType`' |
| 128 |
assert restify(TracebackType, 'smart') == ':py:class:`~types.TracebackType`' |
| 129 |
|
| 130 |
assert restify(Path) == ':py:class:`pathlib.Path`' |
| 131 |
assert restify(Path, 'smart') == ':py:class:`~pathlib.Path`' |
| 132 |
|
| 133 |
assert restify(Any) == ':py:obj:`~typing.Any`' |
| 134 |
assert restify(Any, 'smart') == ':py:obj:`~typing.Any`' |
| 135 |
|
| 136 |
assert restify('str') == 'str' |
| 137 |
assert restify('str', 'smart') == 'str' |
| 138 |
|
| 139 |
|
| 140 |
def test_is_invalid_builtin_class() -> None: |
| 141 |
# if these tests start failing, it means that the __module__ |
| 142 |
# of one of these classes has changed, and _INVALID_BUILTIN_CLASSES |
| 143 |
# in sphinx.util.typing needs to be updated. |
| 144 |
invalid_types = ( |
| 145 |
# contextvars |
| 146 |
Context, |
| 147 |
ContextVar, |
| 148 |
Token, |
| 149 |
# ctypes |
| 150 |
ctypes.Array, |
| 151 |
ctypes.Structure, |
| 152 |
ctypes.Union, |
| 153 |
# io |
| 154 |
FileIO, |
| 155 |
BytesIO, |
| 156 |
StringIO, |
| 157 |
BufferedReader, |
| 158 |
BufferedWriter, |
| 159 |
BufferedRWPair, |
| 160 |
BufferedRandom, |
| 161 |
TextIOWrapper, |
| 162 |
# json |
| 163 |
JSONDecoder, |
| 164 |
JSONEncoder, |
| 165 |
# lzma |
| 166 |
LZMACompressor, |
| 167 |
LZMADecompressor, |
| 168 |
# multiprocessing |
| 169 |
Process, |
| 170 |
# pickle |
| 171 |
Pickler, |
| 172 |
Unpickler, |
| 173 |
# struct |
| 174 |
Struct, |
| 175 |
# types |
| 176 |
AsyncGeneratorType, |
| 177 |
BuiltinFunctionType, |
| 178 |
BuiltinMethodType, |
| 179 |
CellType, |
| 180 |
ClassMethodDescriptorType, |
| 181 |
CodeType, |
| 182 |
CoroutineType, |
| 183 |
EllipsisType, |
| 184 |
FrameType, |
| 185 |
FunctionType, |
| 186 |
GeneratorType, |
| 187 |
GetSetDescriptorType, |
| 188 |
LambdaType, |
| 189 |
MappingProxyType, |
| 190 |
MemberDescriptorType, |
| 191 |
MethodDescriptorType, |
| 192 |
MethodType, |
| 193 |
MethodWrapperType, |
| 194 |
ModuleType, |
| 195 |
NoneType, |
| 196 |
NotImplementedType, |
| 197 |
TracebackType, |
| 198 |
WrapperDescriptorType, |
| 199 |
# weakref |
| 200 |
WeakSet, |
| 201 |
# zipfile |
| 202 |
zipfile.Path, |
| 203 |
zipfile.CompleteDirs, |
| 204 |
) |
| 205 |
if sys.version_info[:2] == (3, 13): |
| 206 |
invalid_types += ( |
| 207 |
# pathlib |
| 208 |
Path, |
| 209 |
PosixPath, |
| 210 |
PurePath, |
| 211 |
PurePosixPath, |
| 212 |
PureWindowsPath, |
| 213 |
WindowsPath, |
| 214 |
) |
| 215 |
|
| 216 |
invalid_names = {(cls.__module__, cls.__qualname__) for cls in invalid_types} |
| 217 |
if sys.version_info[:2] != (3, 13): |
| 218 |
invalid_names |= { |
| 219 |
('pathlib._local', 'Path'), |
| 220 |
('pathlib._local', 'PosixPath'), |
| 221 |
('pathlib._local', 'PurePath'), |
| 222 |
('pathlib._local', 'PurePosixPath'), |
| 223 |
('pathlib._local', 'PureWindowsPath'), |
| 224 |
('pathlib._local', 'WindowsPath'), |
| 225 |
} |
| 226 |
assert set(_INVALID_BUILTIN_CLASSES) == invalid_names |
| 227 |
|
| 228 |
|
| 229 |
def test_restify_type_hints_containers(): |
| 230 |
assert restify(List) == ':py:class:`~typing.List`' |
| 231 |
assert restify(Dict) == ':py:class:`~typing.Dict`' |
| 232 |
assert restify(List[int]) == ':py:class:`~typing.List`\\ [:py:class:`int`]' |
| 233 |
assert restify(List[str]) == ':py:class:`~typing.List`\\ [:py:class:`str`]' |
| 234 |
assert restify(Dict[str, float]) == ( |
| 235 |
':py:class:`~typing.Dict`\\ [:py:class:`str`, :py:class:`float`]' |
| 236 |
) |
| 237 |
assert restify(Tuple[str, str, str]) == ( |
| 238 |
':py:class:`~typing.Tuple`\\ ' |
| 239 |
'[:py:class:`str`, :py:class:`str`, ' |
| 240 |
':py:class:`str`]' |
| 241 |
) |
| 242 |
ann_rst = restify(Tuple[str, ...]) |
| 243 |
assert ann_rst == ':py:class:`~typing.Tuple`\\ [:py:class:`str`, ...]' |
| 244 |
|
| 245 |
assert restify(Tuple[()]) == ':py:class:`~typing.Tuple`' |
| 246 |
|
| 247 |
assert restify(List[Dict[str, Tuple]]) == ( |
| 248 |
':py:class:`~typing.List`\\ ' |
| 249 |
'[:py:class:`~typing.Dict`\\ ' |
| 250 |
'[:py:class:`str`, :py:class:`~typing.Tuple`]]' |
| 251 |
) |
| 252 |
assert restify(MyList[Tuple[int, int]]) == ( |
| 253 |
':py:class:`tests.test_util.test_util_typing.MyList`\\ ' |
| 254 |
'[:py:class:`~typing.Tuple`\\ ' |
| 255 |
'[:py:class:`int`, :py:class:`int`]]' |
| 256 |
) |
| 257 |
assert restify(t.Generator[None, None, None]) == ( |
| 258 |
':py:class:`~typing.Generator`\\ ' |
| 259 |
'[:py:obj:`None`, :py:obj:`None`, ' |
| 260 |
':py:obj:`None`]' |
| 261 |
) |
| 262 |
assert restify(abc.Generator[None, None, None]) == ( |
| 263 |
':py:class:`collections.abc.Generator`\\ ' |
| 264 |
'[:py:obj:`None`, :py:obj:`None`, ' |
| 265 |
':py:obj:`None`]' |
| 266 |
) |
| 267 |
assert restify(t.Iterator[None]) == ( |
| 268 |
':py:class:`~typing.Iterator`\\ [:py:obj:`None`]' |
| 269 |
) |
| 270 |
assert restify(abc.Iterator[None]) == ( |
| 271 |
':py:class:`collections.abc.Iterator`\\ [:py:obj:`None`]' |
| 272 |
) |
| 273 |
|
| 274 |
|
| 275 |
def test_restify_Annotated() -> None: |
| 276 |
ann_rst = restify(Annotated[str, 'foo', 'bar']) |
| 277 |
assert ann_rst == ( |
| 278 |
":py:class:`~typing.Annotated`\\ [:py:class:`str`, 'foo', 'bar']" |
| 279 |
) |
| 280 |
ann_rst = restify(Annotated[str, 'foo', 'bar'], 'smart') |
| 281 |
assert ann_rst == ( |
| 282 |
":py:class:`~typing.Annotated`\\ [:py:class:`str`, 'foo', 'bar']" |
| 283 |
) |
| 284 |
ann_rst = restify(Annotated[float, Gt(-10.0)]) |
| 285 |
assert ann_rst == ( |
| 286 |
':py:class:`~typing.Annotated`\\ [:py:class:`float`, :py:class:`tests.test_util.test_util_typing.Gt`\\ (gt=\\ -10.0)]' |
| 287 |
) |
| 288 |
ann_rst = restify(Annotated[float, Gt(-10.0)], 'smart') |
| 289 |
assert ann_rst == ( |
| 290 |
':py:class:`~typing.Annotated`\\ [:py:class:`float`, :py:class:`~tests.test_util.test_util_typing.Gt`\\ (gt=\\ -10.0)]' |
| 291 |
) |
| 292 |
|
| 293 |
|
| 294 |
def test_restify_type_hints_Callable() -> None: |
| 295 |
assert restify(t.Callable) == ':py:class:`~typing.Callable`' |
| 296 |
assert restify(t.Callable[[str], int]) == ( |
| 297 |
':py:class:`~typing.Callable`\\ [[:py:class:`str`], :py:class:`int`]' |
| 298 |
) |
| 299 |
assert restify(t.Callable[..., int]) == ( |
| 300 |
':py:class:`~typing.Callable`\\ [[...], :py:class:`int`]' |
| 301 |
) |
| 302 |
assert restify(abc.Callable) == ':py:class:`collections.abc.Callable`' |
| 303 |
assert restify(abc.Callable[[str], int]) == ( |
| 304 |
':py:class:`collections.abc.Callable`\\ [[:py:class:`str`], :py:class:`int`]' |
| 305 |
) |
| 306 |
assert restify(abc.Callable[..., int]) == ( |
| 307 |
':py:class:`collections.abc.Callable`\\ [[...], :py:class:`int`]' |
| 308 |
) |
| 309 |
|
| 310 |
|
| 311 |
def test_restify_type_hints_Union() -> None: |
| 312 |
assert restify(Union[int]) == ':py:class:`int`' |
| 313 |
assert restify(Union[int, str]) == ':py:class:`int` | :py:class:`str`' |
| 314 |
assert restify(Optional[int]) == ':py:class:`int` | :py:obj:`None`' |
| 315 |
|
| 316 |
assert restify(Union[str, None]) == ':py:class:`str` | :py:obj:`None`' |
| 317 |
assert restify(Union[None, str]) == ':py:obj:`None` | :py:class:`str`' |
| 318 |
assert restify(Optional[str]) == ':py:class:`str` | :py:obj:`None`' |
| 319 |
|
| 320 |
assert restify(Union[int, str, None]) == ( |
| 321 |
':py:class:`int` | :py:class:`str` | :py:obj:`None`' |
| 322 |
) |
| 323 |
assert restify(Optional[Union[int, str]]) in { |
| 324 |
':py:class:`str` | :py:class:`int` | :py:obj:`None`', |
| 325 |
':py:class:`int` | :py:class:`str` | :py:obj:`None`', |
| 326 |
} |
| 327 |
|
| 328 |
assert restify(Union[int, Integral]) == ( |
| 329 |
':py:class:`int` | :py:class:`numbers.Integral`' |
| 330 |
) |
| 331 |
assert restify(Union[int, Integral], 'smart') == ( |
| 332 |
':py:class:`int` | :py:class:`~numbers.Integral`' |
| 333 |
) |
| 334 |
|
| 335 |
assert restify(Union[MyClass1, MyClass2]) == ( |
| 336 |
':py:class:`tests.test_util.test_util_typing.MyClass1`' |
| 337 |
' | :py:class:`tests.test_util.test_util_typing.<MyClass2>`' |
| 338 |
) |
| 339 |
assert restify(Union[MyClass1, MyClass2], 'smart') == ( |
| 340 |
':py:class:`~tests.test_util.test_util_typing.MyClass1`' |
| 341 |
' | :py:class:`~tests.test_util.test_util_typing.<MyClass2>`' |
| 342 |
) |
| 343 |
|
| 344 |
assert restify(Optional[Union[MyClass1, MyClass2]]) == ( |
| 345 |
':py:class:`tests.test_util.test_util_typing.MyClass1`' |
| 346 |
' | :py:class:`tests.test_util.test_util_typing.<MyClass2>`' |
| 347 |
' | :py:obj:`None`' |
| 348 |
) |
| 349 |
assert restify(Optional[Union[MyClass1, MyClass2]], 'smart') == ( |
| 350 |
':py:class:`~tests.test_util.test_util_typing.MyClass1`' |
| 351 |
' | :py:class:`~tests.test_util.test_util_typing.<MyClass2>`' |
| 352 |
' | :py:obj:`None`' |
| 353 |
) |
| 354 |
|
| 355 |
|
| 356 |
def test_restify_type_hints_typevars(): |
| 357 |
T = TypeVar('T') |
| 358 |
T_co = TypeVar('T_co', covariant=True) |
| 359 |
T_contra = TypeVar('T_contra', contravariant=True) |
| 360 |
|
| 361 |
assert restify(T) == ':py:obj:`tests.test_util.test_util_typing.T`' |
| 362 |
assert restify(T, 'smart') == ':py:obj:`~tests.test_util.test_util_typing.T`' |
| 363 |
|
| 364 |
assert restify(T_co) == ':py:obj:`tests.test_util.test_util_typing.T_co`' |
| 365 |
assert restify(T_co, 'smart') == ':py:obj:`~tests.test_util.test_util_typing.T_co`' |
| 366 |
|
| 367 |
assert restify(T_contra) == ':py:obj:`tests.test_util.test_util_typing.T_contra`' |
| 368 |
ann_rst = restify(T_contra, 'smart') |
| 369 |
assert ann_rst == ':py:obj:`~tests.test_util.test_util_typing.T_contra`' |
| 370 |
|
| 371 |
ann_rst = restify(List[T]) |
| 372 |
assert ann_rst == ( |
| 373 |
':py:class:`~typing.List`\\ [:py:obj:`tests.test_util.test_util_typing.T`]' |
| 374 |
) |
| 375 |
ann_rst = restify(List[T], 'smart') |
| 376 |
assert ann_rst == ( |
| 377 |
':py:class:`~typing.List`\\ [:py:obj:`~tests.test_util.test_util_typing.T`]' |
| 378 |
) |
| 379 |
|
| 380 |
ann_rst = restify(list[T]) |
| 381 |
assert ann_rst == ( |
| 382 |
':py:class:`list`\\ [:py:obj:`tests.test_util.test_util_typing.T`]' |
| 383 |
) |
| 384 |
ann_rst = restify(list[T], 'smart') |
| 385 |
assert ann_rst == ( |
| 386 |
':py:class:`list`\\ [:py:obj:`~tests.test_util.test_util_typing.T`]' |
| 387 |
) |
| 388 |
|
| 389 |
assert restify(MyInt) == ':py:class:`tests.test_util.test_util_typing.MyInt`' |
| 390 |
ann_rst = restify(MyInt, 'smart') |
| 391 |
assert ann_rst == ':py:class:`~tests.test_util.test_util_typing.MyInt`' |
| 392 |
|
| 393 |
|
| 394 |
def test_restify_type_hints_custom_class() -> None: |
| 395 |
assert restify(MyClass1) == ':py:class:`tests.test_util.test_util_typing.MyClass1`' |
| 396 |
ann_rst = restify(MyClass1, 'smart') |
| 397 |
assert ann_rst == ':py:class:`~tests.test_util.test_util_typing.MyClass1`' |
| 398 |
|
| 399 |
ann_rst = restify(MyClass2) |
| 400 |
assert ann_rst == ':py:class:`tests.test_util.test_util_typing.<MyClass2>`' |
| 401 |
ann_rst = restify(MyClass2, 'smart') |
| 402 |
assert ann_rst == ':py:class:`~tests.test_util.test_util_typing.<MyClass2>`' |
| 403 |
|
| 404 |
|
| 405 |
def test_restify_type_hints_alias() -> None: |
| 406 |
MyStr = str |
| 407 |
MyTypingTuple = Tuple[str, str] |
| 408 |
MyTuple = tuple[str, str] |
| 409 |
assert restify(MyStr) == ':py:class:`str`' |
| 410 |
ann_rst = restify(MyTypingTuple) |
| 411 |
assert ann_rst == ':py:class:`~typing.Tuple`\\ [:py:class:`str`, :py:class:`str`]' |
| 412 |
assert restify(MyTuple) == ':py:class:`tuple`\\ [:py:class:`str`, :py:class:`str`]' |
| 413 |
|
| 414 |
|
| 415 |
def test_restify_type_ForwardRef(): |
| 416 |
assert restify(ForwardRef('MyInt')) == ':py:class:`MyInt`' |
| 417 |
|
| 418 |
assert ( |
| 419 |
restify(list[ForwardRef('MyInt')]) == ':py:class:`list`\\ [:py:class:`MyInt`]' # ty: ignore[invalid-type-form] |
| 420 |
) |
| 421 |
|
| 422 |
ann_rst = restify(Tuple[dict[ForwardRef('MyInt'), str], list[List[int]]]) # ty: ignore[invalid-type-form] |
| 423 |
assert ann_rst == ( |
| 424 |
':py:class:`~typing.Tuple`\\ [:py:class:`dict`\\ [:py:class:`MyInt`, :py:class:`str`], :py:class:`list`\\ [:py:class:`~typing.List`\\ [:py:class:`int`]]]' |
| 425 |
) |
| 426 |
|
| 427 |
|
| 428 |
def test_restify_type_Literal() -> None: |
| 429 |
ann_rst = restify(Literal[1, '2', '\r']) |
| 430 |
assert ann_rst == ":py:obj:`~typing.Literal`\\ [1, '2', '\\r']" |
| 431 |
|
| 432 |
ann_rst = restify(Literal[MyEnum.a], 'fully-qualified-except-typing') |
| 433 |
assert ann_rst == ( |
| 434 |
':py:obj:`~typing.Literal`\\ [:py:attr:`tests.test_util.test_util_typing.MyEnum.a`]' |
| 435 |
) |
| 436 |
ann_rst = restify(Literal[MyEnum.a], 'smart') |
| 437 |
assert ann_rst == ( |
| 438 |
':py:obj:`~typing.Literal`\\ [:py:attr:`~tests.test_util.test_util_typing.MyEnum.a`]' |
| 439 |
) |
| 440 |
|
| 441 |
|
| 442 |
def test_restify_pep_585() -> None: |
| 443 |
assert restify(list[str]) == ':py:class:`list`\\ [:py:class:`str`]' |
| 444 |
ann_rst = restify(dict[str, str]) |
| 445 |
assert ann_rst == ':py:class:`dict`\\ [:py:class:`str`, :py:class:`str`]' |
| 446 |
assert restify(tuple[str, ...]) == ':py:class:`tuple`\\ [:py:class:`str`, ...]' |
| 447 |
assert restify(tuple[str, str, str]) == ( |
| 448 |
':py:class:`tuple`\\ [:py:class:`str`, :py:class:`str`, :py:class:`str`]' |
| 449 |
) |
| 450 |
ann_rst = restify(dict[str, tuple[int, ...]]) |
| 451 |
assert ann_rst == ( |
| 452 |
':py:class:`dict`\\ ' |
| 453 |
'[:py:class:`str`, :py:class:`tuple`\\ ' |
| 454 |
'[:py:class:`int`, ...]]' |
| 455 |
) |
| 456 |
|
| 457 |
assert restify(tuple[()]) == ':py:class:`tuple`\\ [()]' |
| 458 |
|
| 459 |
# Mix old typing with PEP 585 |
| 460 |
assert restify(List[dict[str, Tuple[str, ...]]]) == ( |
| 461 |
':py:class:`~typing.List`\\ ' |
| 462 |
'[:py:class:`dict`\\ ' |
| 463 |
'[:py:class:`str`, :py:class:`~typing.Tuple`\\ ' |
| 464 |
'[:py:class:`str`, ...]]]' |
| 465 |
) |
| 466 |
assert restify(tuple[MyList[list[int]], int]) == ( |
| 467 |
':py:class:`tuple`\\ [' |
| 468 |
':py:class:`tests.test_util.test_util_typing.MyList`\\ ' |
| 469 |
'[:py:class:`list`\\ [:py:class:`int`]], ' |
| 470 |
':py:class:`int`]' |
| 471 |
) |
| 472 |
|
| 473 |
|
| 474 |
def test_restify_Unpack() -> None: |
| 475 |
from typing_extensions import Unpack as UnpackCompat |
| 476 |
|
| 477 |
class X(t.TypedDict): |
| 478 |
x: int |
| 479 |
y: int |
| 480 |
label: str |
| 481 |
|
| 482 |
# Unpack is considered as typing special form so we always have '~' |
| 483 |
expect = r':py:obj:`~typing.Unpack`\ [:py:class:`X`]' |
| 484 |
assert restify(UnpackCompat['X'], 'fully-qualified-except-typing') == expect |
| 485 |
assert restify(UnpackCompat['X'], 'smart') == expect |
| 486 |
|
| 487 |
expect = r':py:obj:`~typing.Unpack`\ [:py:class:`X`]' |
| 488 |
assert restify(t.Unpack['X'], 'fully-qualified-except-typing') == expect |
| 489 |
assert restify(t.Unpack['X'], 'smart') == expect |
| 490 |
|
| 491 |
|
| 492 |
def test_restify_type_union_operator() -> None: |
| 493 |
assert restify(int | None) == ':py:class:`int` | :py:obj:`None`' |
| 494 |
assert restify(None | int) == ':py:obj:`None` | :py:class:`int`' |
| 495 |
assert restify(int | str) == ':py:class:`int` | :py:class:`str`' |
| 496 |
ann_rst = restify(int | str | None) |
| 497 |
assert ann_rst == ':py:class:`int` | :py:class:`str` | :py:obj:`None`' |
| 498 |
|
| 499 |
|
| 500 |
def test_restify_broken_type_hints() -> None: |
| 501 |
ann_rst = restify(BrokenType) |
| 502 |
assert ann_rst == ':py:class:`tests.test_util.test_util_typing.BrokenType`' |
| 503 |
ann_rst = restify(BrokenType, 'smart') |
| 504 |
assert ann_rst == ':py:class:`~tests.test_util.test_util_typing.BrokenType`' |
| 505 |
|
| 506 |
|
| 507 |
def test_restify_mock() -> None: |
| 508 |
with mock(['unknown']): |
| 509 |
import unknown # type: ignore[import-not-found] |
| 510 |
|
| 511 |
assert restify(unknown) == ':py:class:`unknown`' |
| 512 |
assert restify(unknown.secret.Class) == ':py:class:`unknown.secret.Class`' |
| 513 |
ann_rst = restify(unknown.secret.Class, 'smart') |
| 514 |
assert ann_rst == ':py:class:`~unknown.secret.Class`' |
| 515 |
|
| 516 |
|
| 517 |
def test_restify_type_hints_paramspec(): |
| 518 |
P = ParamSpec('P') |
| 519 |
|
| 520 |
assert restify(P) == ':py:obj:`tests.test_util.test_util_typing.P`' |
| 521 |
assert restify(P, 'smart') == ':py:obj:`~tests.test_util.test_util_typing.P`' |
| 522 |
|
| 523 |
assert restify(P.args) == 'P.args' |
| 524 |
assert restify(P.args, 'smart') == 'P.args' |
| 525 |
|
| 526 |
assert restify(P.kwargs) == 'P.kwargs' |
| 527 |
assert restify(P.kwargs, 'smart') == 'P.kwargs' |
| 528 |
|
| 529 |
|
| 530 |
def test_stringify_annotation() -> None: |
| 531 |
assert stringify_annotation(int, 'fully-qualified-except-typing') == 'int' |
| 532 |
assert stringify_annotation(int, 'smart') == 'int' |
| 533 |
|
| 534 |
assert stringify_annotation(str, 'fully-qualified-except-typing') == 'str' |
| 535 |
assert stringify_annotation(str, 'smart') == 'str' |
| 536 |
|
| 537 |
assert stringify_annotation(None, 'fully-qualified-except-typing') == 'None' |
| 538 |
assert stringify_annotation(None, 'smart') == 'None' |
| 539 |
|
| 540 |
ann_str = stringify_annotation(Integral, 'fully-qualified-except-typing') |
| 541 |
assert ann_str == 'numbers.Integral' |
| 542 |
assert stringify_annotation(Integral, 'smart') == '~numbers.Integral' |
| 543 |
|
| 544 |
ann_str = stringify_annotation(Struct, 'fully-qualified-except-typing') |
| 545 |
assert ann_str == 'struct.Struct' |
| 546 |
assert stringify_annotation(Struct, 'smart') == '~struct.Struct' |
| 547 |
|
| 548 |
ann_str = stringify_annotation(TracebackType, 'fully-qualified-except-typing') |
| 549 |
assert ann_str == 'types.TracebackType' |
| 550 |
assert stringify_annotation(TracebackType, 'smart') == '~types.TracebackType' |
| 551 |
|
| 552 |
ann_str = stringify_annotation(Path, 'fully-qualified-except-typing') |
| 553 |
assert ann_str == 'pathlib.Path' |
| 554 |
assert stringify_annotation(Path, 'smart') == '~pathlib.Path' |
| 555 |
|
| 556 |
assert stringify_annotation(Any, 'fully-qualified-except-typing') == 'Any' |
| 557 |
assert stringify_annotation(Any, 'fully-qualified') == 'typing.Any' |
| 558 |
assert stringify_annotation(Any, 'smart') == '~typing.Any' |
| 559 |
|
| 560 |
|
| 561 |
def test_stringify_type_hints_containers(): |
| 562 |
assert stringify_annotation(List, 'fully-qualified-except-typing') == 'List' |
| 563 |
assert stringify_annotation(List, 'fully-qualified') == 'typing.List' |
| 564 |
assert stringify_annotation(List, 'smart') == '~typing.List' |
| 565 |
|
| 566 |
assert stringify_annotation(Dict, 'fully-qualified-except-typing') == 'Dict' |
| 567 |
assert stringify_annotation(Dict, 'fully-qualified') == 'typing.Dict' |
| 568 |
assert stringify_annotation(Dict, 'smart') == '~typing.Dict' |
| 569 |
|
| 570 |
ann_str = stringify_annotation(List[int], 'fully-qualified-except-typing') |
| 571 |
assert ann_str == 'List[int]' |
| 572 |
assert stringify_annotation(List[int], 'fully-qualified') == 'typing.List[int]' |
| 573 |
assert stringify_annotation(List[int], 'smart') == '~typing.List[int]' |
| 574 |
|
| 575 |
ann_str = stringify_annotation(List[str], 'fully-qualified-except-typing') |
| 576 |
assert ann_str == 'List[str]' |
| 577 |
assert stringify_annotation(List[str], 'fully-qualified') == 'typing.List[str]' |
| 578 |
assert stringify_annotation(List[str], 'smart') == '~typing.List[str]' |
| 579 |
|
| 580 |
ann_str = stringify_annotation(Dict[str, float], 'fully-qualified-except-typing') |
| 581 |
assert ann_str == 'Dict[str, float]' |
| 582 |
ann_str = stringify_annotation(Dict[str, float], 'fully-qualified') |
| 583 |
assert ann_str == 'typing.Dict[str, float]' |
| 584 |
assert stringify_annotation(Dict[str, float], 'smart') == '~typing.Dict[str, float]' |
| 585 |
|
| 586 |
ann_str = stringify_annotation( |
| 587 |
Tuple[str, str, str], 'fully-qualified-except-typing' |
| 588 |
) |
| 589 |
assert ann_str == 'Tuple[str, str, str]' |
| 590 |
ann_str = stringify_annotation(Tuple[str, str, str], 'fully-qualified') |
| 591 |
assert ann_str == 'typing.Tuple[str, str, str]' |
| 592 |
ann_str = stringify_annotation(Tuple[str, str, str], 'smart') |
| 593 |
assert ann_str == '~typing.Tuple[str, str, str]' |
| 594 |
|
| 595 |
ann_str = stringify_annotation(Tuple[str, ...], 'fully-qualified-except-typing') |
| 596 |
assert ann_str == 'Tuple[str, ...]' |
| 597 |
ann_str = stringify_annotation(Tuple[str, ...], 'fully-qualified') |
| 598 |
assert ann_str == 'typing.Tuple[str, ...]' |
| 599 |
assert stringify_annotation(Tuple[str, ...], 'smart') == '~typing.Tuple[str, ...]' |
| 600 |
|
| 601 |
assert stringify_annotation(Tuple[()], 'fully-qualified-except-typing') == 'Tuple' |
| 602 |
assert stringify_annotation(Tuple[()], 'fully-qualified') == 'typing.Tuple' |
| 603 |
assert stringify_annotation(Tuple[()], 'smart') == '~typing.Tuple' |
| 604 |
|
| 605 |
ann_str = stringify_annotation( |
| 606 |
List[Dict[str, Tuple]], 'fully-qualified-except-typing' |
| 607 |
) |
| 608 |
assert ann_str == 'List[Dict[str, Tuple]]' |
| 609 |
ann_str = stringify_annotation(List[Dict[str, Tuple]], 'fully-qualified') |
| 610 |
assert ann_str == 'typing.List[typing.Dict[str, typing.Tuple]]' |
| 611 |
ann_str = stringify_annotation(List[Dict[str, Tuple]], 'smart') |
| 612 |
assert ann_str == '~typing.List[~typing.Dict[str, ~typing.Tuple]]' |
| 613 |
|
| 614 |
ann_str = stringify_annotation( |
| 615 |
MyList[Tuple[int, int]], 'fully-qualified-except-typing' |
| 616 |
) |
| 617 |
assert ann_str == 'tests.test_util.test_util_typing.MyList[Tuple[int, int]]' |
| 618 |
ann_str = stringify_annotation(MyList[Tuple[int, int]], 'fully-qualified') |
| 619 |
assert ann_str == ( |
| 620 |
'tests.test_util.test_util_typing.MyList[typing.Tuple[int, int]]' |
| 621 |
) |
| 622 |
ann_str = stringify_annotation(MyList[Tuple[int, int]], 'smart') |
| 623 |
assert ann_str == ( |
| 624 |
'~tests.test_util.test_util_typing.MyList[~typing.Tuple[int, int]]' |
| 625 |
) |
| 626 |
|
| 627 |
ann_str = stringify_annotation( |
| 628 |
t.Generator[None, None, None], 'fully-qualified-except-typing' |
| 629 |
) |
| 630 |
assert ann_str == 'Generator[None, None, None]' |
| 631 |
ann_str = stringify_annotation(t.Generator[None, None, None], 'fully-qualified') |
| 632 |
assert ann_str == 'typing.Generator[None, None, None]' |
| 633 |
ann_str = stringify_annotation(t.Generator[None, None, None], 'smart') |
| 634 |
assert ann_str == '~typing.Generator[None, None, None]' |
| 635 |
|
| 636 |
ann_str = stringify_annotation( |
| 637 |
abc.Generator[None, None, None], 'fully-qualified-except-typing' |
| 638 |
) |
| 639 |
assert ann_str == 'collections.abc.Generator[None, None, None]' |
| 640 |
ann_str = stringify_annotation(abc.Generator[None, None, None], 'fully-qualified') |
| 641 |
assert ann_str == 'collections.abc.Generator[None, None, None]' |
| 642 |
ann_str = stringify_annotation(abc.Generator[None, None, None], 'smart') |
| 643 |
assert ann_str == '~collections.abc.Generator[None, None, None]' |
| 644 |
|
| 645 |
ann_str = stringify_annotation(t.Iterator[None], 'fully-qualified-except-typing') |
| 646 |
assert ann_str == 'Iterator[None]' |
| 647 |
ann_str = stringify_annotation(t.Iterator[None], 'fully-qualified') |
| 648 |
assert ann_str == 'typing.Iterator[None]' |
| 649 |
assert stringify_annotation(t.Iterator[None], 'smart') == '~typing.Iterator[None]' |
| 650 |
|
| 651 |
ann_str = stringify_annotation(abc.Iterator[None], 'fully-qualified-except-typing') |
| 652 |
assert ann_str == 'collections.abc.Iterator[None]' |
| 653 |
ann_str = stringify_annotation(abc.Iterator[None], 'fully-qualified') |
| 654 |
assert ann_str == 'collections.abc.Iterator[None]' |
| 655 |
ann_str = stringify_annotation(abc.Iterator[None], 'smart') |
| 656 |
assert ann_str == '~collections.abc.Iterator[None]' |
| 657 |
|
| 658 |
|
| 659 |
def test_stringify_type_hints_pep_585(): |
| 660 |
ann_str = stringify_annotation(list[int], 'fully-qualified-except-typing') |
| 661 |
assert ann_str == 'list[int]' |
| 662 |
assert stringify_annotation(list[int], 'smart') == 'list[int]' |
| 663 |
|
| 664 |
ann_str = stringify_annotation(list[str], 'fully-qualified-except-typing') |
| 665 |
assert ann_str == 'list[str]' |
| 666 |
assert stringify_annotation(list[str], 'smart') == 'list[str]' |
| 667 |
|
| 668 |
ann_str = stringify_annotation(dict[str, float], 'fully-qualified-except-typing') |
| 669 |
assert ann_str == 'dict[str, float]' |
| 670 |
assert stringify_annotation(dict[str, float], 'smart') == 'dict[str, float]' |
| 671 |
|
| 672 |
ann_str = stringify_annotation( |
| 673 |
tuple[str, str, str], 'fully-qualified-except-typing' |
| 674 |
) |
| 675 |
assert ann_str == 'tuple[str, str, str]' |
| 676 |
assert stringify_annotation(tuple[str, str, str], 'smart') == 'tuple[str, str, str]' |
| 677 |
|
| 678 |
ann_str = stringify_annotation(tuple[str, ...], 'fully-qualified-except-typing') |
| 679 |
assert ann_str == 'tuple[str, ...]' |
| 680 |
assert stringify_annotation(tuple[str, ...], 'smart') == 'tuple[str, ...]' |
| 681 |
|
| 682 |
assert ( |
| 683 |
stringify_annotation(tuple[()], 'fully-qualified-except-typing') == 'tuple[()]' |
| 684 |
) |
| 685 |
assert stringify_annotation(tuple[()], 'smart') == 'tuple[()]' |
| 686 |
|
| 687 |
ann_str = stringify_annotation( |
| 688 |
list[dict[str, tuple]], 'fully-qualified-except-typing' |
| 689 |
) |
| 690 |
assert ann_str == 'list[dict[str, tuple]]' |
| 691 |
ann_str = stringify_annotation(list[dict[str, tuple]], 'smart') |
| 692 |
assert ann_str == 'list[dict[str, tuple]]' |
| 693 |
|
| 694 |
ann_str = stringify_annotation( |
| 695 |
MyList[tuple[int, int]], 'fully-qualified-except-typing' |
| 696 |
) |
| 697 |
assert ann_str == 'tests.test_util.test_util_typing.MyList[tuple[int, int]]' |
| 698 |
ann_str = stringify_annotation(MyList[tuple[int, int]], 'fully-qualified') |
| 699 |
assert ann_str == 'tests.test_util.test_util_typing.MyList[tuple[int, int]]' |
| 700 |
ann_str = stringify_annotation(MyList[tuple[int, int]], 'smart') |
| 701 |
assert ann_str == '~tests.test_util.test_util_typing.MyList[tuple[int, int]]' |
| 702 |
|
| 703 |
ann_str = stringify_annotation(type[int], 'fully-qualified-except-typing') |
| 704 |
assert ann_str == 'type[int]' |
| 705 |
assert stringify_annotation(type[int], 'smart') == 'type[int]' |
| 706 |
|
| 707 |
# Mix typing and pep 585 |
| 708 |
ann_str = stringify_annotation( |
| 709 |
tuple[List[dict[int, str]], str, ...], 'fully-qualified-except-typing' |
| 710 |
) |
| 711 |
assert ann_str == 'tuple[List[dict[int, str]], str, ...]' |
| 712 |
ann_str = stringify_annotation(tuple[List[dict[int, str]], str, ...], 'smart') |
| 713 |
assert ann_str == 'tuple[~typing.List[dict[int, str]], str, ...]' |
| 714 |
|
| 715 |
|
| 716 |
def test_stringify_Annotated() -> None: |
| 717 |
ann_str = stringify_annotation( |
| 718 |
Annotated[str, 'foo', 'bar'], 'fully-qualified-except-typing' |
| 719 |
) |
| 720 |
assert ann_str == "Annotated[str, 'foo', 'bar']" |
| 721 |
ann_str = stringify_annotation(Annotated[str, 'foo', 'bar'], 'smart') |
| 722 |
assert ann_str == "~typing.Annotated[str, 'foo', 'bar']" |
| 723 |
ann_str = stringify_annotation( |
| 724 |
Annotated[float, Gt(-10.0)], 'fully-qualified-except-typing' |
| 725 |
) |
| 726 |
assert ann_str == ( |
| 727 |
'Annotated[float, tests.test_util.test_util_typing.Gt(gt=-10.0)]' |
| 728 |
) |
| 729 |
ann_str = stringify_annotation(Annotated[float, Gt(-10.0)], 'smart') |
| 730 |
assert ann_str == ( |
| 731 |
'~typing.Annotated[float, ~tests.test_util.test_util_typing.Gt(gt=-10.0)]' |
| 732 |
) |
| 733 |
|
| 734 |
|
| 735 |
def test_stringify_Unpack() -> None: |
| 736 |
class X(t.TypedDict): |
| 737 |
x: int |
| 738 |
y: int |
| 739 |
label: str |
| 740 |
|
| 741 |
assert stringify_annotation(t.Unpack['X']) == 'Unpack[X]' |
| 742 |
assert stringify_annotation(t.Unpack['X'], 'smart') == '~typing.Unpack[X]' |
| 743 |
|
| 744 |
|
| 745 |
def test_stringify_type_hints_string() -> None: |
| 746 |
assert stringify_annotation('int', 'fully-qualified-except-typing') == 'int' |
| 747 |
assert stringify_annotation('int', 'fully-qualified') == 'int' |
| 748 |
assert stringify_annotation('int', 'smart') == 'int' |
| 749 |
|
| 750 |
assert stringify_annotation('str', 'fully-qualified-except-typing') == 'str' |
| 751 |
assert stringify_annotation('str', 'fully-qualified') == 'str' |
| 752 |
assert stringify_annotation('str', 'smart') == 'str' |
| 753 |
|
| 754 |
ann_str = stringify_annotation(List['int'], 'fully-qualified-except-typing') |
| 755 |
assert ann_str == 'List[int]' |
| 756 |
assert stringify_annotation(List['int'], 'fully-qualified') == 'typing.List[int]' |
| 757 |
assert stringify_annotation(List['int'], 'smart') == '~typing.List[int]' |
| 758 |
|
| 759 |
ann_str = stringify_annotation(list['int'], 'fully-qualified-except-typing') |
| 760 |
assert ann_str == 'list[int]' |
| 761 |
assert stringify_annotation(list['int'], 'fully-qualified') == 'list[int]' |
| 762 |
assert stringify_annotation(list['int'], 'smart') == 'list[int]' |
| 763 |
|
| 764 |
ann_str = stringify_annotation('Tuple[str]', 'fully-qualified-except-typing') |
| 765 |
assert ann_str == 'Tuple[str]' |
| 766 |
assert stringify_annotation('Tuple[str]', 'fully-qualified') == 'Tuple[str]' |
| 767 |
assert stringify_annotation('Tuple[str]', 'smart') == 'Tuple[str]' |
| 768 |
|
| 769 |
ann_str = stringify_annotation('tuple[str]', 'fully-qualified-except-typing') |
| 770 |
assert ann_str == 'tuple[str]' |
| 771 |
assert stringify_annotation('tuple[str]', 'fully-qualified') == 'tuple[str]' |
| 772 |
assert stringify_annotation('tuple[str]', 'smart') == 'tuple[str]' |
| 773 |
|
| 774 |
assert stringify_annotation('unknown', 'fully-qualified-except-typing') == 'unknown' |
| 775 |
assert stringify_annotation('unknown', 'fully-qualified') == 'unknown' |
| 776 |
assert stringify_annotation('unknown', 'smart') == 'unknown' |
| 777 |
|
| 778 |
|
| 779 |
def test_stringify_type_hints_Callable() -> None: |
| 780 |
ann_str = stringify_annotation(t.Callable, 'fully-qualified-except-typing') |
| 781 |
assert ann_str == 'Callable' |
| 782 |
assert stringify_annotation(t.Callable, 'fully-qualified') == 'typing.Callable' |
| 783 |
assert stringify_annotation(t.Callable, 'smart') == '~typing.Callable' |
| 784 |
|
| 785 |
ann_str = stringify_annotation( |
| 786 |
t.Callable[[str], int], 'fully-qualified-except-typing' |
| 787 |
) |
| 788 |
assert ann_str == 'Callable[[str], int]' |
| 789 |
ann_str = stringify_annotation(t.Callable[[str], int], 'fully-qualified') |
| 790 |
assert ann_str == 'typing.Callable[[str], int]' |
| 791 |
ann_str = stringify_annotation(t.Callable[[str], int], 'smart') |
| 792 |
assert ann_str == '~typing.Callable[[str], int]' |
| 793 |
|
| 794 |
ann_str = stringify_annotation( |
| 795 |
t.Callable[..., int], 'fully-qualified-except-typing' |
| 796 |
) |
| 797 |
assert ann_str == 'Callable[[...], int]' |
| 798 |
ann_str = stringify_annotation(t.Callable[..., int], 'fully-qualified') |
| 799 |
assert ann_str == 'typing.Callable[[...], int]' |
| 800 |
ann_str = stringify_annotation(t.Callable[..., int], 'smart') |
| 801 |
assert ann_str == '~typing.Callable[[...], int]' |
| 802 |
|
| 803 |
ann_str = stringify_annotation(abc.Callable, 'fully-qualified-except-typing') |
| 804 |
assert ann_str == 'collections.abc.Callable' |
| 805 |
ann_str = stringify_annotation(abc.Callable, 'fully-qualified') |
| 806 |
assert ann_str == 'collections.abc.Callable' |
| 807 |
assert stringify_annotation(abc.Callable, 'smart') == '~collections.abc.Callable' |
| 808 |
|
| 809 |
ann_str = stringify_annotation( |
| 810 |
abc.Callable[[str], int], 'fully-qualified-except-typing' |
| 811 |
) |
| 812 |
assert ann_str == 'collections.abc.Callable[[str], int]' |
| 813 |
ann_str = stringify_annotation(abc.Callable[[str], int], 'fully-qualified') |
| 814 |
assert ann_str == 'collections.abc.Callable[[str], int]' |
| 815 |
ann_str = stringify_annotation(abc.Callable[[str], int], 'smart') |
| 816 |
assert ann_str == '~collections.abc.Callable[[str], int]' |
| 817 |
|
| 818 |
ann_str = stringify_annotation( |
| 819 |
abc.Callable[..., int], 'fully-qualified-except-typing' |
| 820 |
) |
| 821 |
assert ann_str == 'collections.abc.Callable[[...], int]' |
| 822 |
ann_str = stringify_annotation(abc.Callable[..., int], 'fully-qualified') |
| 823 |
assert ann_str == 'collections.abc.Callable[[...], int]' |
| 824 |
ann_str = stringify_annotation(abc.Callable[..., int], 'smart') |
| 825 |
assert ann_str == '~collections.abc.Callable[[...], int]' |
| 826 |
|
| 827 |
|
| 828 |
def test_stringify_type_hints_Union() -> None: |
| 829 |
ann_str = stringify_annotation(Optional[int], 'fully-qualified-except-typing') |
| 830 |
assert ann_str == 'int | None' |
| 831 |
assert stringify_annotation(Optional[int], 'fully-qualified') == 'int | None' |
| 832 |
assert stringify_annotation(Optional[int], 'smart') == 'int | None' |
| 833 |
|
| 834 |
ann_str = stringify_annotation(Union[int, None], 'fully-qualified-except-typing') |
| 835 |
assert ann_str == 'int | None' |
| 836 |
ann_str = stringify_annotation(Union[None, int], 'fully-qualified-except-typing') |
| 837 |
assert ann_str == 'None | int' |
| 838 |
assert stringify_annotation(Union[int, None], 'fully-qualified') == 'int | None' |
| 839 |
assert stringify_annotation(Union[None, int], 'fully-qualified') == 'None | int' |
| 840 |
assert stringify_annotation(Union[int, None], 'smart') == 'int | None' |
| 841 |
assert stringify_annotation(Union[None, int], 'smart') == 'None | int' |
| 842 |
|
| 843 |
ann_str = stringify_annotation(Union[int, str], 'fully-qualified-except-typing') |
| 844 |
assert ann_str == 'int | str' |
| 845 |
assert stringify_annotation(Union[int, str], 'fully-qualified') == 'int | str' |
| 846 |
assert stringify_annotation(Union[int, str], 'smart') == 'int | str' |
| 847 |
|
| 848 |
ann_str = stringify_annotation( |
| 849 |
Union[int, Integral], 'fully-qualified-except-typing' |
| 850 |
) |
| 851 |
assert ann_str == 'int | numbers.Integral' |
| 852 |
ann_str = stringify_annotation(Union[int, Integral], 'fully-qualified') |
| 853 |
assert ann_str == 'int | numbers.Integral' |
| 854 |
ann_str = stringify_annotation(Union[int, Integral], 'smart') |
| 855 |
assert ann_str == 'int | ~numbers.Integral' |
| 856 |
|
| 857 |
ann_str = stringify_annotation( |
| 858 |
Union[MyClass1, MyClass2], 'fully-qualified-except-typing' |
| 859 |
) |
| 860 |
assert ann_str == ( |
| 861 |
'tests.test_util.test_util_typing.MyClass1 | tests.test_util.test_util_typing.<MyClass2>' |
| 862 |
) |
| 863 |
ann_str = stringify_annotation(Union[MyClass1, MyClass2], 'fully-qualified') |
| 864 |
assert ann_str == ( |
| 865 |
'tests.test_util.test_util_typing.MyClass1 | tests.test_util.test_util_typing.<MyClass2>' |
| 866 |
) |
| 867 |
ann_str = stringify_annotation(Union[MyClass1, MyClass2], 'smart') |
| 868 |
assert ann_str == ( |
| 869 |
'~tests.test_util.test_util_typing.MyClass1 | ~tests.test_util.test_util_typing.<MyClass2>' |
| 870 |
) |
| 871 |
|
| 872 |
|
| 873 |
def test_stringify_type_hints_typevars(): |
| 874 |
T = TypeVar('T') |
| 875 |
T_co = TypeVar('T_co', covariant=True) |
| 876 |
T_contra = TypeVar('T_contra', contravariant=True) |
| 877 |
|
| 878 |
ann_str = stringify_annotation(T, 'fully-qualified-except-typing') |
| 879 |
assert ann_str == 'tests.test_util.test_util_typing.T' |
| 880 |
assert stringify_annotation(T, 'smart') == '~tests.test_util.test_util_typing.T' |
| 881 |
|
| 882 |
ann_str = stringify_annotation(T_co, 'fully-qualified-except-typing') |
| 883 |
assert ann_str == 'tests.test_util.test_util_typing.T_co' |
| 884 |
ann_str = stringify_annotation(T_co, 'smart') |
| 885 |
assert ann_str == '~tests.test_util.test_util_typing.T_co' |
| 886 |
|
| 887 |
ann_str = stringify_annotation(T_contra, 'fully-qualified-except-typing') |
| 888 |
assert ann_str == 'tests.test_util.test_util_typing.T_contra' |
| 889 |
ann_str = stringify_annotation(T_contra, 'smart') |
| 890 |
assert ann_str == '~tests.test_util.test_util_typing.T_contra' |
| 891 |
|
| 892 |
ann_str = stringify_annotation(List[T], 'fully-qualified-except-typing') |
| 893 |
assert ann_str == 'List[tests.test_util.test_util_typing.T]' |
| 894 |
ann_str = stringify_annotation(List[T], 'smart') |
| 895 |
assert ann_str == '~typing.List[~tests.test_util.test_util_typing.T]' |
| 896 |
|
| 897 |
ann_str = stringify_annotation(list[T], 'fully-qualified-except-typing') |
| 898 |
assert ann_str == 'list[tests.test_util.test_util_typing.T]' |
| 899 |
ann_str = stringify_annotation(list[T], 'smart') |
| 900 |
assert ann_str == 'list[~tests.test_util.test_util_typing.T]' |
| 901 |
|
| 902 |
ann_str = stringify_annotation(MyInt, 'fully-qualified-except-typing') |
| 903 |
assert ann_str == 'tests.test_util.test_util_typing.MyInt' |
| 904 |
ann_str = stringify_annotation(MyInt, 'smart') |
| 905 |
assert ann_str == '~tests.test_util.test_util_typing.MyInt' |
| 906 |
|
| 907 |
|
| 908 |
def test_stringify_type_hints_custom_class() -> None: |
| 909 |
ann_str = stringify_annotation(MyClass1, 'fully-qualified-except-typing') |
| 910 |
assert ann_str == 'tests.test_util.test_util_typing.MyClass1' |
| 911 |
ann_str = stringify_annotation(MyClass1, 'smart') |
| 912 |
assert ann_str == '~tests.test_util.test_util_typing.MyClass1' |
| 913 |
|
| 914 |
ann_str = stringify_annotation(MyClass2, 'fully-qualified-except-typing') |
| 915 |
assert ann_str == 'tests.test_util.test_util_typing.<MyClass2>' |
| 916 |
ann_str = stringify_annotation(MyClass2, 'smart') |
| 917 |
assert ann_str == '~tests.test_util.test_util_typing.<MyClass2>' |
| 918 |
|
| 919 |
|
| 920 |
def test_stringify_type_hints_alias() -> None: |
| 921 |
MyStr = str |
| 922 |
MyTuple = Tuple[str, str] |
| 923 |
|
| 924 |
assert stringify_annotation(MyStr, 'fully-qualified-except-typing') == 'str' |
| 925 |
assert stringify_annotation(MyStr, 'smart') == 'str' |
| 926 |
|
| 927 |
assert stringify_annotation(MyTuple) == 'Tuple[str, str]' |
| 928 |
assert stringify_annotation(MyTuple, 'smart') == '~typing.Tuple[str, str]' |
| 929 |
|
| 930 |
|
| 931 |
def test_stringify_type_Literal() -> None: |
| 932 |
ann_str = stringify_annotation( |
| 933 |
Literal[1, '2', '\r'], 'fully-qualified-except-typing' |
| 934 |
) |
| 935 |
assert ann_str == "Literal[1, '2', '\\r']" |
| 936 |
ann_str = stringify_annotation(Literal[1, '2', '\r'], 'fully-qualified') |
| 937 |
assert ann_str == "typing.Literal[1, '2', '\\r']" |
| 938 |
ann_str = stringify_annotation(Literal[1, '2', '\r'], 'smart') |
| 939 |
assert ann_str == "~typing.Literal[1, '2', '\\r']" |
| 940 |
|
| 941 |
ann_str = stringify_annotation(Literal[MyEnum.a], 'fully-qualified-except-typing') |
| 942 |
assert ann_str == 'Literal[tests.test_util.test_util_typing.MyEnum.a]' |
| 943 |
ann_str = stringify_annotation(Literal[MyEnum.a], 'fully-qualified') |
| 944 |
assert ann_str == 'typing.Literal[tests.test_util.test_util_typing.MyEnum.a]' |
| 945 |
ann_str = stringify_annotation(Literal[MyEnum.a], 'smart') |
| 946 |
assert ann_str == '~typing.Literal[MyEnum.a]' |
| 947 |
|
| 948 |
|
| 949 |
def test_stringify_type_union_operator() -> None: |
| 950 |
assert stringify_annotation(int | None) == 'int | None' |
| 951 |
assert stringify_annotation(int | None, 'smart') == 'int | None' |
| 952 |
|
| 953 |
assert stringify_annotation(int | str) == 'int | str' |
| 954 |
assert stringify_annotation(int | str, 'smart') == 'int | str' |
| 955 |
|
| 956 |
assert stringify_annotation(int | str | None) == 'int | str | None' |
| 957 |
assert stringify_annotation(int | str | None, 'smart') == 'int | str | None' |
| 958 |
|
| 959 |
ann_str = stringify_annotation( |
| 960 |
int | tuple[dict[str, int | None], list[int | str]] | None |
| 961 |
) |
| 962 |
assert ann_str == 'int | tuple[dict[str, int | None], list[int | str]] | None' |
| 963 |
ann_str = stringify_annotation( |
| 964 |
int | tuple[dict[str, int | None], list[int | str]] | None, 'smart' |
| 965 |
) |
| 966 |
assert ann_str == 'int | tuple[dict[str, int | None], list[int | str]] | None' |
| 967 |
|
| 968 |
assert stringify_annotation(int | Struct) == 'int | struct.Struct' |
| 969 |
assert stringify_annotation(int | Struct, 'smart') == 'int | ~struct.Struct' |
| 970 |
|
| 971 |
|
| 972 |
def test_stringify_broken_type_hints() -> None: |
| 973 |
ann_str = stringify_annotation(BrokenType, 'fully-qualified-except-typing') |
| 974 |
assert ann_str == 'tests.test_util.test_util_typing.BrokenType' |
| 975 |
ann_str = stringify_annotation(BrokenType, 'smart') |
| 976 |
assert ann_str == '~tests.test_util.test_util_typing.BrokenType' |
| 977 |
|
| 978 |
|
| 979 |
def test_stringify_mock() -> None: |
| 980 |
with mock(['unknown']): |
| 981 |
import unknown # ty: ignore[unresolved-import] |
| 982 |
|
| 983 |
ann_str = stringify_annotation(unknown, 'fully-qualified-except-typing') |
| 984 |
assert ann_str == 'unknown' |
| 985 |
ann_str = stringify_annotation( |
| 986 |
unknown.secret.Class, 'fully-qualified-except-typing' |
| 987 |
) |
| 988 |
assert ann_str == 'unknown.secret.Class' |
| 989 |
ann_str = stringify_annotation(unknown.secret.Class, 'smart') |
| 990 |
assert ann_str == 'unknown.secret.Class' |
| 991 |
|
| 992 |
|
| 993 |
def test_stringify_type_ForwardRef(): |
| 994 |
assert stringify_annotation(ForwardRef('MyInt')) == 'MyInt' |
| 995 |
assert stringify_annotation(ForwardRef('MyInt'), 'smart') == 'MyInt' |
| 996 |
|
| 997 |
assert stringify_annotation(list[ForwardRef('MyInt')]) == 'list[MyInt]' # ty: ignore[invalid-type-form] |
| 998 |
assert stringify_annotation(list[ForwardRef('MyInt')], 'smart') == 'list[MyInt]' # ty: ignore[invalid-type-form] |
| 999 |
|
| 1000 |
ann_str = stringify_annotation( |
| 1001 |
Tuple[dict[ForwardRef('MyInt'), str], list[List[int]]] # ty: ignore[invalid-type-form] |
| 1002 |
) |
| 1003 |
assert ann_str == 'Tuple[dict[MyInt, str], list[List[int]]]' |
| 1004 |
ann_str = stringify_annotation( |
| 1005 |
Tuple[dict[ForwardRef('MyInt'), str], list[List[int]]], # ty: ignore[invalid-type-form] |
| 1006 |
'fully-qualified-except-typing', |
| 1007 |
) |
| 1008 |
assert ann_str == 'Tuple[dict[MyInt, str], list[List[int]]]' |
| 1009 |
ann_str = stringify_annotation( |
| 1010 |
Tuple[dict[ForwardRef('MyInt'), str], list[List[int]]], # ty: ignore[invalid-type-form] |
| 1011 |
'smart', |
| 1012 |
) |
| 1013 |
assert ann_str == '~typing.Tuple[dict[MyInt, str], list[~typing.List[int]]]' |
| 1014 |
|
| 1015 |
|
| 1016 |
def test_stringify_type_hints_paramspec(): |
| 1017 |
P = ParamSpec('P') |
| 1018 |
|
| 1019 |
assert stringify_annotation(P, 'fully-qualified') == '~P' |
| 1020 |
assert stringify_annotation(P, 'fully-qualified-except-typing') == '~P' |
| 1021 |
assert stringify_annotation(P, 'smart') == '~P' |
| 1022 |
|
| 1023 |
assert stringify_annotation(P.args, 'fully-qualified') == 'typing.~P' |
| 1024 |
assert stringify_annotation(P.args, 'fully-qualified-except-typing') == '~P' |
| 1025 |
assert stringify_annotation(P.args, 'smart') == '~typing.~P' |
| 1026 |
|
| 1027 |
assert stringify_annotation(P.kwargs, 'fully-qualified') == 'typing.~P' |
| 1028 |
assert stringify_annotation(P.kwargs, 'fully-qualified-except-typing') == '~P' |
| 1029 |
assert stringify_annotation(P.kwargs, 'smart') == '~typing.~P' |