Skip to content

Allow path_type of the Path type to accept a callable (#405_)#1234

Closed
termim wants to merge 1 commit into
pallets:masterfrom
termim:I405
Closed

Allow path_type of the Path type to accept a callable (#405_)#1234
termim wants to merge 1 commit into
pallets:masterfrom
termim:I405

Conversation

@termim
Copy link
Copy Markdown

@termim termim commented Feb 19, 2019

This would allow to convert path names to pathlib.Path instances
for example.

closes #405

Copy link
Copy Markdown

@LucidOne LucidOne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looks like this assertion needs to be uncommented.

@LucidOne
Copy link
Copy Markdown

Ran tox on this locally

  py37: commands succeeded
SKIPPED:  py36: InterpreterNotFound: python3.6
  py35: commands succeeded
SKIPPED:  py34: InterpreterNotFound: python3.4
  py27: commands succeeded
SKIPPED:  pypy3: InterpreterNotFound: pypy3
SKIPPED:  pypy: InterpreterNotFound: pypy

I also tested this with an application I'm developing by adding git+https://github.com/termim/click.git@I405#egg=click to my requirements.txt and it works as advertised.

However, using pathlib.Path as the path_type with Click7.0

@click.option('--config', '-c', type=click.Path(exists=True,dir_okay=False,path_type=pathlib.Path), required=False, help='Config file')

sets config to type bytes though backwards compatibility can be maintained as needed with

config = pathlib.Path(config.decode()) if isinstance(config,bytes) else config

as pathlib.Path(None) isn't accepted.

Also, I'm not sure if there would be an advantage to setting this to isinstance(input,os.PathLike) but it may be worth considering.

Aside from the above it looks good to me, I would like to see this merged.

This would allow to convert path names to pathlib.Path instances
for example.
@termim
Copy link
Copy Markdown
Author

termim commented Feb 21, 2019

I looks like this assertion needs to be uncommented.

Done.

@termim
Copy link
Copy Markdown
Author

termim commented Feb 21, 2019

However, using pathlib.Path as the path_type with Click7.0

@click.option('--config', '-c', type=click.Path(exists=True,dir_okay=False,path_type=pathlib.Path), required=False, help='Config file')

sets config to type bytes though backwards compatibility can be maintained as needed with

I can't reproduce this:

click>$ python3 --version
Python 3.5.2
click>$ grep __version__ ./click/click/__init__.py 
__version__ = '7.1.dev'
click>$ cat t.py
import click
import pathlib

@click.command()
@click.option('--config', '-c', type=click.Path(exists=True,dir_okay=False,path_type=pathlib.Path), required=False, help='Config file')
def hello(config):
    click.echo('Hello %s!' % type(config))

if __name__ == '__main__':
    hello()
click>$ 
click>$ PYTHONPATH=./click python3 ./t.py -c ./t.py
Hello <class 'pathlib.PosixPath'>!
click>$ 
config = pathlib.Path(config.decode()) if isinstance(config,bytes) else config

as pathlib.Path(None) isn't accepted.

Also, I'm not sure if there would be an advantage to setting this to isinstance(input,os.PathLike) but it may be worth considering.

That would make click require pathlib2 for python2 ...

@LucidOne
Copy link
Copy Markdown

LucidOne commented Feb 21, 2019

However, using pathlib.Path as the path_type with Click7.0

@click.option('--config', '-c', type=click.Path(exists=True,dir_okay=False,path_type=pathlib.Path), required=False, help='Config file')

sets config to type bytes though backwards compatibility can be maintained as needed with

I can't reproduce this:

click>$ python3 --version
Python 3.5.2
click>$ grep __version__ ./click/click/__init__.py 
__version__ = '7.1.dev'
click>$ cat t.py
import click
import pathlib

@click.command()
@click.option('--config', '-c', type=click.Path(exists=True,dir_okay=False,path_type=pathlib.Path), required=False, help='Config file')
def hello(config):
    click.echo('Hello %s!' % type(config))

if __name__ == '__main__':
    hello()
click>$ 
click>$ PYTHONPATH=./click python3 ./t.py -c ./t.py
Hello <class 'pathlib.PosixPath'>!
click>$ 

I was getting bytes using path_type=pathlib.Path without your PR on version 7.0.

$ python t.py -c t.py
Hello <class 'bytes'>!

@termim
Copy link
Copy Markdown
Author

termim commented Feb 21, 2019

I was getting bytes using path_type=pathlib.Path without your PR on version 7.0.

Ah, yes, without this #1234 it always returns bytes if path_type is not a string ...

@LucidOne
Copy link
Copy Markdown

config = pathlib.Path(config.decode()) if isinstance(config,bytes) else config
provides backwards compatibility, so application developers can move to pathlib while waiting for your PR to make it downstream.

@davidism davidism mentioned this pull request Mar 5, 2021
6 tasks
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 20, 2021
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.

click.Path returns str, not pathlib.Path

2 participants