MCPcopy Create free account
hub / github.com/pytest-dev/pytest / _idval

Function _idval

src/_pytest/python.py:1363–1403  ·  view source on GitHub ↗
(
    val: object,
    argname: str,
    idx: int,
    idfn: Optional[Callable[[Any], Optional[object]]],
    nodeid: Optional[str],
    config: Optional[Config],
)

Source from the content-addressed store, hash-verified

1361
1362
1363def _idval(
1364 val: object,
1365 argname: str,
1366 idx: int,
1367 idfn: Optional[Callable[[Any], Optional[object]]],
1368 nodeid: Optional[str],
1369 config: Optional[Config],
1370) -> str:
1371 if idfn:
1372 try:
1373 generated_id = idfn(val)
1374 if generated_id is not None:
1375 val = generated_id
1376 except Exception as e:
1377 prefix = f"{nodeid}: " if nodeid is not None else ""
1378 msg = "error raised while trying to determine id of parameter '{}' at position {}"
1379 msg = prefix + msg.format(argname, idx)
1380 raise ValueError(msg) from e
1381 elif config:
1382 hook_id: Optional[str] = config.hook.pytest_make_parametrize_id(
1383 config=config, val=val, argname=argname
1384 )
1385 if hook_id:
1386 return hook_id
1387
1388 if isinstance(val, STRING_TYPES):
1389 return _ascii_escaped_by_config(val, config)
1390 elif val is None or isinstance(val, (float, int, bool, complex)):
1391 return str(val)
1392 elif isinstance(val, Pattern):
1393 return ascii_escaped(val.pattern)
1394 elif val is NOTSET:
1395 # Fallback to default. Note that NOTSET is an enum.Enum.
1396 pass
1397 elif isinstance(val, enum.Enum):
1398 return str(val)
1399 elif isinstance(getattr(val, "__name__", None), str):
1400 # Name of a class, function, module, etc.
1401 name: str = getattr(val, "__name__")
1402 return name
1403 return str(argname) + str(idx)
1404
1405
1406def _idvalset(

Callers 7

test_idval_hypothesisMethod · 0.90
test_unicode_idvalMethod · 0.90
test_bytes_idvalMethod · 0.90
test_notset_idvalMethod · 0.90
_idvalsetFunction · 0.85

Calls 4

ascii_escapedFunction · 0.90
_ascii_escaped_by_configFunction · 0.85
formatMethod · 0.45

Tested by 6

test_idval_hypothesisMethod · 0.72
test_unicode_idvalMethod · 0.72
test_bytes_idvalMethod · 0.72
test_notset_idvalMethod · 0.72