Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jinja2 incompatibility with paths created using pathlib #870

Closed
breisfeld opened this issue Jun 29, 2018 · 6 comments · Fixed by #1064
Closed

jinja2 incompatibility with paths created using pathlib #870

breisfeld opened this issue Jun 29, 2018 · 6 comments · Fixed by #1064
Milestone

Comments

@breisfeld
Copy link

@breisfeld breisfeld commented Jun 29, 2018

As part of refactoring, I have been converting my application from using os.path to pathlib.

However, it appears that jinja2 is incompatible with paths created using pathlib.Path.

For example, if tdir is a pathlib Path object, the following generates an error:

jinja_environment = Environment(loader=FileSystemLoader(tdir), trim_blocks=False, lstrip_blocks=True),

where the bottom of the error traceback is

    jinja_environment = Environment(loader=FileSystemLoader(tdir),
  File ".../python/anaconda3/lib/python3.6/site-packages/jinja2/loaders.py", line 163, in __init__
    self.searchpath = list(searchpath)
TypeError: 'PosixPath' object is not iterable

If I convert tdir to a string, things work fine.

jinja: v 2.10
python: v3.6.5

@ThiefMaster

This comment has been minimized.

Copy link
Member

@ThiefMaster ThiefMaster commented Jun 29, 2018

Are python libraries supposed to accept pathlib Path objects in places where usually a string path is provided? If yes, a pull request (that does not break Python 2 compatibility!) would be appreciated.

@breisfeld

This comment has been minimized.

Copy link
Author

@breisfeld breisfeld commented Jun 29, 2018

I don't know about 'supposed to', but pathlib seems to be picking up steam as an alternative to os.path. I will take a look to see if this change could be made without affecting Python 2 compatibility.

@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Aug 7, 2018

PRs for this: #871, #874, #876. Please push new commits rather than new PRs for every review.

However, I don't think we should be converting anything to string. We don't do anything with the paths that requires them to be a string. Python functions already know how to use Path and we're just passing on to them.

The original issue was that Path is not string_types, so the loader was incorrectly calling str on it. The solution should be to fix the isinstance check to check that it's not an iterable.

tonal added a commit to tonal/jinja that referenced this issue Apr 8, 2019
@dedekind

This comment has been minimized.

Copy link

@dedekind dedekind commented Aug 8, 2019

Experienced the following crash today.

{% include path %}

where "path" is a pathlib.Path object, not a string. Rendering the template crashes:

TypeError: 'PosixPath' object is not iterable

Does this belong to this ticket?

Many python 3 developers love pathlib (including myself). It would be great to make jinja2 pathlib-friendly.

@ngfgrant

This comment has been minimized.

Copy link

@ngfgrant ngfgrant commented Oct 2, 2019

This issue also arises if a project uses pytest tmpdir for testing.

@iomintz

This comment has been minimized.

Copy link

@iomintz iomintz commented Oct 7, 2019

@dedekind, I believe that is also due to FileSystemLoader not supporting pathlib.Paths. It does belong here.

@davidism davidism added this to the 2.11.0 milestone Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

6 participants
You can’t perform that action at this time.