Resolve optional Path correctly#197
Conversation
|
Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry. |
|
Can we not keep this as a
|
There was a problem hiding this comment.
Pull request overview
This PR fixes the type behavior of confuse.templates.Path when wrapped in confuse.Optional(...), so type-checkers infer pathlib.Path | None (instead of pathlib.PurePath | None)—addressing the reported mypy return-type mismatch.
Changes:
- Update
confuse.templates.Pathto be typed asFilename[pathlib.Path](notPurePath). - Ensure
as_template()convertspathlib.PurePathinputs topathlib.Pathbefore constructing aPathtemplate. - Add a typing assertion test to validate
Optional(Path())returnspathlib.Path | None.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/test_valid.py | Adds a typing assertion for Optional(Path()), but currently imports typing_extensions in a way that breaks Python 3.13+ test runs. |
| confuse/templates.py | Adjusts Path’s generic type to pathlib.Path and updates as_template() to pass a concrete pathlib.Path default. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Looking at the following graph It seems like both |
|
Do we need to use It feels more natural to me to have |
|
Quoting from the URL I shared:
I think we should assume that users expect to work with 'real' paths? |
|
Yeah fair enough. |
5d148ac to
f2e16cb
Compare
|
Added another sneaky commit, to make sure that |
2923e8f to
6c6bd01
Compare
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]
6c6bd01 to
e24ce6e
Compare

I was adding types to beetsplug/fetchart.py and found that this
fails with