Skip to content

Support paths created with pathlib in FileSystemLoader and ModuleLoader#1064

Merged
davidism merged 2 commits intopallets:masterfrom
alexwlchan:support-pathlib
Oct 21, 2019
Merged

Support paths created with pathlib in FileSystemLoader and ModuleLoader#1064
davidism merged 2 commits intopallets:masterfrom
alexwlchan:support-pathlib

Conversation

@alexwlchan
Copy link
Copy Markdown
Contributor

This patch adds support for passing paths created with pathlib.Path in FileSystemLoader and ModuleLoader, both as a single path or one of a list, i.e.,

FileSystemLoader(pathlib.Path('/templates'))
FileSystemLoader([pathlib.Path('/templates'), '/more/templates'])

ModuleLoader(pathlib.Path('/templates'))
ModuleLoader([pathlib.Path('/templates'), '/more/templates'])

There's no change in Python 2.7 (which doesn't support pathlib).

I've included tests for all four scenarios, along with a couple of other tests to improve the coverage of FileSystemLoader.

I believe this fixes #870.

@alexwlchan
Copy link
Copy Markdown
Contributor Author

(Tests are now passing, I forgot I had to support Python 2.7… 😢 )

@davidism
Copy link
Copy Markdown
Member

Great PR, thanks!

@davidism davidism added this to the 2.11.0 milestone Oct 18, 2019
@davidism
Copy link
Copy Markdown
Member

Ah, wait a second, @ThiefMaster's other review applies to this:

Just thinking.. would it make more sense to check for:

Like this it would not only support pathlib paths / objects whose __str__ is a path, but any object that represents a filesystem path.

Since pathlib support is a new feature in general I don't think the fact that __fspath__ is Python3.6-only (probably one could use a pathlib backport for any older Python version) is a problem.

@davidism
Copy link
Copy Markdown
Member

davidism commented Oct 18, 2019

pallets/flask#3059 might be helpful

@davidism davidism merged commit afd6c48 into pallets:master Oct 21, 2019
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

jinja2 incompatibility with paths created using pathlib

2 participants