Checking that pandas-specific warnings are used for deprecations. Parameters ---------- file_obj : IO File-like object containing the Python code to validate. Yields ------ line_number : int Line number of the warning. msg : str Explanation
(file_obj: IO[str])
| 348 | |
| 349 | |
| 350 | def doesnt_use_pandas_warnings(file_obj: IO[str]) -> Iterable[tuple[int, str]]: |
| 351 | """ |
| 352 | Checking that pandas-specific warnings are used for deprecations. |
| 353 | |
| 354 | Parameters |
| 355 | ---------- |
| 356 | file_obj : IO |
| 357 | File-like object containing the Python code to validate. |
| 358 | |
| 359 | Yields |
| 360 | ------ |
| 361 | line_number : int |
| 362 | Line number of the warning. |
| 363 | msg : str |
| 364 | Explanation of the error. |
| 365 | """ |
| 366 | contents = file_obj.read() |
| 367 | lines = contents.split("\n") |
| 368 | tree = ast.parse(contents) |
| 369 | for node in ast.walk(tree): |
| 370 | if not isinstance(node, ast.Call): |
| 371 | continue |
| 372 | |
| 373 | if isinstance(node.func, ast.Attribute) and isinstance( |
| 374 | node.func.value, ast.Name |
| 375 | ): |
| 376 | # Check for `warnings.warn`. |
| 377 | if node.func.value.id != "warnings" or node.func.attr != "warn": |
| 378 | continue |
| 379 | elif isinstance(node.func, ast.Name): |
| 380 | # Check for just `warn` when using `from warnings import warn`. |
| 381 | if node.func.id != "warn": |
| 382 | continue |
| 383 | if any( |
| 384 | "# pdlint: ignore[warning_class]" in lines[k] |
| 385 | for k in range(node.lineno - 1, node.end_lineno + 1) |
| 386 | ): |
| 387 | continue |
| 388 | values = [arg.id for arg in node.args if isinstance(arg, ast.Name)] + [ |
| 389 | kw.value.id for kw in node.keywords if kw.arg == "category" |
| 390 | ] |
| 391 | for value in values: |
| 392 | matches = re.match(DEPRECATION_WARNINGS_PATTERN, value) |
| 393 | if matches is not None: |
| 394 | yield ( |
| 395 | node.lineno, |
| 396 | f"Don't use {matches[0]}, use a pandas-specific warning in " |
| 397 | f"pd.errors instead. You can add " |
| 398 | f"`# pdlint: ignore[warning_class]` to override.", |
| 399 | ) |
| 400 | |
| 401 | |
| 402 | def main( |