Skip to content

Commit 52433c7

Browse files
authored
Catch and assert warnings in tests (#487)
* tests: add checks for warnings * tests: add checks for warnings read_text write_text * tests: ignore keyword warnings in NotImplemented tests * tests: check warnings in extensions tests * tests: fix warnings in pathlib312 tests
1 parent be85f9b commit 52433c7

File tree

6 files changed

+267
-33
lines changed

6 files changed

+267
-33
lines changed

upath/implementations/local.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def from_uri(cls, uri: str, **storage_options: Any) -> Self:
449449
def is_dir(self, *, follow_symlinks: bool = True) -> bool:
450450
if not follow_symlinks:
451451
warnings.warn(
452-
f"{type(self).__name__}.is_dir() follow_symlinks=False"
452+
f"{type(self).__name__}.is_dir(): follow_symlinks=False"
453453
" is currently ignored.",
454454
UserWarning,
455455
stacklevel=2,
@@ -459,7 +459,7 @@ def is_dir(self, *, follow_symlinks: bool = True) -> bool:
459459
def is_file(self, *, follow_symlinks: bool = True) -> bool:
460460
if not follow_symlinks:
461461
warnings.warn(
462-
f"{type(self).__name__}.is_file() follow_symlinks=False"
462+
f"{type(self).__name__}.is_file(): follow_symlinks=False"
463463
" is currently ignored.",
464464
UserWarning,
465465
stacklevel=2,
@@ -474,7 +474,7 @@ def read_text(
474474
) -> str:
475475
if newline is not None:
476476
warnings.warn(
477-
f"{type(self).__name__}.read_text() newline"
477+
f"{type(self).__name__}.read_text(): newline"
478478
" is currently ignored.",
479479
UserWarning,
480480
stacklevel=2,
@@ -499,7 +499,7 @@ def glob( # type: ignore[override]
499499
if case_sensitive is not None:
500500
if sys.version_info < (3, 12):
501501
warnings.warn(
502-
f"{type(self).__name__}.glob() case_sensitive"
502+
f"{type(self).__name__}.glob(): case_sensitive"
503503
" is currently ignored.",
504504
UserWarning,
505505
stacklevel=2,
@@ -508,7 +508,7 @@ def glob( # type: ignore[override]
508508
kw["case_sensitive"] = case_sensitive
509509
if recurse_symlinks:
510510
warnings.warn(
511-
f"{type(self).__name__}.glob() recurse_symlinks=True"
511+
f"{type(self).__name__}.glob(): recurse_symlinks=True"
512512
" is currently ignored.",
513513
UserWarning,
514514
stacklevel=2,
@@ -530,7 +530,7 @@ def rglob( # type: ignore[override]
530530
if case_sensitive is not None:
531531
if sys.version_info < (3, 12):
532532
warnings.warn(
533-
f"{type(self).__name__}.rglob() case_sensitive"
533+
f"{type(self).__name__}.rglob(): case_sensitive"
534534
" is currently ignored.",
535535
UserWarning,
536536
stacklevel=2,
@@ -539,7 +539,7 @@ def rglob( # type: ignore[override]
539539
kw["case_sensitive"] = case_sensitive
540540
if recurse_symlinks:
541541
warnings.warn(
542-
f"{type(self).__name__}.rglob() recurse_symlinks=True"
542+
f"{type(self).__name__}.rglob(): recurse_symlinks=True"
543543
" is currently ignored.",
544544
UserWarning,
545545
stacklevel=2,
@@ -592,7 +592,7 @@ def match(
592592
pattern_str = os.fspath(path_pattern)
593593
if case_sensitive is not None:
594594
warnings.warn(
595-
f"{type(self).__name__}.match() case_sensitive"
595+
f"{type(self).__name__}.match(): case_sensitive"
596596
" is currently ignored.",
597597
UserWarning,
598598
stacklevel=2,
@@ -602,7 +602,7 @@ def match(
602602
def exists(self, *, follow_symlinks: bool = True) -> bool:
603603
if not follow_symlinks:
604604
warnings.warn(
605-
f"{type(self).__name__}.exists() follow_symlinks=False"
605+
f"{type(self).__name__}.exists(): follow_symlinks=False"
606606
" is currently ignored.",
607607
UserWarning,
608608
stacklevel=2,

upath/tests/pathlib/test_pathlib_312.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,7 @@ def test_with_segments_common(self):
129129
class P(_BasePurePathSubclass, self.cls):
130130
pass
131131

132-
if self.cls is UPath:
133-
cm = temporary_register("", P)
134-
else:
135-
cm = nullcontext()
136-
137-
with cm:
132+
with temporary_register("", P):
138133
p = P('foo', 'bar', session_id=42)
139134
self.assertEqual(42, (p / 'foo').session_id)
140135
self.assertEqual(42, ('foo' / p).session_id)
@@ -3281,6 +3276,15 @@ class PathSubclassTest(_BasePathTest, unittest.TestCase):
32813276
class cls(WindowsUPath if os.name == 'nt' else PosixUPath):
32823277
pass
32833278

3279+
def setUp(self):
3280+
self._registry_cm = temporary_register("", self.cls)
3281+
self._registry_cm.__enter__()
3282+
return super().setUp()
3283+
3284+
def tearDown(self):
3285+
self._registry_cm.__exit__(None, None, None)
3286+
return super().tearDown()
3287+
32843288
# repr() roundtripping is not supported in custom subclass.
32853289
test_repr_roundtrips = None
32863290

upath/tests/test_extensions.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ def test_chmod(self):
6565
def test_eq(self):
6666
super().test_eq()
6767

68+
if sys.version_info < (3, 12):
69+
70+
def test_storage_options_dont_affect_hash(self):
71+
# On Python < 3.12, storage_options trigger warnings for LocalPath
72+
with pytest.warns(
73+
UserWarning,
74+
match=r".*on python <= \(3, 11\) ignores protocol and storage_options",
75+
):
76+
super().test_storage_options_dont_affect_hash()
77+
6878
def test_group(self):
6979
pytest.importorskip("grp")
7080
self.path.group()
@@ -91,9 +101,22 @@ def test_as_uri(self):
91101
def test_rename2(self):
92102
super().test_rename2()
93103

94-
def test_lstat(self):
95-
st = self.path.lstat()
96-
assert st is not None
104+
if sys.version_info < (3, 10):
105+
106+
def test_lstat(self):
107+
# On Python < 3.10, stat(follow_symlinks=False) triggers warnings
108+
with pytest.warns(
109+
UserWarning,
110+
match=r".*stat\(\) follow_symlinks=False is currently ignored",
111+
):
112+
st = self.path.lstat()
113+
assert st is not None
114+
115+
else:
116+
117+
def test_lstat(self):
118+
st = self.path.lstat()
119+
assert st is not None
97120

98121
def test_relative_to(self):
99122
base = self.path

0 commit comments

Comments
 (0)