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

Add a Traversable.read_text() errors parameter #321

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

kurtmckee
Copy link

This PR introduces the following changes, in three commits:

  1. Demonstrate importlib.abc.Traversable.read_text() incompatible with importlib.resources._functional.read_text() usage (Python 3.13) cpython#127012.

    This adds an in-memory finder, loader, and traversable implementation,
    which allows the Traversable protocol and concrete methods to be tested.

    This additional infrastructure demonstrates the original issue,
    but also highlights that the Traversable.joinpath() concrete method
    raises TraversalError which is not getting caught in several places.

  2. Catch TraversalError, raised by Traversable.joinpath().

    Exercising the Traversable protocol's concrete methods
    has highlighted that .joinpath() raises TraversalError,
    which needs to be caught in several places.

    This is primarily resolved within the test suite,
    but implicates the is_resource() function as well.

  3. Resolve a TypeError lurking in the read_text() functional API.

    importlib_resources.read_text() calls the Traversable.read_text()
    concrete method with an errors argument that doesn't exist in the
    method signature, resulting in an TypeError.

    This is resolved by adding an errors parameter to
    Traversable.read_text().

Fixes python/cpython#127012

Note

This PR includes the unrelated fixes posted in #320, which allows the test suite (locally, at least) to pass.

This is a purely mechanical change introduced by `pre-commit run -a`.

* Unused imports in `test_files.py`
* Formatting in `test_functional.py`
This adds an in-memory finder, loader, and traversable implementation,
which allows the `Traversable` protocol and concrete methods to be tested.

This additional infrastructure demonstrates python/cpython#127012,
but also highlights that the `Traversable.joinpath()` concrete method
raises `TraversalError` which is not getting caught in several places.
Exercising the `Traversable` protocol's concrete methods
has highlighted that `.joinpath()` raises `TraversalError`,
which needs to be caught in several places.

This is primarily resolved within the test suite,
but implicates the `is_resource()` function as well.
`importlib_resources.read_text()` calls the `Traversable.read_text()`
concrete method with an `errors` argument that doesn't exist in the
method signature, resulting in an `TypeError`.

This is resolved by adding an `errors` parameter to
`Traversable.read_text()`.

Fixes python/cpython#127012
@kurtmckee kurtmckee changed the title Add a Traversable.readtext() errors parameter Add a Traversable.read_text() errors parameter Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant