PR #115551
tensorflow/tensorflow · Runtime · Merged · 2 files changed
Fixed a runtime TypeError during TensorFlow module initialization when sys.path contains pathlib.PosixPath objects by normalizing PathLike entries with os.fspath.
Checkout Pull Request
Inspect the full commit history and diff locally from upstream tensorflow/tensorflow.
$gh pr checkout 115551 --repo tensorflow/tensorflowFixed a runtime TypeError during TensorFlow module initialization when sys.path contains pathlib.PosixPath objects by normalizing PathLike entries with os.fspath.
File-by-File Modification Breakdown (2)
- +1 / -1
tensorflow/api_template.__init__.py
Upstream Python/C++ module implementation
- +1 / -1
tensorflow/api_template_v1.__init__.py
Upstream Python/C++ module implementation
Changed Files (2)
1. Introduction & Motivation
Modern Python projects frequently append pathlib.Path (PosixPath) objects directly to sys.path. During TensorFlow package startup in api_template.__init__.py, path-inspection logic assumed every sys.path element was a plain str and invoked string methods directly, raising TypeError on import [1].
2. PathLike Normalization
By PEP 519, any valid path entry implements os.PathLike or is a str. Applying os.fspath(p) maps both representations uniformly to str:
3. References
- Brett Cannon et al., PEP 519: Adding a File System Path Protocol, Python Enhancement Proposals, 2016.