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 — importlib: improve bytes handling by JelleZijlstra · Pull Request #9070 · python/typeshed

Conversation

@JelleZijlstra

@JelleZijlstra JelleZijlstra commented Nov 3, 2022

Copy link
Copy Markdown
Member

No description provided.

Sorry, something went wrong.

Uh oh!

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

All reactions
@JelleZijlstra
importlib: improve bytes handling
e0f741c
JelleZijlstra
JelleZijlstra commented Nov 3, 2022
View reviewed changes
Comment thread stdlib/importlib/abc.pyi
class ResourceLoader(Loader):
@abstractmethod
def get_data(self, path: _Path) -> bytes: ...
def get_data(self, path: str) -> bytes: ...

@JelleZijlstra JelleZijlstra Nov 3, 2022

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.

The docstring says it must be a str: https://github.com/python/cpython/blob/2cfcaf5af602b297fc90086de4d8ac980c7891e2/Lib/importlib/abc.py#L182

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread stdlib/importlib/abc.pyi
class ExecutionLoader(InspectLoader):
@abstractmethod
def get_filename(self, fullname: str) -> _Path: ...
def get_filename(self, fullname: str) -> str: ...

@JelleZijlstra JelleZijlstra Nov 3, 2022

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.

Docstring says "Abstract method which should return the value that file is to be set to." __file__ is always a str, not bytes. (Note that _Path = str | bytes.)

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread stdlib/importlib/abc.pyi
class SourceLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
def path_mtime(self, path: _Path) -> float: ...
def set_data(self, path: _Path, data: bytes) -> None: ...
def path_mtime(self, path: str) -> float: ...

@JelleZijlstra JelleZijlstra Nov 3, 2022

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.

Docstrings say the path must be str for all three methods.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread stdlib/importlib/abc.pyi
class PathEntryFinder(Finder):
def find_module(self, fullname: str) -> Loader | None: ...
def find_loader(self, fullname: str) -> tuple[Loader | None, Sequence[_Path]]: ...
def find_loader(self, fullname: str) -> tuple[Loader | None, Sequence[str]]: ...

@JelleZijlstra JelleZijlstra Nov 3, 2022

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.

The second element is ModuleSpec.submodule_search_locations which is a list[str]. https://github.com/python/cpython/blob/2cfcaf5af602b297fc90086de4d8ac980c7891e2/Lib/importlib/abc.py#L155

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread stdlib/importlib/abc.pyi
def __init__(self, fullname: str, path: _Path) -> None: ...
def get_data(self, path: _Path) -> bytes: ...
def get_filename(self, name: str | None = ...) -> _Path: ...
path: str

@JelleZijlstra JelleZijlstra Nov 3, 2022

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.

This class has no implementation, but from _bootstrap_external.FileLoader it seems clear the path is not intended to be bytes: https://github.com/python/cpython/blob/2cfcaf5af602b297fc90086de4d8ac980c7891e2/Lib/importlib/_bootstrap_external.py#L1195

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread stdlib/importlib/machinery.pyi
@classmethod
def find_spec(
cls, fullname: str, path: Sequence[bytes | str] | None = ..., target: types.ModuleType | None = ...
cls, fullname: str, path: Sequence[str] | None = ..., target: types.ModuleType | None = ...

@JelleZijlstra JelleZijlstra Nov 3, 2022

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.

The path gets passed to _get_spec, which ignores any non-str entries in the path: https://github.com/python/cpython/blob/2cfcaf5af602b297fc90086de4d8ac980c7891e2/Lib/importlib/_bootstrap_external.py#L1525

Sorry, something went wrong.

Uh oh!

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

All reactions
@github-actions

This comment has been minimized.

Sign in to view
@JelleZijlstra
remove more _Path usage
6f65926
@github-actions

This comment has been minimized.

Sign in to view
@JelleZijlstra JelleZijlstra mentioned this pull request Nov 3, 2022
Track review of bytes types #9006
Open
JelleZijlstra and others added 2 commits November 2, 2022 21:35
@JelleZijlstra
more fixes
dc72afd
@pre-commit-ci
[pre-commit.ci] auto fixes from pre-commit.com hooks
fbc3c96
@JelleZijlstra
JelleZijlstra marked this pull request as ready for review November 3, 2022 04:46
@github-actions

github-actions Bot commented Nov 3, 2022

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

All reactions

Sorry, something went wrong.

Uh oh!

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

srittau
srittau approved these changes Nov 3, 2022
View reviewed changes
Comment thread stdlib/importlib/abc.pyi
def exec_module(self, module: types.ModuleType) -> None: ...
@staticmethod
def source_to_code(data: bytes | str, path: str = ...) -> types.CodeType: ...
def source_to_code(data: ReadableBuffer | str, path: str = ...) -> types.CodeType: ...

@srittau srittau Nov 3, 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.

Could also be AST, but I don't think that's too important.

Sorry, something went wrong.

Uh oh!

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

All reactions
@srittau
srittau merged commit 4363107 into python:main Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@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.

2 participants

@JelleZijlstra @srittau
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.