| 1 |
from textwrap import dedent |
| 2 |
from operator import eq, ge, lt |
| 3 |
import re |
| 4 |
import os |
| 5 |
from typing import Any |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from jedi.inference.gradual.conversion import _stub_to_python_value_set |
| 10 |
from ..helpers import get_example_dir |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.parametrize( |
| 14 |
'code, sig, names, op, version', [ |
| 15 |
('import math; math.cos', 'cos(x, /)', ['x'], ge, (3, 6)), |
| 16 |
|
| 17 |
('next', 'next(iterator, default=None, /)', ['iterator', 'default'], lt, (3, 12)), |
| 18 |
('next', 'next()', [], eq, (3, 12)), |
| 19 |
('next', 'next(iterator, default=None, /)', ['iterator', 'default'], ge, (3, 13)), |
| 20 |
|
| 21 |
('str', "str(object='', /) -> str", ['object'], ge, (3, 6)), |
| 22 |
|
| 23 |
('pow', 'pow(base, exp, mod=None)', ['base', 'exp', 'mod'], ge, (3, 8)), |
| 24 |
|
| 25 |
('bytes.partition', 'partition(self, sep, /)', ['self', 'sep'], ge, (3, 6)), |
| 26 |
|
| 27 |
('bytes().partition', 'partition(sep, /)', ['sep'], ge, (3, 6)), |
| 28 |
] |
| 29 |
) |
| 30 |
def test_compiled_signature(Script, environment, code, sig, names, op, version): |
| 31 |
if not op(environment.version_info, version): |
| 32 |
return |
| 33 |
|
| 34 |
d, = Script(code).infer() |
| 35 |
value, = d._name.infer() |
| 36 |
compiled, = _stub_to_python_value_set(value) |
| 37 |
signature, = compiled.get_signatures() |
| 38 |
assert signature.to_string() == sig |
| 39 |
assert [n.string_name for n in signature.get_param_names()] == names |
| 40 |
|
| 41 |
|
| 42 |
classmethod_code = ''' |
| 43 |
class X: |
| 44 |
@classmethod |
| 45 |
def x(cls, a, b): |
| 46 |
pass |
| 47 |
|
| 48 |
@staticmethod |
| 49 |
def static(a, b): |
| 50 |
pass |
| 51 |
''' |
| 52 |
|
| 53 |
|
| 54 |
partial_code = ''' |
| 55 |
import functools |
| 56 |
|
| 57 |
def func(a, b, c): |
| 58 |
pass |
| 59 |
|
| 60 |
a = functools.partial(func) |
| 61 |
b = functools.partial(func, 1) |
| 62 |
c = functools.partial(func, 1, c=2) |
| 63 |
d = functools.partial() |
| 64 |
''' |
| 65 |
|
| 66 |
|
| 67 |
partialmethod_code = ''' |
| 68 |
import functools |
| 69 |
|
| 70 |
class X: |
| 71 |
def func(self, a, b, c): |
| 72 |
pass |
| 73 |
a = functools.partialmethod(func) |
| 74 |
b = functools.partialmethod(func, 1) |
| 75 |
c = functools.partialmethod(func, 1, c=2) |
| 76 |
d = functools.partialmethod() |
| 77 |
''' |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.parametrize( |
| 81 |
'code, expected', [ |
| 82 |
('def f(a, * args, x): pass\n f(', 'f(a, *args, x)'), |
| 83 |
('def f(a, *, x): pass\n f(', 'f(a, *, x)'), |
| 84 |
('def f(*, x= 3,**kwargs): pass\n f(', 'f(*, x=3, **kwargs)'), |
| 85 |
('def f(x,/,y,* ,z): pass\n f(', 'f(x, /, y, *, z)'), |
| 86 |
('def f(a, /, *, x=3, **kwargs): pass\n f(', 'f(a, /, *, x=3, **kwargs)'), |
| 87 |
|
| 88 |
(classmethod_code + 'X.x(', 'x(a, b)'), |
| 89 |
(classmethod_code + 'X().x(', 'x(a, b)'), |
| 90 |
(classmethod_code + 'X.static(', 'static(a, b)'), |
| 91 |
(classmethod_code + 'X().static(', 'static(a, b)'), |
| 92 |
|
| 93 |
(partial_code + 'a(', 'func(a, b, c)'), |
| 94 |
(partial_code + 'b(', 'func(b, c)'), |
| 95 |
(partial_code + 'c(', 'func(b)'), |
| 96 |
(partial_code + 'd(', None), |
| 97 |
|
| 98 |
(partialmethod_code + 'X().a(', 'func(a, b, c)'), |
| 99 |
(partialmethod_code + 'X().b(', 'func(b, c)'), |
| 100 |
(partialmethod_code + 'X().c(', 'func(b)'), |
| 101 |
(partialmethod_code + 'X().d(', None), |
| 102 |
(partialmethod_code + 'X.c(', 'func(a, b)'), |
| 103 |
(partialmethod_code + 'X.d(', None), |
| 104 |
|
| 105 |
('import contextlib\n@contextlib.contextmanager\ndef f(x): pass\nf(', 'f(x)'), |
| 106 |
|
| 107 |
|
| 108 |
('from typing import cast\ncast(', { |
| 109 |
'cast(typ: object, val: Any) -> Any', |
| 110 |
'cast(typ: str, val: Any) -> Any', |
| 111 |
'cast(typ: type[_T], val: Any) -> _T'}), |
| 112 |
('from typing import TypeVar\nTypeVar(', |
| 113 |
'TypeVar(name: str, *constraints: Any, bound: Any | None=None, covariant: bool=False, ' |
| 114 |
'contravariant: bool=False)'), |
| 115 |
('from typing import List\nList(', None), |
| 116 |
('from typing import List\nList[int](', None), |
| 117 |
('from typing import Tuple\nTuple(', None), |
| 118 |
('from typing import Tuple\nTuple[int](', None), |
| 119 |
('from typing import Optional\nOptional(', None), |
| 120 |
('from typing import Optional\nOptional[int](', None), |
| 121 |
('from typing import Any\nAny(', None), |
| 122 |
('from typing import NewType\nNewType(', 'NewType(name: str, tp: Any)'), |
| 123 |
] |
| 124 |
) |
| 125 |
def test_tree_signature(Script, environment, code, expected): |
| 126 |
if expected is None: |
| 127 |
assert not Script(code).get_signatures() |
| 128 |
else: |
| 129 |
actual = {sig.to_string() for sig in Script(code).get_signatures()} |
| 130 |
if not isinstance(expected, set): |
| 131 |
expected = {expected} |
| 132 |
assert expected == actual |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.parametrize( |
| 136 |
'combination, expected', [ |
| 137 |
|
| 138 |
('full_redirect(simple)', 'b, *, c'), |
| 139 |
('full_redirect(simple4)', 'b, x: int'), |
| 140 |
('full_redirect(a)', 'b, *args'), |
| 141 |
('full_redirect(kw)', 'b, *, c, **kwargs'), |
| 142 |
('full_redirect(akw)', 'c, *args, **kwargs'), |
| 143 |
|
| 144 |
|
| 145 |
('full_redirect(lambda x, y: ...)', 'y'), |
| 146 |
('full_redirect()', '*args, **kwargs'), |
| 147 |
('full_redirect(1)', '*args, **kwargs'), |
| 148 |
|
| 149 |
|
| 150 |
('full_redirect(C)', 'z, *, c'), |
| 151 |
('full_redirect(C())', 'y'), |
| 152 |
('full_redirect(G)', 't: T'), |
| 153 |
('full_redirect(G[str])', '*args, **kwargs'), |
| 154 |
('D', 'D(a, z, /)'), |
| 155 |
('D()', 'D(x, y)'), |
| 156 |
('D().foo', 'foo(a, *, bar, z, **kwargs)'), |
| 157 |
|
| 158 |
|
| 159 |
('two_redirects(simple, simple)', 'a, b, *, c'), |
| 160 |
('two_redirects(simple2, simple2)', 'x'), |
| 161 |
('two_redirects(akw, kw)', 'a, c, *args, **kwargs'), |
| 162 |
('two_redirects(kw, akw)', 'a, b, *args, c, **kwargs'), |
| 163 |
|
| 164 |
('two_kwargs_redirects(simple, simple)', '*args, a, b, c'), |
| 165 |
('two_kwargs_redirects(kw, kw)', '*args, a, b, c, **kwargs'), |
| 166 |
('two_kwargs_redirects(simple, kw)', '*args, a, b, c, **kwargs'), |
| 167 |
('two_kwargs_redirects(simple2, two_kwargs_redirects(simple, simple))', |
| 168 |
'*args, x, a, b, c'), |
| 169 |
|
| 170 |
('combined_redirect(simple, simple2)', 'a, b, /, *, x'), |
| 171 |
('combined_redirect(simple, simple3)', 'a, b, /, *, a, x: int'), |
| 172 |
('combined_redirect(simple2, simple)', 'x, /, *, a, b, c'), |
| 173 |
('combined_redirect(simple3, simple)', 'a, x: int, /, *, a, b, c'), |
| 174 |
|
| 175 |
('combined_redirect(simple, kw)', 'a, b, /, *, a, b, c, **kwargs'), |
| 176 |
('combined_redirect(kw, simple)', 'a, b, /, *, a, b, c'), |
| 177 |
('combined_redirect(simple, simple2)', 'a, b, /, *, x'), |
| 178 |
|
| 179 |
('combined_lot_of_args(kw, simple4)', '*, b'), |
| 180 |
('combined_lot_of_args(simple4, kw)', '*, b, c, **kwargs'), |
| 181 |
|
| 182 |
('combined_redirect(combined_redirect(simple2, simple4), combined_redirect(kw, simple5))', |
| 183 |
'x, /, *, y'), |
| 184 |
('combined_redirect(combined_redirect(simple4, simple2), combined_redirect(simple5, kw))', |
| 185 |
'a, b, x: int, /, *, a, b, c, **kwargs'), |
| 186 |
('combined_redirect(combined_redirect(a, kw), combined_redirect(kw, simple5))', |
| 187 |
'a, b, /, *args, y'), |
| 188 |
|
| 189 |
('no_redirect(kw)', '*args, **kwargs'), |
| 190 |
('no_redirect(akw)', '*args, **kwargs'), |
| 191 |
('no_redirect(simple)', '*args, **kwargs'), |
| 192 |
] |
| 193 |
) |
| 194 |
def test_nested_signatures(Script, environment, combination, expected): |
| 195 |
code = dedent(''' |
| 196 |
def simple(a, b, *, c): ... |
| 197 |
def simple2(x): ... |
| 198 |
def simple3(a, x: int): ... |
| 199 |
def simple4(a, b, x: int): ... |
| 200 |
def simple5(y): ... |
| 201 |
def a(a, b, *args): ... |
| 202 |
def kw(a, b, *, c, **kwargs): ... |
| 203 |
def akw(a, c, *args, **kwargs): ... |
| 204 |
|
| 205 |
def no_redirect(func): |
| 206 |
return lambda *args, **kwargs: func(1) |
| 207 |
def full_redirect(func): |
| 208 |
return lambda *args, **kwargs: func(1, *args, **kwargs) |
| 209 |
def two_redirects(func1, func2): |
| 210 |
return lambda *args, **kwargs: func1(*args, **kwargs) + func2(1, *args, **kwargs) |
| 211 |
def two_kwargs_redirects(func1, func2): |
| 212 |
return lambda *args, **kwargs: func1(**kwargs) + func2(1, **kwargs) |
| 213 |
def combined_redirect(func1, func2): |
| 214 |
return lambda *args, **kwargs: func1(*args) + func2(**kwargs) |
| 215 |
def combined_lot_of_args(func1, func2): |
| 216 |
return lambda *args, **kwargs: func1(1, 2, 3, 4, *args) + func2(a=3, x=1, y=1, **kwargs) |
| 217 |
|
| 218 |
class C: |
| 219 |
def __init__(self, a, z, *, c): ... |
| 220 |
def __call__(self, x, y): ... |
| 221 |
|
| 222 |
def foo(self, bar, z, **kwargs): ... |
| 223 |
|
| 224 |
class D(C): |
| 225 |
def __init__(self, *args): |
| 226 |
super().__init__(*args) |
| 227 |
|
| 228 |
def foo(self, a, **kwargs): |
| 229 |
super().foo(**kwargs) |
| 230 |
|
| 231 |
from typing import Generic, TypeVar |
| 232 |
T = TypeVar('T') |
| 233 |
class G(Generic[T]): |
| 234 |
def __init__(self, i, t: T): ... |
| 235 |
''') |
| 236 |
code += 'z = ' + combination + '\nz(' |
| 237 |
sig, = Script(code).get_signatures() |
| 238 |
computed = sig.to_string() |
| 239 |
if not re.match(r'\w+\(', expected): |
| 240 |
expected = '<lambda>(' + expected + ')' |
| 241 |
assert expected == computed |
| 242 |
|
| 243 |
|
| 244 |
def test_pow_signature(Script, environment): |
| 245 |
|
| 246 |
sigs = Script('pow(').get_signatures() |
| 247 |
strings = {sig.to_string() for sig in sigs} |
| 248 |
assert 'pow(base: _PositiveInteger, exp: float, mod: None=None) -> float' in strings |
| 249 |
assert len(strings) > 4 |
| 250 |
|
| 251 |
|
| 252 |
@pytest.mark.parametrize( |
| 253 |
'code, signature', [ |
| 254 |
[dedent(''' |
| 255 |
# identifier:A |
| 256 |
import functools |
| 257 |
def f(x): |
| 258 |
pass |
| 259 |
def x(f): |
| 260 |
@functools.wraps(f) |
| 261 |
def wrapper(*args): |
| 262 |
return f(*args) |
| 263 |
return wrapper |
| 264 |
|
| 265 |
x(f)('''), 'f(x, /)'], |
| 266 |
[dedent(''' |
| 267 |
# identifier:B |
| 268 |
import functools |
| 269 |
def f(x): |
| 270 |
pass |
| 271 |
def x(f): |
| 272 |
@functools.wraps(f) |
| 273 |
def wrapper(): |
| 274 |
# Have no arguments here, but because of wraps, the signature |
| 275 |
# should still be f's. |
| 276 |
return 1 |
| 277 |
return wrapper |
| 278 |
|
| 279 |
x(f)('''), 'f()'], |
| 280 |
[dedent(''' |
| 281 |
# identifier:C |
| 282 |
import functools |
| 283 |
def f(x: int, y: float): |
| 284 |
pass |
| 285 |
|
| 286 |
@functools.wraps(f) |
| 287 |
def wrapper(*args, **kwargs): |
| 288 |
return f(*args, **kwargs) |
| 289 |
|
| 290 |
wrapper('''), 'f(x: int, y: float)'], |
| 291 |
[dedent(''' |
| 292 |
# identifier:D |
| 293 |
def f(x: int, y: float): |
| 294 |
pass |
| 295 |
|
| 296 |
def wrapper(*args, **kwargs): |
| 297 |
return f(*args, **kwargs) |
| 298 |
|
| 299 |
wrapper('''), 'wrapper(x: int, y: float)'], |
| 300 |
] |
| 301 |
) |
| 302 |
def test_wraps_signature(Script, code, signature): |
| 303 |
sigs = Script(code).get_signatures() |
| 304 |
assert {sig.to_string() for sig in sigs} == {signature} |
| 305 |
|
| 306 |
|
| 307 |
@pytest.mark.parametrize( |
| 308 |
"start, start_params, include_params", |
| 309 |
[ |
| 310 |
["@dataclass\nclass X:", [], True], |
| 311 |
["@dataclass(eq=True)\nclass X:", [], True], |
| 312 |
[ |
| 313 |
dedent( |
| 314 |
""" |
| 315 |
class Y(): |
| 316 |
y: int |
| 317 |
@dataclass |
| 318 |
class X(Y):""" |
| 319 |
), |
| 320 |
[], |
| 321 |
True, |
| 322 |
], |
| 323 |
[ |
| 324 |
dedent( |
| 325 |
""" |
| 326 |
@dataclass |
| 327 |
class Y(): |
| 328 |
y: int |
| 329 |
z = 5 |
| 330 |
@dataclass |
| 331 |
class X(Y):""" |
| 332 |
), |
| 333 |
["y"], |
| 334 |
True, |
| 335 |
], |
| 336 |
[ |
| 337 |
dedent( |
| 338 |
""" |
| 339 |
@dataclass |
| 340 |
class Y(): |
| 341 |
y: int |
| 342 |
class Z(Y): # Not included |
| 343 |
z = 5 |
| 344 |
@dataclass |
| 345 |
class X(Z):""" |
| 346 |
), |
| 347 |
["y"], |
| 348 |
True, |
| 349 |
], |
| 350 |
|
| 351 |
[ |
| 352 |
dedent( |
| 353 |
""" |
| 354 |
@dataclass(init=False) |
| 355 |
class X:""" |
| 356 |
), |
| 357 |
[], |
| 358 |
False, |
| 359 |
], |
| 360 |
[ |
| 361 |
dedent( |
| 362 |
""" |
| 363 |
@dataclass(eq=True, init=False) |
| 364 |
class X:""" |
| 365 |
), |
| 366 |
[], |
| 367 |
False, |
| 368 |
], |
| 369 |
|
| 370 |
[ |
| 371 |
dedent( |
| 372 |
""" |
| 373 |
@dataclass() |
| 374 |
class X: |
| 375 |
def __init__(self, toto: str): |
| 376 |
pass |
| 377 |
""" |
| 378 |
), |
| 379 |
["toto"], |
| 380 |
False, |
| 381 |
], |
| 382 |
], |
| 383 |
ids=[ |
| 384 |
"direct_transformed", |
| 385 |
"transformed_with_params", |
| 386 |
"subclass_transformed", |
| 387 |
"both_transformed", |
| 388 |
"intermediate_not_transformed", |
| 389 |
"init_false", |
| 390 |
"init_false_multiple", |
| 391 |
"custom_init", |
| 392 |
], |
| 393 |
) |
| 394 |
def test_dataclass_signature( |
| 395 |
Script, start, start_params, include_params, environment |
| 396 |
): |
| 397 |
price_type = "Final[float]" |
| 398 |
price_type_infer = "_SpecialForm" |
| 399 |
|
| 400 |
code = dedent( |
| 401 |
f""" |
| 402 |
name: str |
| 403 |
foo = 3 |
| 404 |
blob: ClassVar[str] |
| 405 |
price: {price_type} |
| 406 |
quantity: int = 0.0 |
| 407 |
|
| 408 |
X(""" |
| 409 |
) |
| 410 |
|
| 411 |
code = ( |
| 412 |
"from dataclasses import dataclass\n" |
| 413 |
+ "from typing import ClassVar, Final\n" |
| 414 |
+ start |
| 415 |
+ code |
| 416 |
) |
| 417 |
|
| 418 |
sig, = Script(code).get_signatures() |
| 419 |
expected_params = ( |
| 420 |
[*start_params, "name", "price", "quantity"] |
| 421 |
if include_params |
| 422 |
else [*start_params] |
| 423 |
) |
| 424 |
assert [p.name for p in sig.params] == expected_params |
| 425 |
|
| 426 |
if include_params: |
| 427 |
quantity, = sig.params[-1].infer() |
| 428 |
assert quantity.name == 'int' |
| 429 |
price, = sig.params[-2].infer() |
| 430 |
assert price.name == price_type_infer |
| 431 |
|
| 432 |
|
| 433 |
dataclass_transform_cases: list[Any] = [ |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
['@dataclass_transform\nclass X:', [], False], |
| 439 |
|
| 440 |
['@dataclass_transform(eq_default=True)\nclass X:', [], False], |
| 441 |
|
| 442 |
[dedent(''' |
| 443 |
class Y(): |
| 444 |
y: int |
| 445 |
@dataclass_transform |
| 446 |
class X(Y):'''), [], False], |
| 447 |
|
| 448 |
|
| 449 |
[dedent(''' |
| 450 |
@dataclass_transform |
| 451 |
class Y(): |
| 452 |
y: int |
| 453 |
z = 5 |
| 454 |
class X(Y):'''), [], True], |
| 455 |
|
| 456 |
[dedent(''' |
| 457 |
@dataclass_transform(eq_default=True) |
| 458 |
class Y(): |
| 459 |
y: int |
| 460 |
z = 5 |
| 461 |
class X(Y):'''), [], True], |
| 462 |
|
| 463 |
[dedent(''' |
| 464 |
@dataclass_transform |
| 465 |
def create_model(): |
| 466 |
pass |
| 467 |
@create_model |
| 468 |
class X:'''), [], True], |
| 469 |
[dedent(''' |
| 470 |
@dataclass_transform |
| 471 |
def create_model(): |
| 472 |
pass |
| 473 |
class Y: |
| 474 |
y: int |
| 475 |
@create_model |
| 476 |
class X(Y):'''), [], True], |
| 477 |
[dedent(''' |
| 478 |
@dataclass_transform |
| 479 |
def create_model(): |
| 480 |
pass |
| 481 |
@create_model |
| 482 |
class Y: |
| 483 |
y: int |
| 484 |
@create_model |
| 485 |
class X(Y):'''), ["y"], True], |
| 486 |
[dedent(''' |
| 487 |
@dataclass_transform |
| 488 |
def create_model(): |
| 489 |
pass |
| 490 |
@create_model |
| 491 |
class Y: |
| 492 |
y: int |
| 493 |
class Z(Y): |
| 494 |
z: int |
| 495 |
@create_model |
| 496 |
class X(Z):'''), ["y"], True], |
| 497 |
|
| 498 |
[dedent(''' |
| 499 |
@dataclass_transform |
| 500 |
class ModelMeta(): |
| 501 |
y: int |
| 502 |
z = 5 |
| 503 |
class ModelBase(metaclass=ModelMeta): |
| 504 |
t: int |
| 505 |
p = 5 |
| 506 |
class X(ModelBase):'''), [], True], |
| 507 |
|
| 508 |
[dedent(''' |
| 509 |
@dataclass_transform() |
| 510 |
class Y(): |
| 511 |
y: int |
| 512 |
z = 5 |
| 513 |
class X(Y): |
| 514 |
def __init__(self, toto: str): |
| 515 |
pass |
| 516 |
'''), ["toto"], False], |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
[dedent(''' |
| 532 |
@dataclass_transform |
| 533 |
class Y(): |
| 534 |
y: int |
| 535 |
z = 5 |
| 536 |
def __init_subclass__( |
| 537 |
cls, |
| 538 |
*, |
| 539 |
init: bool = False, |
| 540 |
) |
| 541 |
class X(Y, init=True):'''), [], True], |
| 542 |
[dedent(''' |
| 543 |
@dataclass_transform |
| 544 |
class Y(): |
| 545 |
y: int |
| 546 |
z = 5 |
| 547 |
def __init_subclass__( |
| 548 |
cls, |
| 549 |
*, |
| 550 |
init: bool = False, |
| 551 |
) |
| 552 |
class X(Y, init=False):'''), [], False], |
| 553 |
[dedent(''' |
| 554 |
@dataclass_transform |
| 555 |
class Y(): |
| 556 |
y: int |
| 557 |
z = 5 |
| 558 |
class X(Y, init=False):'''), [], False], |
| 559 |
|
| 560 |
[dedent(''' |
| 561 |
@dataclass_transform |
| 562 |
def create_model(init=False): |
| 563 |
pass |
| 564 |
@create_model() |
| 565 |
class X:'''), [], False], |
| 566 |
[dedent(''' |
| 567 |
@dataclass_transform |
| 568 |
def create_model(init=False): |
| 569 |
pass |
| 570 |
@create_model(init=True) |
| 571 |
class X:'''), [], True], |
| 572 |
[dedent(''' |
| 573 |
@dataclass_transform |
| 574 |
def create_model(init=False): |
| 575 |
pass |
| 576 |
@create_model(init=False) |
| 577 |
class X:'''), [], False], |
| 578 |
[dedent(''' |
| 579 |
@dataclass_transform |
| 580 |
def create_model(): |
| 581 |
pass |
| 582 |
@create_model(init=False) |
| 583 |
class X:'''), [], False], |
| 584 |
|
| 585 |
[dedent(''' |
| 586 |
@dataclass_transform |
| 587 |
class ModelMeta(): |
| 588 |
y: int |
| 589 |
z = 5 |
| 590 |
def __new__( |
| 591 |
cls, |
| 592 |
name, |
| 593 |
bases, |
| 594 |
namespace, |
| 595 |
*, |
| 596 |
init: bool = False, |
| 597 |
): |
| 598 |
... |
| 599 |
class ModelBase(metaclass=ModelMeta): |
| 600 |
t: int |
| 601 |
p = 5 |
| 602 |
class X(ModelBase):'''), [], False], |
| 603 |
[dedent(''' |
| 604 |
@dataclass_transform |
| 605 |
class ModelMeta(): |
| 606 |
y: int |
| 607 |
z = 5 |
| 608 |
def __new__( |
| 609 |
cls, |
| 610 |
name, |
| 611 |
bases, |
| 612 |
namespace, |
| 613 |
*, |
| 614 |
init: bool = False, |
| 615 |
): |
| 616 |
... |
| 617 |
class ModelBase(metaclass=ModelMeta): |
| 618 |
t: int |
| 619 |
p = 5 |
| 620 |
class X(ModelBase, init=True):'''), [], True], |
| 621 |
[dedent(''' |
| 622 |
@dataclass_transform |
| 623 |
class ModelMeta(): |
| 624 |
y: int |
| 625 |
z = 5 |
| 626 |
def __new__( |
| 627 |
cls, |
| 628 |
name, |
| 629 |
bases, |
| 630 |
namespace, |
| 631 |
*, |
| 632 |
init: bool = False, |
| 633 |
): |
| 634 |
... |
| 635 |
class ModelBase(metaclass=ModelMeta): |
| 636 |
t: int |
| 637 |
p = 5 |
| 638 |
class X(ModelBase, init=False):'''), [], False], |
| 639 |
[dedent(''' |
| 640 |
@dataclass_transform |
| 641 |
class ModelMeta(): |
| 642 |
y: int |
| 643 |
z = 5 |
| 644 |
class ModelBase(metaclass=ModelMeta): |
| 645 |
t: int |
| 646 |
p = 5 |
| 647 |
class X(ModelBase, init=False):'''), [], False], |
| 648 |
|
| 649 |
|
| 650 |
[dedent(''' |
| 651 |
@dataclass_transform |
| 652 |
class Y(): |
| 653 |
y: int |
| 654 |
z = 5 |
| 655 |
class X(Y, eq=True):'''), [], True], |
| 656 |
|
| 657 |
[dedent(''' |
| 658 |
@dataclass_transform |
| 659 |
def create_model(): |
| 660 |
pass |
| 661 |
@create_model(eq=True) |
| 662 |
class X:'''), [], True], |
| 663 |
|
| 664 |
[dedent(''' |
| 665 |
@dataclass_transform |
| 666 |
class ModelMeta(): |
| 667 |
y: int |
| 668 |
z = 5 |
| 669 |
class ModelBase(metaclass=ModelMeta): |
| 670 |
t: int |
| 671 |
p = 5 |
| 672 |
class X(ModelBase, eq=True):'''), [], True], |
| 673 |
] |
| 674 |
|
| 675 |
ids = [ |
| 676 |
"direct_transformer", |
| 677 |
"transformer_with_params", |
| 678 |
"subclass_transformer", |
| 679 |
"base_transformed", |
| 680 |
"base_transformed_with_params", |
| 681 |
"decorator_transformed_direct", |
| 682 |
"decorator_transformed_subclass", |
| 683 |
"decorator_transformed_both", |
| 684 |
"decorator_transformed_intermediate_not", |
| 685 |
"metaclass_transformed", |
| 686 |
"custom_init", |
| 687 |
|
| 688 |
"base_transformed_init_false_dataclass_init_true", |
| 689 |
"base_transformed_init_false_dataclass_init_false", |
| 690 |
"base_transformed_init_default_dataclass_init_false", |
| 691 |
"decorator_transformed_init_false_dataclass_init_default", |
| 692 |
"decorator_transformed_init_false_dataclass_init_true", |
| 693 |
"decorator_transformed_init_false_dataclass_init_false", |
| 694 |
"decorator_transformed_init_default_dataclass_init_false", |
| 695 |
"metaclass_transformed_init_false_dataclass_init_default", |
| 696 |
"metaclass_transformed_init_false_dataclass_init_true", |
| 697 |
"metaclass_transformed_init_false_dataclass_init_false", |
| 698 |
"metaclass_transformed_init_default_dataclass_init_false", |
| 699 |
"base_transformed_other_parameters", |
| 700 |
"decorator_transformed_other_parameters", |
| 701 |
"metaclass_transformed_other_parameters", |
| 702 |
] |
| 703 |
|
| 704 |
|
| 705 |
@pytest.mark.parametrize( |
| 706 |
'start, start_params, include_params', dataclass_transform_cases, ids=ids |
| 707 |
) |
| 708 |
def test_extensions_dataclass_transform_signature( |
| 709 |
Script, start, start_params, include_params, environment |
| 710 |
): |
| 711 |
has_typing_ext = bool(Script('import typing_extensions').infer()) |
| 712 |
if not has_typing_ext: |
| 713 |
raise pytest.skip("typing_extensions needed in target environment to run this test") |
| 714 |
|
| 715 |
price_type = "Final[float]" |
| 716 |
price_type_infer = "_SpecialForm" |
| 717 |
|
| 718 |
code = dedent( |
| 719 |
f""" |
| 720 |
name: str |
| 721 |
foo = 3 |
| 722 |
blob: ClassVar[str] |
| 723 |
price: {price_type} |
| 724 |
quantity: int = 0.0 |
| 725 |
|
| 726 |
X(""" |
| 727 |
) |
| 728 |
|
| 729 |
code = ( |
| 730 |
"from typing_extensions import dataclass_transform\n" |
| 731 |
+ "from typing import ClassVar, Final\n" |
| 732 |
+ start |
| 733 |
+ code |
| 734 |
) |
| 735 |
|
| 736 |
(sig,) = Script(code).get_signatures() |
| 737 |
expected_params = ( |
| 738 |
[*start_params, "name", "price", "quantity"] |
| 739 |
if include_params |
| 740 |
else [*start_params] |
| 741 |
) |
| 742 |
assert [p.name for p in sig.params] == expected_params |
| 743 |
|
| 744 |
if include_params: |
| 745 |
quantity, = sig.params[-1].infer() |
| 746 |
assert quantity.name == 'int' |
| 747 |
price, = sig.params[-2].infer() |
| 748 |
assert price.name == price_type_infer |
| 749 |
|
| 750 |
|
| 751 |
def test_dataclass_transform_complete(Script): |
| 752 |
script = Script('''\ |
| 753 |
@dataclass_transform |
| 754 |
class Y(): |
| 755 |
y: int |
| 756 |
z = 5 |
| 757 |
|
| 758 |
class X(Y): |
| 759 |
name: str |
| 760 |
foo = 3 |
| 761 |
|
| 762 |
def f(x: X): |
| 763 |
x.na''') |
| 764 |
completion, = script.complete() |
| 765 |
assert completion.description == 'name: str' |
| 766 |
|
| 767 |
|
| 768 |
@pytest.mark.parametrize( |
| 769 |
"start, start_params, include_params", dataclass_transform_cases, ids=ids |
| 770 |
) |
| 771 |
def test_dataclass_transform_signature( |
| 772 |
Script, skip_pre_python311, start, start_params, include_params |
| 773 |
): |
| 774 |
code = dedent(''' |
| 775 |
name: str |
| 776 |
foo = 3 |
| 777 |
blob: ClassVar[str] |
| 778 |
price: Final[float] |
| 779 |
quantity: int = 0.0 |
| 780 |
|
| 781 |
X(''') |
| 782 |
|
| 783 |
code = ( |
| 784 |
"from typing import dataclass_transform\n" |
| 785 |
+ "from typing import ClassVar, Final\n" |
| 786 |
+ start |
| 787 |
+ code |
| 788 |
) |
| 789 |
|
| 790 |
sig, = Script(code).get_signatures() |
| 791 |
expected_params = ( |
| 792 |
[*start_params, "name", "price", "quantity"] |
| 793 |
if include_params |
| 794 |
else [*start_params] |
| 795 |
) |
| 796 |
assert [p.name for p in sig.params] == expected_params |
| 797 |
|
| 798 |
if include_params: |
| 799 |
quantity, = sig.params[-1].infer() |
| 800 |
assert quantity.name == 'int' |
| 801 |
price, = sig.params[-2].infer() |
| 802 |
assert price.name == '_SpecialForm' |
| 803 |
|
| 804 |
|
| 805 |
@pytest.mark.parametrize( |
| 806 |
'start, start_params', [ |
| 807 |
['@define\nclass X:', []], |
| 808 |
['@frozen\nclass X:', []], |
| 809 |
['@define(eq=True)\nclass X:', []], |
| 810 |
[dedent(''' |
| 811 |
class Y(): |
| 812 |
y: int |
| 813 |
@define |
| 814 |
class X(Y):'''), []], |
| 815 |
[dedent(''' |
| 816 |
@define |
| 817 |
class Y(): |
| 818 |
y: int |
| 819 |
z = 5 |
| 820 |
@define |
| 821 |
class X(Y):'''), ['y']], |
| 822 |
], |
| 823 |
ids=["define", "frozen", "define_customized", "define_subclass", "define_both"] |
| 824 |
) |
| 825 |
def test_attrs_signature(Script, start, start_params): |
| 826 |
has_attrs = bool(Script('import attrs').infer()) |
| 827 |
if not has_attrs: |
| 828 |
raise pytest.skip("attrs needed in target environment to run this test") |
| 829 |
|
| 830 |
code = dedent(''' |
| 831 |
name: str |
| 832 |
foo = 3 |
| 833 |
price: float |
| 834 |
quantity: int = 0.0 |
| 835 |
|
| 836 |
X(''') |
| 837 |
|
| 838 |
|
| 839 |
code = 'from attrs import define, frozen\n' + start + code |
| 840 |
|
| 841 |
sig, = Script(code).get_signatures() |
| 842 |
assert [p.name for p in sig.params] == start_params + ['name', 'price', 'quantity'] |
| 843 |
quantity, = sig.params[-1].infer() |
| 844 |
assert quantity.name == 'int' |
| 845 |
price, = sig.params[-2].infer() |
| 846 |
assert price.name == 'float' |
| 847 |
|
| 848 |
|
| 849 |
@pytest.mark.parametrize( |
| 850 |
'stmt, expected', [ |
| 851 |
('args = 1', 'wrapped(*args, b, c)'), |
| 852 |
('args = (1,)', 'wrapped(*args, c)'), |
| 853 |
('kwargs = 1', 'wrapped(b, /, **kwargs)'), |
| 854 |
('kwargs = dict(b=3)', 'wrapped(b, /, **kwargs)'), |
| 855 |
] |
| 856 |
) |
| 857 |
def test_param_resolving_to_static(Script, stmt, expected): |
| 858 |
code = dedent('''\ |
| 859 |
def full_redirect(func): |
| 860 |
def wrapped(*args, **kwargs): |
| 861 |
{stmt} |
| 862 |
return func(1, *args, **kwargs) |
| 863 |
return wrapped |
| 864 |
def simple(a, b, *, c): ... |
| 865 |
full_redirect(simple)('''.format(stmt=stmt)) |
| 866 |
|
| 867 |
sig, = Script(code).get_signatures() |
| 868 |
assert sig.to_string() == expected |
| 869 |
|
| 870 |
|
| 871 |
@pytest.mark.parametrize( |
| 872 |
'code', [ |
| 873 |
'from file import with_overload; with_overload(', |
| 874 |
'from file import *\nwith_overload(', |
| 875 |
] |
| 876 |
) |
| 877 |
def test_overload(Script, code): |
| 878 |
dir_ = get_example_dir('typing_overload') |
| 879 |
x1, x2 = Script(code, path=os.path.join(dir_, 'foo.py')).get_signatures() |
| 880 |
assert x1.to_string() == 'with_overload(x: int, y: int) -> float' |
| 881 |
assert x2.to_string() == 'with_overload(x: str, y: list) -> float' |
| 882 |
|
| 883 |
|
| 884 |
def test_enum(Script): |
| 885 |
script = Script('''\ |
| 886 |
from enum import Enum |
| 887 |
|
| 888 |
class Planet(Enum): |
| 889 |
MERCURY = (3.303e+23, 2.4397e6) |
| 890 |
VENUS = (4.869e+24, 6.0518e6) |
| 891 |
|
| 892 |
def __init__(self, mass, radius): |
| 893 |
self.mass = mass # in kilograms |
| 894 |
self.radius = radius # in meters |
| 895 |
|
| 896 |
Planet.MERCURY''') |
| 897 |
completion, = script.complete() |
| 898 |
assert not completion.get_signatures() |