-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
click.Path returns str, not pathlib.Path #405
Comments
pathlib is Python 3 only afaict, Click isn't. |
FWIW I'm certain that an option to return a Path object is useful, but I can't see it becoming the default. |
It's still not possible to make this a default since this would mean that the behavior of Click would depend on whether that backport is installed. And depending on that backport is not worth the added value. |
what cost/value function evaluates to this result? 😄 |
In this case, new functionality. Pathlib is merely a new API for the same Sorry, but I don't think this is going to happen. I'll gladly accept PRs for a On Mon, Aug 10, 2015 at 06:28:08AM -0700, Philipp A. wrote:
|
What do you mean with “non-default option”? |
Opt-in for returning a pathlib.Path object instead of a string. The user has to On Mon, Aug 10, 2015 at 06:37:04AM -0700, Philipp A. wrote:
|
got it, makes sense. best would be to do it on arbitrary levels, right? e.g. you could create a group that is configured to do it, passing it down to all subcommands and parameters, or on single parameters or even options. the interaction of this with |
Honestly I don't think that's a good idea, it makes it hard to trace why a particular option is returning this particular type. If you want to avoid repetition/verbosity, you can still bind the created decorator to a new name for reuse, or create a new decorator that has it as default. |
hmm, yeah, using |
Not going to happen. A custom path implementation might be something for addon packages. |
Welp- |
worth reevaluating in the light of PEP 519? also are you going to sign the python 3 statement? if so, we can tackle this in 2020. |
That PEP makes a lot of things easier, but AFAICT it doesn't even apply to all
versions of Python 3.
I'd personally love to drop Python 2 at some point, but even for me 2020 is way
too soon. It's not (only) my decision to make.
…On Mon, Jan 23, 2017 at 02:26:37AM -0800, Philipp A. wrote:
worth reevaluating in the light of [PEP 519](https://www.python.org/dev/peps/pep-0519/)?
also are you going to sign the [python statement](http://www.python3statement.org/)? if so, we can tackle this in 2020.
--
You are receiving this because you modified the open/close state.
Reply to this email directly or view it on GitHub:
#405 (comment)
|
i didn’t want to propose that, just ask 😉 the PEP partly applies to everything, as it describes a protocol, not just an API. checking for the existence of |
It would be really confusing to get a Is gating this on a (non-default) flag on |
Since That way someone can also pass it through something else that does something interesting with the path. |
err, #1148 |
I'd love to see this. I expected |
This would allow to convert path names to pathlib.Path instances for example.
I 'd agree with @NotTheEconomist version without new arguments. |
This would allow to convert path names to pathlib.Path instances for example.
This would allow to convert path names to pathlib.Path instances for example.
This would allow to convert path names to pathlib.Path instances for example.
This would allow to convert path names to pathlib.Path instances for example.
This would allow to convert path names to pathlib.Path instances for example.
This would allow to convert path names to pathlib.Path instances for example.
For the impatient, I defined my own like this. It accepts all the same options as class PathPath(click.Path):
"""A Click path argument that returns a pathlib Path, not a string"""
def convert(self, value, param, ctx):
return Path(super().convert(value, param, ctx)) @click.argument('input', type=PathPath(dir_okay=False))
def ... (this usage style also feels cleaner to me than |
Thanks @jeremyh ! I have packaged this solution into https://pypi.org/project/click-pathlib, so you can use:
import click
import click_pathlib
@click.command('delete')
@click.argument(
'existing_file',
type=click_pathlib.Path(exists=True),
)
def delete(existing_file):
existing_file.unlink() |
Seeing as Python 2 is now dead and all, does it maybe make sense to now transition to using pathlib.Path as a default instead of a str value? |
Pretty sure that would require a major release, and while pathlib is awesome, I think it's the kind of change that would be a pain in the ass for any downstream user who doesn't already use pathlib. |
Also, even if Path is available on Python 3.x it's not mandatory to use it, a developer can still choose to use plain string or a Path instance |
Any request for making this the default basically asks for two things:
Each of those points in isolation is an extreme measure to make this happen, combined even more. |
The second one isn’t, Python 2 isn’t supported anymore so you’re free to move on and get rid of all hideous compatibility hacks 🎉 About the first … click has a lot of major version releases, and derive from your reluctance that they’re all less intrusive than this one? |
Click supports Python 2. |
You know I mean that Python 2 is unmaintained as of 2 weeks ago. Supporting something that’s unmaintained is pretty futile. |
I'm saying that this is a completely separate decision to make. But I forgot that we already announced dropped support for Python 2 so this would probably be fine to land in Click 8. |
Hmm, just spitballing here, but maybe a transitional release could be done (after #1148 is merged). In Click 8, the following would raise a @click.argument('filename', type=click.Path())
And consequently, in Click 9 (or 10), the warning would be gone and the default changed. |
An alternative (also depending on #1148) would be to follow @mitsuhiko’s idea in #405 (comment): We just add |
It’s surprising that
INT
returns anint
,File
afile
,STRING
astr
, butPath
a …str
?A
click.Path
parameter should return apathlib.Path
object.The text was updated successfully, but these errors were encountered: