1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
from __future__ import annotations
import pytest
from pyupgrade._data import Settings
from pyupgrade._main import _fix_plugins
@pytest.mark.parametrize(
's',
(
'',
pytest.param(
'from typing import NamedTuple as wat\n'
'C = wat("C", ("a", int))\n',
id='currently not following as imports',
),
pytest.param('C = typing.NamedTuple("C", ())', id='no types'),
pytest.param('C = typing.NamedTuple("C")', id='not enough args'),
pytest.param(
'C = typing.NamedTuple("C", (), nonsense=1)',
id='namedtuple with named args',
),
pytest.param(
'C = typing.NamedTuple("C", {"foo": int, "bar": str})',
id='namedtuple without a list/tuple',
),
pytest.param(
'C = typing.NamedTuple("C", [["a", str], ["b", int]])',
id='namedtuple without inner tuples',
),
pytest.param(
'C = typing.NamedTuple("C", [(), ()])',
id='namedtuple but inner tuples are incorrect length',
),
pytest.param(
'C = typing.NamedTuple("C", [(not_a_str, str)])',
id='namedtuple but attribute name is not a string',
),
pytest.param(
'C = typing.NamedTuple("C", [("def", int)])',
id='uses keyword',
),
pytest.param(
'C = typing.NamedTuple("C", [("not-ok", int)])',
id='uses non-identifier',
),
pytest.param(
'C = typing.NamedTuple("C", *types)',
id='NamedTuple starargs',
),
pytest.param(
'from .typing import NamedTuple\n'
'C = NamedTuple("C", [("a", int)])\n',
id='relative imports',
),
),
)
def test_typing_named_tuple_noop(s):
assert _fix_plugins(s, settings=Settings(min_version=(3, 6))) == s
@pytest.mark.parametrize(
('s', 'expected'),
(
pytest.param(
'from typing import NamedTuple\n'
'C = NamedTuple("C", [("a", int), ("b", str)])\n',
'from typing import NamedTuple\n'
'class C(NamedTuple):\n'
' a: int\n'
' b: str\n',
id='typing from import',
),
pytest.param(
'import typing\n'
'C = typing.NamedTuple("C", [("a", int), ("b", str)])\n',
'import typing\n'
'class C(typing.NamedTuple):\n'
' a: int\n'
' b: str\n',
id='import typing',
),
pytest.param(
'C = typing.NamedTuple("C", [("a", List[int])])',
'class C(typing.NamedTuple):\n'
' a: List[int]',
id='generic attribute types',
),
pytest.param(
'C = typing.NamedTuple("C", [("a", Mapping[int, str])])',
'class C(typing.NamedTuple):\n'
' a: Mapping[int, str]',
id='generic attribute types with multi types',
),
pytest.param(
'C = typing.NamedTuple("C", [("a", "Queue[int]")])',
'class C(typing.NamedTuple):\n'
" a: 'Queue[int]'",
id='quoted type names',
),
pytest.param(
'C = typing.NamedTuple("C", [("a", Tuple[int, ...])])',
'class C(typing.NamedTuple):\n'
' a: Tuple[int, ...]',
id='type with ellipsis',
),
pytest.param(
'C = typing.NamedTuple("C", [("a", Callable[[Any], None])])',
'class C(typing.NamedTuple):\n'
' a: Callable[[Any], None]',
id='type containing a list',
),
pytest.param(
'if False:\n'
' pass\n'
'C = typing.NamedTuple("C", [("a", int)])\n',
'if False:\n'
' pass\n'
'class C(typing.NamedTuple):\n'
' a: int\n',
id='class directly after block',
),
pytest.param(
'if True:\n'
' C = typing.NamedTuple("C", [("a", int)])\n',
'if True:\n'
' class C(typing.NamedTuple):\n'
' a: int\n',
id='indented',
),
pytest.param(
'if True:\n'
' ...\n'
' C = typing.NamedTuple("C", [("a", int)])\n',
'if True:\n'
' ...\n'
' class C(typing.NamedTuple):\n'
' a: int\n',
id='indented, but on next line',
),
pytest.param(
# mypy treats Tuple[int] and Tuple[int,] the same so in practice
# preserving this doesn't really matter
'C = typing.NamedTuple("C", [("a", Tuple[int,])])',
'class C(typing.NamedTuple):\n'
' a: Tuple[int,]',
id='actually a tuple in generic argument',
),
pytest.param(
'from typing import NamedTuple\n'
'C = NamedTuple( # 1\n'
' # 2\n'
' "C", # 3\n'
' # 4\n'
' [("x", "int")], # 5\n'
' # 6\n'
') # 7\n',
'from typing import NamedTuple\n'
'class C(NamedTuple):\n'
' # 1\n'
' # 2\n'
' # 3\n'
' # 4\n'
" x: 'int' # 5\n"
' # 6\n'
' # 7\n',
id='preserves comments in all positions',
),
pytest.param(
'from typing import NamedTuple, Optional\n'
'DeferredNode = NamedTuple(\n'
' "DeferredNode",\n'
' [\n'
' ("active_typeinfo", Optional[int]), # this member (and\n'
' # its semantics)\n'
' ]\n'
')\n',
'from typing import NamedTuple, Optional\n'
'class DeferredNode(NamedTuple):\n'
' active_typeinfo: Optional[int] # this member (and\n'
' # its semantics)\n',
id='preserves comments without alignment',
),
pytest.param(
'from typing import NamedTuple\n'
'Foo = NamedTuple("Foo", [("union", str | None)])',
'from typing import NamedTuple\n'
'class Foo(NamedTuple):\n'
' union: str | None',
id='BitOr unparse error',
),
),
)
def test_fix_typing_named_tuple(s, expected):
assert _fix_plugins(s, settings=Settings(min_version=(3, 6))) == expected
@pytest.mark.parametrize(
's',
(
pytest.param(
'from wat import TypedDict\n'
'Q = TypedDict("Q")\n',
id='from imported from elsewhere',
),
pytest.param('D = typing.TypedDict("D")', id='no typed kwargs'),
pytest.param('D = typing.TypedDict("D", {})', id='no typed args'),
pytest.param('D = typing.TypedDict("D", {}, a=int)', id='both'),
pytest.param('D = typing.TypedDict("D", 1)', id='not a dict'),
pytest.param(
'D = typing.TypedDict("D", {1: str})',
id='key is not a string',
),
pytest.param(
'D = typing.TypedDict("D", {"a-b": str})',
id='key is not an identifier',
),
pytest.param(
'D = typing.TypedDict("D", {"class": str})',
id='key is a keyword',
),
pytest.param(
'D = typing.TypedDict("D", {**d, "a": str})',
id='dictionary splat operator',
),
pytest.param(
'C = typing.TypedDict("C", *types)',
id='starargs',
),
pytest.param(
'D = typing.TypedDict("D", **types)',
id='starstarkwargs',
),
pytest.param(
'D = typing.TypedDict("D", x=int, total=False)',
id='kw_typed_dict with total',
),
),
)
def test_typing_typed_dict_noop(s):
assert _fix_plugins(s, settings=Settings(min_version=(3, 6))) == s
@pytest.mark.parametrize(
('s', 'expected'),
(
pytest.param(
'from typing import TypedDict\n'
'D = TypedDict("D", a=int)\n',
'from typing import TypedDict\n'
'class D(TypedDict):\n'
' a: int\n',
id='keyword TypedDict from imported',
),
pytest.param(
'import typing\n'
'D = typing.TypedDict("D", a=int)\n',
'import typing\n'
'class D(typing.TypedDict):\n'
' a: int\n',
id='keyword TypedDict from attribute',
),
pytest.param(
'import typing\n'
'D = typing.TypedDict("D", {"a": int})\n',
'import typing\n'
'class D(typing.TypedDict):\n'
' a: int\n',
id='TypedDict from dict literal',
),
pytest.param(
'import typing\n'
'D = typing.TypedDict("D", {"a": typing.Literal["b", b"c"]})\n',
'import typing\n'
'class D(typing.TypedDict):\n'
" a: typing.Literal['b', b'c']\n",
id='with Literal of bytes',
),
pytest.param(
'import typing\n'
'D = typing.TypedDict("D", {"a": int}, total=False)\n',
'import typing\n'
'class D(typing.TypedDict, total=False):\n'
' a: int\n',
id='TypedDict from dict literal with total',
),
pytest.param(
'import typing\n'
'D = typing.TypedDict("D", {\n'
' # first comment\n'
' "a": int, # inline comment\n'
' # second comment\n'
' "b": str,\n'
' # third comment\n'
'}, total=False)\n',
'import typing\n'
'class D(typing.TypedDict, total=False):\n'
' # first comment\n'
' a: int # inline comment\n'
' # second comment\n'
' b: str\n'
' # third comment\n',
id='preserves comments',
),
pytest.param(
'from typing_extensions import TypedDict\n'
'D = TypedDict("D", a=int)\n',
'from typing_extensions import TypedDict\n'
'class D(TypedDict):\n'
' a: int\n',
id='keyword TypedDict from typing_extensions',
),
pytest.param(
'import typing_extensions\n'
'D = typing_extensions.TypedDict("D", {"a": int})\n',
'import typing_extensions\n'
'class D(typing_extensions.TypedDict):\n'
' a: int\n',
id='dict TypedDict from typing_extensions',
),
pytest.param(
'import typing_extensions\n'
'D = typing_extensions.TypedDict("D", {"a": int}, total=True)\n',
'import typing_extensions\n'
'class D(typing_extensions.TypedDict, total=True):\n'
' a: int\n',
id='keyword TypedDict from typing_extensions, with total',
),
pytest.param(
'from typing import List\n'
'from typing_extensions import TypedDict\n'
'Foo = TypedDict("Foo", {"lsts": List[List[int]]})',
'from typing import List\n'
'from typing_extensions import TypedDict\n'
'class Foo(TypedDict):\n'
' lsts: List[List[int]]',
id='index unparse error',
),
pytest.param(
'import typing\n'
'if True:\n'
' if False:\n'
' pass\n'
' D = typing.TypedDict("D", a=int)\n',
'import typing\n'
'if True:\n'
' if False:\n'
' pass\n'
' class D(typing.TypedDict):\n'
' a: int\n',
id='right after a dedent',
),
pytest.param(
'from typing import TypedDict\n'
'Foo = TypedDict("Foo", {"union": str | int | None})',
'from typing import TypedDict\n'
'class Foo(TypedDict):\n'
' union: str | int | None',
id='BitOr unparse error',
),
),
)
def test_typing_typed_dict(s, expected):
assert _fix_plugins(s, settings=Settings(min_version=(3, 6))) == expected
|