Skip to content
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

GH-128520: More consistent type-checking behaviour in pathlib #130199

Merged
merged 6 commits into from
Feb 21, 2025

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Feb 16, 2025

In the following methods, skip casting of the argument to a path object if the argument has a with_segments attribute.

  • In PurePath: relative_to(), is_relative_to(), match(), and full_match().
  • In Path: rename(), replace(), copy(), copy_into(), move(), and move_into().

Previously the check varied a bit from method to method. The PurePath methods checked isinstance(arg, PurePath); the rename() and replace() methods always cast, and the remaining Path methods checked for a private arg._copy_writer attribute.

We apply identical changes to relevant methods of the private ABCs. This improves performance a bit, because isinstance() checks on ABCs are expensive.

In the following methods, skip casting of the argument to a path object if
the argument has a `with_segments` attribute. In `PurePath`:
`relative_to()`, `is_relative_to()`, `match()`, and `full_match()`. In
`Path`: `rename()`, `replace()`, `copy()`, `copy_into()`, `move()`, and
`move_into()`.

Previously the check varied a bit from method to method. The `PurePath`
methods used `isinstance(arg, PurePath)`; the `rename()` and `replace()`
methods always cast, and the remaining `Path` methods checked for a private
`_copy_writer` attribute.

We apply identical changes to relevant methods of the private ABCs. This
improves performance a bit, because `isinstance()` checks on ABCs are
expensive.
@barneygale barneygale merged commit d88677a into python:main Feb 21, 2025
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant