Fix typing for save_html, save_text, save_svg to accept PathLike #3941
+7
−6
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.
Type of changes
AI?
AI generated PRs may be accepted, but only if @willmcgugan has responded on an issue or discussion.
Checklist
Description
Issue
The typing for
save_html(),save_text(), andsave_svg()methods is overly strict. They only acceptstrfor thepathparameter, but the underlyingopen()call acceptsPathLikeobjects (likepathlib.Path).This causes static type checkers to complain when passing valid path-like objects:
Changes Made
Updated the type hints for the
pathparameter in these methods:save_html(path: str, ...)→save_html(path: Union[str, PathLike[str]], ...)save_text(path: str, ...)→save_text(path: Union[str, PathLike[str]], ...)save_svg(path: str, ...)→save_svg(path: Union[str, PathLike[str]], ...)Also:
from os import PathLikeimportThis matches the typing used by the builtin
open()function and allows users to passpathlib.Pathobjects without type checker errors.Behavior
No runtime behavior changes - the code already worked with
PathLikeobjects sinceopen()handles them. This is purely a typing fix to match the actual runtime behavior.Fixes #3784