Passing an instance of Pathlib.Path to CliRunner.invoke() gets a TypeError #1324
Comments
|
This code change would solve your problem in core.BaseCommand.main: if args is None:
args = get_os_args()
else:
args = [arg.__fspath__() if hasattr(arg, "__fspath__") else arg
for arg in args]However, that would still not make support for other types work, such as integers. In general, the parser assumes all inputs are strings. To properly handle this, a pre-processing step would have to be done to determine which arguments on the command line are associated with which types. However, this adds a lot of complexity, since command line arguments like dashes need to be handled. This would be a huge overhaul to the parser and is probably not a good idea. I suggest closing this issue and just using the noted workaround. |
|
I think it’s reasonable to say “we can’t coerce all arguments to strings”, but the confusing error message remains. Would it be possible to add a check something like:
to make it more obvious when somebody has made this mistake? |
|
If the documentation is unclear that the |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Consider the following example:
With Python 3.6.4 and click 7.0, this is the output you get:
This can be easily fixed by wrapping in
str(), i.e.,But in general I've got used to the implicit contract (at least in the standard library) that I can use a
Pathobject anywhere I can use a string representation of a path. Since paths are often the arguments to command-line tools, it'd be nice to be able to use a Path object here as well.Alternatively, raise a more helpful TypeError if you pass a non-string object into
invoke()– I spent a while poking around in my code to see where I was callinglen()on the wrong object before realising it was actually coming from click.The text was updated successfully, but these errors were encountered: