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

API: Add support for PEP 519 #13823

Closed
AnkurDedania opened this Issue Jul 27, 2016 · 3 comments

Comments

Projects
None yet
5 participants
@AnkurDedania

AnkurDedania commented Jul 27, 2016

File System Path Protocol has been accepted and implemented into Python 3.6, a backwards compatibility layer to support the introduced PEP 519 for python version < 3.6.

@shoyer

This comment has been minimized.

Show comment
Hide comment
@shoyer

shoyer Jul 28, 2016

Member

See this section for guidance relevant to pandas:
https://www.python.org/dev/peps/pep-0519/#backwards-compatibility

Libraries wishing to support path objects and a version of Python prior to Python 3.6 and the existence of os.fspath() can use the idiom of path.__fspath__() if hasattr(path, "__fspath__") else path .

Member

shoyer commented Jul 28, 2016

See this section for guidance relevant to pandas:
https://www.python.org/dev/peps/pep-0519/#backwards-compatibility

Libraries wishing to support path objects and a version of Python prior to Python 3.6 and the existence of os.fspath() can use the idiom of path.__fspath__() if hasattr(path, "__fspath__") else path .

@flying-sheep

This comment has been minimized.

Show comment
Hide comment
@flying-sheep

flying-sheep Jan 12, 2017

Contributor

so we add to compat/__init__.py (with extracted stuff from here)

def fspath(path):
	if isinstance(path, (str, bytes)):
 		return path
	
	if _PATHLIB_INTSTALLED and isinstance(path, pathlib.Path):
		path = text_type(path)  # 3.4/3.5 compat
	if _PYPATH_INSTALLED and isinstance(path, LocalPath):
		path = path.strpath
	else:
		path_type = type(path)
		try:
			path = path_type.__fspath__(path)
		except AttributeError:
			if hasattr(path_type, '__fspath__'):
				raise
			raise TypeError("expected str, bytes, py.path.LocalPath or os.PathLike object, not " + path_type.__name__) from None
	
	if isinstance(path, (str, bytes)):
		return path
	else:
		raise TypeError("expected __fspath__() to return str or bytes, not " + type(path).__name__)
Contributor

flying-sheep commented Jan 12, 2017

so we add to compat/__init__.py (with extracted stuff from here)

def fspath(path):
	if isinstance(path, (str, bytes)):
 		return path
	
	if _PATHLIB_INTSTALLED and isinstance(path, pathlib.Path):
		path = text_type(path)  # 3.4/3.5 compat
	if _PYPATH_INSTALLED and isinstance(path, LocalPath):
		path = path.strpath
	else:
		path_type = type(path)
		try:
			path = path_type.__fspath__(path)
		except AttributeError:
			if hasattr(path_type, '__fspath__'):
				raise
			raise TypeError("expected str, bytes, py.path.LocalPath or os.PathLike object, not " + path_type.__name__) from None
	
	if isinstance(path, (str, bytes)):
		return path
	else:
		raise TypeError("expected __fspath__() to return str or bytes, not " + type(path).__name__)
@jreback

This comment has been minimized.

Show comment
Hide comment
@jreback

jreback Jan 12, 2017

Contributor

there are already routines in io/common.py to handle pathlike things - they just need some updating

Contributor

jreback commented Jan 12, 2017

there are already routines in io/common.py to handle pathlike things - they just need some updating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment