Fix CliRunner TypeError with pathlib.Path arguments#3284
Closed
elazar wants to merge 7 commits intopallets:mainfrom
Closed
Fix CliRunner TypeError with pathlib.Path arguments#3284elazar wants to merge 7 commits intopallets:mainfrom
elazar wants to merge 7 commits intopallets:mainfrom
Conversation
- Add automatic Path-to-string conversion in CliRunner.invoke() - Resolves 'object of type PosixPath has no len()' error - Uses __fspath__ protocol for path-like object detection - Maintains backward compatibility with existing string arguments - Add comprehensive test coverage for pathlib integration Fixes pallets#1324
Apply automatic formatting fixes from ruff: - Convert single quotes to double quotes for consistency - Reformat list comprehension to single line - Fix import order and spacing - Add blank lines after docstrings per style guide
The TypeError message differs between CPython and PyPy: - CPython: "object of type 'PosixPath' has no len()" - PyPy: "'PosixPath' has no length" Update regex to match both variants.
- Use str(test_path) in assertions to handle platform-specific path separators - Update error message regex to match both PosixPath and WindowsPath - Handle different error message formats across Python implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
• Fixes
TypeError: object of type 'PosixPath' has no len()when passingpathlib.Pathobjects toCliRunner.invoke()• Implements automatic Path-to-string conversion using Python's
__fspath__protocol• Preserves backward compatibility and core parser behavior
Background
When
pathlib.Pathobjects are passed toCliRunner.invoke(), Click's argument parser crashes because it callslen()on the Path object without type checking. This issue was reported in #1324 but remained unresolved with a cryptic error message that doesn't guide users to the solution.Changes
Core Fix (
src/click/testing.py):CliRunner.invoke()__fspath__protocol for path-like object detectionTest Coverage (
tests/test_testing.py):Related Issues
Fixes #1324