Skip to content

Commit 5ca08fc

Browse files
authored
Type annotations on path-related unit tests (#128)
Originally part of #122, this PR adds type annotations to the path (unit) tests that didn't already have this, to ensure that Mypy validates the test bodies.
1 parent 29dd960 commit 5ca08fc

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

tests/unit/test_paths.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -421,194 +421,194 @@ def test_iterdir() -> None:
421421
}
422422

423423

424-
def test_exists_when_path_exists():
424+
def test_exists_when_path_exists() -> None:
425425
ws = create_autospec(WorkspaceClient)
426426
workspace_path = WorkspacePath(ws, "/test/path")
427427
ws.workspace.get_status.return_value = True
428428
assert workspace_path.exists()
429429

430430

431-
def test_exists_when_path_does_not_exist():
431+
def test_exists_when_path_does_not_exist() -> None:
432432
ws = create_autospec(WorkspaceClient)
433433
workspace_path = WorkspacePath(ws, "/test/path")
434434
ws.workspace.get_status.side_effect = NotFound("Simulated NotFound")
435435
assert not workspace_path.exists()
436436

437437

438-
def test_mkdir_creates_directory():
438+
def test_mkdir_creates_directory() -> None:
439439
ws = create_autospec(WorkspaceClient)
440440
workspace_path = WorkspacePath(ws, "/test/path")
441441
workspace_path.mkdir()
442442
ws.workspace.mkdirs.assert_called_once_with("/test/path")
443443

444444

445-
def test_rmdir_removes_directory():
445+
def test_rmdir_removes_directory() -> None:
446446
ws = create_autospec(WorkspaceClient)
447447
workspace_path = WorkspacePath(ws, "/test/path")
448448
workspace_path.rmdir()
449449
ws.workspace.delete.assert_called_once_with("/test/path", recursive=False)
450450

451451

452-
def test_is_dir_when_path_is_directory():
452+
def test_is_dir_when_path_is_directory() -> None:
453453
ws = create_autospec(WorkspaceClient)
454454
workspace_path = WorkspacePath(ws, "/test/path")
455455
ws.workspace.get_status.return_value = ObjectInfo(object_type=ObjectType.DIRECTORY)
456456
assert workspace_path.is_dir()
457457

458458

459-
def test_is_dir_when_path_is_not_directory():
459+
def test_is_dir_when_path_is_not_directory() -> None:
460460
ws = create_autospec(WorkspaceClient)
461461
workspace_path = WorkspacePath(ws, "/test/path")
462462
ws.workspace.get_status.return_value = ObjectInfo(object_type=ObjectType.FILE)
463463
assert not workspace_path.is_dir()
464464

465465

466-
def test_is_file_when_path_is_file():
466+
def test_is_file_when_path_is_file() -> None:
467467
ws = create_autospec(WorkspaceClient)
468468
workspace_path = WorkspacePath(ws, "/test/path")
469469
ws.workspace.get_status.return_value = ObjectInfo(object_type=ObjectType.FILE)
470470
assert workspace_path.is_file()
471471

472472

473-
def test_is_file_when_path_is_not_file():
473+
def test_is_file_when_path_is_not_file() -> None:
474474
ws = create_autospec(WorkspaceClient)
475475
workspace_path = WorkspacePath(ws, "/test/path")
476476
ws.workspace.get_status.return_value = ObjectInfo(object_type=ObjectType.DIRECTORY)
477477
assert not workspace_path.is_file()
478478

479479

480-
def test_is_notebook_when_path_is_notebook():
480+
def test_is_notebook_when_path_is_notebook() -> None:
481481
ws = create_autospec(WorkspaceClient)
482482
workspace_path = WorkspacePath(ws, "/test/path")
483483
ws.workspace.get_status.return_value = ObjectInfo(object_type=ObjectType.NOTEBOOK)
484484
assert workspace_path.is_notebook()
485485

486486

487-
def test_is_notebook_when_path_is_not_notebook():
487+
def test_is_notebook_when_path_is_not_notebook() -> None:
488488
ws = create_autospec(WorkspaceClient)
489489
workspace_path = WorkspacePath(ws, "/test/path")
490490
ws.workspace.get_status.return_value = ObjectInfo(object_type=ObjectType.FILE)
491491
assert not workspace_path.is_notebook()
492492

493493

494-
def test_open_file_in_read_binary_mode():
494+
def test_open_file_in_read_binary_mode() -> None:
495495
ws = create_autospec(WorkspaceClient)
496496
ws.workspace.download.return_value.__enter__.return_value.read.return_value = b"test"
497497
workspace_path = WorkspacePath(ws, "/test/path")
498498
assert workspace_path.read_bytes() == b"test"
499499

500500

501-
def test_open_file_in_write_binary_mode():
501+
def test_open_file_in_write_binary_mode() -> None:
502502
ws = create_autospec(WorkspaceClient)
503503
workspace_path = WorkspacePath(ws, "/test/path")
504504
workspace_path.write_bytes(b"test")
505505
ws.workspace.upload.assert_called_with("/test/path", b"test", format=ImportFormat.AUTO)
506506

507507

508-
def test_open_file_in_read_text_mode():
508+
def test_open_file_in_read_text_mode() -> None:
509509
ws = create_autospec(WorkspaceClient)
510510
workspace_path = WorkspacePath(ws, "/test/path")
511511
ws.workspace.download.return_value.__enter__.return_value.read.return_value = b"test"
512512
assert workspace_path.read_text() == "test"
513513

514514

515-
def test_open_file_in_write_text_mode():
515+
def test_open_file_in_write_text_mode() -> None:
516516
ws = create_autospec(WorkspaceClient)
517517
workspace_path = WorkspacePath(ws, "/test/path")
518518
workspace_path.write_text("test")
519519
ws.workspace.upload.assert_called_with("/test/path", "test", format=ImportFormat.AUTO)
520520

521521

522-
def test_open_file_in_invalid_mode():
522+
def test_open_file_in_invalid_mode() -> None:
523523
ws = create_autospec(WorkspaceClient)
524524
workspace_path = WorkspacePath(ws, "/test/path")
525525
with pytest.raises(ValueError):
526526
workspace_path.open(mode="invalid")
527527

528528

529-
def test_suffix_when_file_has_extension():
529+
def test_suffix_when_file_has_extension() -> None:
530530
ws = create_autospec(WorkspaceClient)
531531
workspace_path = WorkspacePath(ws, "/test/path.py")
532532
assert workspace_path.suffix == ".py"
533533

534534

535-
def test_suffix_when_file_is_notebook_and_language_matches():
535+
def test_suffix_when_file_is_notebook_and_language_matches() -> None:
536536
ws = create_autospec(WorkspaceClient)
537537
ws.workspace.get_status.return_value = ObjectInfo(language=Language.PYTHON, object_type=ObjectType.NOTEBOOK)
538538

539539
workspace_path = WorkspacePath(ws, "/test/path")
540540
assert workspace_path.suffix == ".py"
541541

542542

543-
def test_suffix_when_file_is_notebook_and_language_does_not_match():
543+
def test_suffix_when_file_is_notebook_and_language_does_not_match() -> None:
544544
ws = create_autospec(WorkspaceClient)
545545
ws.workspace.get_status.return_value = ObjectInfo(language=None, object_type=ObjectType.NOTEBOOK)
546546

547547
workspace_path = WorkspacePath(ws, "/test/path")
548548
assert workspace_path.suffix == ""
549549

550550

551-
def test_suffix_when_file_is_not_notebook():
551+
def test_suffix_when_file_is_not_notebook() -> None:
552552
ws = create_autospec(WorkspaceClient)
553553
ws.workspace.get_status.return_value = ObjectInfo(language=Language.PYTHON, object_type=ObjectType.FILE)
554554

555555
workspace_path = WorkspacePath(ws, "/test/path")
556556
assert workspace_path.suffix == ""
557557

558558

559-
def test_mkdir_creates_directory_with_valid_mode():
559+
def test_mkdir_creates_directory_with_valid_mode() -> None:
560560
ws = create_autospec(WorkspaceClient)
561561
workspace_path = WorkspacePath(ws, "/test/path")
562562
workspace_path.mkdir(mode=0o600)
563563
ws.workspace.mkdirs.assert_called_once_with("/test/path")
564564

565565

