Skip to content

Commit f2e16cb

Browse files
committed
Fix incompatibility with pathlib.Path
I was adding types to beetsplug/fetchart.py and found that this @cached_property def fallback(self) -> Path | None: return self.config["fallback"].get( confuse.Optional(confuse.templates.Path()) ) fails with beetsplug/fetchart.py:1370: error: Incompatible return value type (got "PurePath | None", expected "Path | None") [return-value]
1 parent e5fea14 commit f2e16cb

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

confuse/templates.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
T = TypeVar("T")
2525
T_co = TypeVar("T_co", covariant=True)
2626
K = TypeVar("K", bound=Hashable, default=str)
27-
P = TypeVar("P", bound=pathlib.PurePath | str, default=str)
27+
P = TypeVar("P", bound=pathlib.Path | str, default=str)
2828
V = TypeVar("V", default=object)
2929
ConfigKey = int | str | bytes
3030
ConfigKeyT = TypeVar("ConfigKeyT", bound=ConfigKey, default=str)
@@ -738,7 +738,7 @@ def value(
738738
return os.path.abspath(path_str)
739739

740740

741-
class Path(Filename[pathlib.PurePath]):
741+
class Path(Filename[pathlib.Path]):
742742
"""A template that validates strings as `pathlib.Path` objects.
743743
744744
Filenames are parsed equivalent to the `Filename` template and then
@@ -863,7 +863,7 @@ def as_template(value: set[T]) -> Choice[T, T]: ...
863863
@overload
864864
def as_template(value: list[T]) -> OneOf[T]: ...
865865
@overload
866-
def as_template(value: pathlib.PurePath) -> Path: ...
866+
def as_template(value: pathlib.Path) -> Path: ...
867867
@overload
868868
def as_template(value: None) -> Template[None]: ...
869869
@overload
@@ -897,7 +897,7 @@ def as_template(value: Any) -> Template[Any]:
897897
return Number()
898898
elif isinstance(value, float):
899899
return Number(value)
900-
elif isinstance(value, pathlib.PurePath):
900+
elif isinstance(value, pathlib.Path):
901901
return Path(value)
902902
elif value is None:
903903
return Template(None)

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Unreleased
66

77
- Require `typing_extensions` on older Python versions (and use `typing` on
88
newer versions). [#189](https://github.com/beetbox/confuse/issues/189)
9+
- Narrow `Path` template shorthand handling to concrete `pathlib.Path` values
10+
for more accurate type checking.
911

1012
v2.2.0
1113
------

test/test_valid.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import enum
22
import os
3+
import pathlib
34
import unittest
45
from collections.abc import Mapping, Sequence
56
from typing import Any
67

78
import pytest
9+
from typing_extensions import assert_type
810

911
import confuse
1012

@@ -461,19 +463,20 @@ def test_filename_wrong_type(self):
461463

462464
class PathTest(unittest.TestCase):
463465
def test_path_value(self):
464-
import pathlib
465-
466466
config = _root({"foo": "foo/bar"})
467467
valid = config["foo"].get(confuse.Path())
468468
assert valid == pathlib.Path(os.path.abspath("foo/bar"))
469469

470470
def test_default_value(self):
471-
import pathlib
472-
473471
config = _root({})
474472
valid = config["foo"].get(confuse.Path("foo/bar"))
475473
assert valid == pathlib.Path("foo/bar")
476474

475+
def test_optional_value_type(self):
476+
config = _root({"foo": "foo/bar"})
477+
valid = config["foo"].get(confuse.Optional(confuse.Path()))
478+
assert_type(valid, pathlib.Path | None)
479+
477480
def test_default_none(self):
478481
config = _root({})
479482
valid = config["foo"].get(confuse.Path(None))

0 commit comments

Comments
 (0)