GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve types and `bytes` usage of `pathlib` by sobolevn · Pull Request #9016 · python/typeshed

Conversation

@sobolevn

@sobolevn sobolevn commented Oct 28, 2022

Copy link
Copy Markdown
Member

Notes:

  • link_to is removed in 3.12: https://github.com/python/cpython/blob/3.11/Lib/pathlib.py#L1222-L1225
  • samefile uses the same args as Path.__new__

Source: https://github.com/python/cpython/blob/main/Lib/pathlib.py
Refs #9006

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
sobolevn and others added 2 commits October 28, 2022 17:17
@sobolevn
Improve types and bytes usage of pathlib
b89c894
@pre-commit-ci
[pre-commit.ci] auto fixes from pre-commit.com hooks
91633a0
AlexWaygood
AlexWaygood reviewed Oct 28, 2022
View reviewed changes
Comment thread stdlib/pathlib.pyi Resolved

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn
Fix ci
6f5bb25
srittau
srittau requested changes Oct 28, 2022
View reviewed changes
Comment thread stdlib/pathlib.pyi
def read_text(self, encoding: str | None = ..., errors: str | None = ...) -> str: ...
def samefile(self, other_path: str | bytes | int | Path) -> bool: ...
def write_bytes(self, data: bytes) -> int: ...
def samefile(self, other_path: StrPath) -> bool: ...

@srittau srittau Oct 28, 2022

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just calls other_path.stat(), which should also work with Path[bytes]. In case it's not a Path, it's passed on to os.stat(), which uses int | StrOrBytesPath. I therefore think the following would be correct:

Suggested change
def samefile(self, other_path: StrPath) -> bool: ...
def samefile(self, other_path: StrOrBytesPath | int) -> bool: ...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AlexWaygood AlexWaygood Oct 28, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path[bytes] isn't a thing — Path isn't a generic class. pathlib only makes promises to work with string paths, unlike os.path or functions in os

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@srittau srittau Oct 28, 2022

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always mix up Path and PathLike. The annotation should be correct nevertheless.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@sobolevn sobolevn Oct 28, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here' how this method is implemented:

    def samefile(self, other_path):
        st = self.stat()
        try:
            other_st = other_path.stat()
        except AttributeError:
            other_st = self.__class__(other_path).stat()
        return os.path.samestat(st, other_st)

So, other_path can either be:

  • Something with .stat() method (assuming Path, it can be a new protocol, but I don't think it is worth it right now)
  • Something that self.__class__.__new__ accepts: StrPath (== str | PathLike[str])

But, Path is a part of StrPath, because Path is PathLike[str].
That's why StrPath seems correct to me. Am I missing something?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@srittau srittau Oct 28, 2022

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah interesting, I was looking at the 3.9 version of the implementation:

https://github.com/python/cpython/blob/b43496c01a554cf41ae654a0379efae18609ad39/Lib/pathlib.py#L1145-L1154

    def samefile(self, other_path):
        """Return whether other_path is the same or not as this file
        (as returned by os.path.samefile()).
        """
        st = self.stat()
        try:
            other_st = other_path.stat()
        except AttributeError:
            other_st = self._accessor.stat(other_path)
        return os.path.samestat(st, other_st)

No need to version guard this. Let's just use the latest version (yours).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread stdlib/pathlib.pyi Resolved

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

This comment has been minimized.

Sign in to view
sobolevn and others added 3 commits October 28, 2022 19:55
@sobolevn
Merge branch 'bytes-pathlib' of https://github.com/sobolevn/typeshed …
b2fbfe4
…into bytes-pathlib
@sobolevn
Address review
c23edae
@pre-commit-ci
[pre-commit.ci] auto fixes from pre-commit.com hooks
b4c23a3
@github-actions

github-actions Bot commented Oct 28, 2022

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

bandersnatch (https://github.com/pypa/bandersnatch)
+ src/bandersnatch_storage_plugins/swift.py: note: In member "write_bytes" of class "SwiftPath":
+ src/bandersnatch_storage_plugins/swift.py:408: error: Signature of "write_bytes" incompatible with supertype "Path"
+ src/bandersnatch_storage_plugins/swift.py:408: note:      Superclass:
+ src/bandersnatch_storage_plugins/swift.py:408: note:          def write_bytes(self, data: Union[bytes, Union[bytearray, memoryview, array[Any], mmap, _CData, PickleBuffer]]) -> int
+ src/bandersnatch_storage_plugins/swift.py:408: note:      Subclass:
+ src/bandersnatch_storage_plugins/swift.py:408: note:          def write_bytes(self, contents: bytes, encoding: Optional[str] = ..., errors: Optional[str] = ...) -> int
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

srittau
srittau approved these changes Oct 28, 2022
View reviewed changes
@srittau
srittau merged commit d3a87eb into python:master Oct 28, 2022
@AlexWaygood AlexWaygood mentioned this pull request Oct 28, 2022
Track review of bytes types #9006
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@AlexWaygood AlexWaygood AlexWaygood left review comments
@srittau srittau srittau approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@sobolevn @srittau @AlexWaygood
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.