566-
def test_mkdir_raises_error_with_invalid_mode():
566+
def test_mkdir_raises_error_with_invalid_mode() -> None:
567567
ws = create_autospec(WorkspaceClient)
568568
workspace_path = WorkspacePath(ws, "/test/path")
569569
with pytest.raises(ValueError):
570570
workspace_path.mkdir(mode=0o700)
571571

572572

573-
def test_rmdir_removes_directory_non_recursive():
573+
def test_rmdir_removes_directory_non_recursive() -> None:
574574
ws = create_autospec(WorkspaceClient)
575575
workspace_path = WorkspacePath(ws, "/test/path")
576576
workspace_path.rmdir()
577577
ws.workspace.delete.assert_called_once_with("/test/path", recursive=False)
578578

579579

580-
def test_rmdir_removes_directory_recursive():
580+
def test_rmdir_removes_directory_recursive() -> None:
581581
ws = create_autospec(WorkspaceClient)
582582
workspace_path = WorkspacePath(ws, "/test/path")
583583
workspace_path.rmdir(recursive=True)
584584
ws.workspace.delete.assert_called_once_with("/test/path", recursive=True)
585585

586586

587-
def test_rename_file_without_overwrite():
587+
def test_rename_file_without_overwrite() -> None:
588588
ws = create_autospec(WorkspaceClient)
589589
workspace_path = WorkspacePath(ws, "/test/path")
590590
ws.workspace.download.return_value.__enter__.return_value.read.return_value = b"test"
591591
workspace_path.rename("/new/path")
592592
ws.workspace.upload.assert_called_once_with("/new/path", b"test", format=ImportFormat.AUTO, overwrite=False)
593593

594594

595-
def test_rename_file_with_overwrite():
595+
def test_rename_file_with_overwrite() -> None:
596596
ws = create_autospec(WorkspaceClient)
597597
workspace_path = WorkspacePath(ws, "/test/path")
598598
ws.workspace.download.return_value.__enter__.return_value.read.return_value = b"test"
599599
workspace_path.rename("/new/path", overwrite=True)
600600
ws.workspace.upload.assert_called_once_with("/new/path", b"test", format=ImportFormat.AUTO, overwrite=True)
601601

602602

603-
def test_unlink_existing_file():
603+
def test_unlink_existing_file() -> None:
604604
ws = create_autospec(WorkspaceClient)
605605
workspace_path = WorkspacePath(ws, "/test/path")
606606
ws.workspace.get_status.return_value = True
607607
workspace_path.unlink()
608608
ws.workspace.delete.assert_called_once_with("/test/path")
609609

610610

611-
def test_unlink_non_existing_file():
611+
def test_unlink_non_existing_file() -> None:
612612
ws = create_autospec(WorkspaceClient)
613613
workspace_path = WorkspacePath(ws, "/test/path")
614614
ws.workspace.get_status.side_effect = NotFound("Simulated NotFound")
@@ -640,23 +640,23 @@ def test_relative_to() -> None:
640640
_ = WorkspacePath(ws, "/home/bob").relative_to("/home/../usr", walk_up=True)
641641

642642

643-
def test_as_fuse_in_databricks_runtime():
643+
def test_as_fuse_in_databricks_runtime() -> None:
644644
with patch.dict("os.environ", {"DATABRICKS_RUNTIME_VERSION": "14.3"}):
645645
ws = create_autospec(WorkspaceClient)
646646
workspace_path = WorkspacePath(ws, "/test/path")
647647
result = workspace_path.as_fuse()
648648
assert str(result) == "/Workspace/test/path"
649649

650650

651-
def test_as_fuse_outside_databricks_runtime():
651+
def test_as_fuse_outside_databricks_runtime() -> None:
652652
with patch.dict("os.environ", {}, clear=True):
653653
ws = create_autospec(WorkspaceClient)
654654
workspace_path = WorkspacePath(ws, "/test/path")
655655
result = workspace_path.as_fuse()
656656
assert str(result) == "/Workspace/test/path"
657657

658658

659-
def test_home_directory():
659+
def test_home_directory() -> None:
660660
ws = create_autospec(WorkspaceClient)
661661
ws.current_user.me.return_value.user_name = "test_user"
662662
workspace_path = WorkspacePath(ws, "/test/path")
@@ -675,63 +675,63 @@ def test_absolute() -> None:
675675
_ = WorkspacePath(ws, "relative/path").absolute()
676676

677677

678-
def test_is_dir_when_object_type_is_directory():
678+
def test_is_dir_when_object_type_is_directory() -> None:
679679
ws = create_autospec(WorkspaceClient)
680680
workspace_path = WorkspacePath(ws, "/test/path")
681681
workspace_path._object_info.object_type = ObjectType.DIRECTORY
682682
assert workspace_path.is_dir() is True
683683

684684

685-
def test_is_dir_when_object_type_is_not_directory():
685+
def test_is_dir_when_object_type_is_not_directory() -> None:
686686
ws = create_autospec(WorkspaceClient)
687687
workspace_path = WorkspacePath(ws, "/test/path")
688688
workspace_path._object_info.object_type = ObjectType.FILE
689689
assert workspace_path.is_dir() is False
690690

691691

692-
def test_is_dir_when_databricks_error_occurs():
692+
def test_is_dir_when_databricks_error_occurs() -> None:
693693
ws = create_autospec(WorkspaceClient)
694694
workspace_path = WorkspacePath(ws, "/test/path")
695695
ws.workspace.get_status.side_effect = NotFound("Simulated NotFound")
696696
assert workspace_path.is_dir() is False
697697

698698

699-
def test_is_file_when_object_type_is_file():
699+
def test_is_file_when_object_type_is_file() -> None:
700700
ws = create_autospec(WorkspaceClient)
701701
workspace_path = WorkspacePath(ws, "/test/path")
702702
workspace_path._object_info.object_type = ObjectType.FILE
703703
assert workspace_path.is_file() is True
704704

705705

706-
def test_is_file_when_object_type_is_not_file():
706+
def test_is_file_when_object_type_is_not_file() -> None:
707707
ws = create_autospec(WorkspaceClient)
708708
workspace_path = WorkspacePath(ws, "/test/path")
709709
workspace_path._object_info.object_type = ObjectType.DIRECTORY
710710
assert workspace_path.is_file() is False
711711

712712

713-
def test_is_file_when_databricks_error_occurs():
713+
def test_is_file_when_databricks_error_occurs() -> None:
714714
ws = create_autospec(WorkspaceClient)
715715
workspace_path = WorkspacePath(ws, "/test/path")
716716
ws.workspace.get_status.side_effect = NotFound("Simulated NotFound")
717717
assert workspace_path.is_file() is False
718718

719719

720-
def test_is_notebook_when_object_type_is_notebook():
720+
def test_is_notebook_when_object_type_is_notebook() -> None:
721721
ws = create_autospec(WorkspaceClient)
722722
workspace_path = WorkspacePath(ws, "/test/path")
723723
workspace_path._object_info.object_type = ObjectType.NOTEBOOK
724724
assert workspace_path.is_notebook() is True
725725

726726

727-
def test_is_notebook_when_object_type_is_not_notebook():
727+
def test_is_notebook_when_object_type_is_not_notebook() -> None:
728728
ws = create_autospec(WorkspaceClient)
729729
workspace_path = WorkspacePath(ws, "/test/path")
730730
workspace_path._object_info.object_type = ObjectType.FILE
731731
assert workspace_path.is_notebook() is False
732732

733733

734-
def test_is_notebook_when_databricks_error_occurs():
734+
def test_is_notebook_when_databricks_error_occurs() -> None:
735735
ws = create_autospec(WorkspaceClient)
736736
workspace_path = WorkspacePath(ws, "/test/path")
737737
ws.workspace.get_status.side_effect = NotFound("Simulated NotFound")
@@ -946,7 +946,7 @@ def test_glob_case_insensitive() -> None:
946946
assert set(WorkspacePath(ws, "/etc").glob("PasSWd", case_sensitive=False)) == {WorkspacePath(ws, "/etc/passwd")}
947947

948948

949-
def test_globbing_when_nested_json_files_exist():
949+
def test_globbing_when_nested_json_files_exist() -> None:
950950
ws = create_autospec(WorkspaceClient)
951951
workspace_path = WorkspacePath(ws, "/test/path")
952952
ws.workspace.get_status.return_value = ObjectInfo(object_type=ObjectType.DIRECTORY)

0 commit comments

Comments
 (0)