Skip to content

Cannot import Anchor from importlib.resources #113238

Description

@nsoranzo

Bug report

Bug description:

The Python 3.12 documentation of the standard library module importlib.resources describes the importlib.resources.Anchor class, which is used for the type annotation of the files() function.
But in reality Anchor is not available to import from importlib.resources, it can only by found in importlib.resources._common as an alias for Package (which is instead available from importlib.resources).

To reproduce:

>>> from importlib.resources import Anchor
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'Anchor' from 'importlib.resources' (/usr/lib/python3.12/importlib/resources/__init__.py)

Expected result:
No ImportError

Use case:
To add type annotation to a function that receives an anchor and passes it to files().

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added a commit that references this issue on Jan 7, 2024
  2. mikeziminio commented on Jan 7, 2024

    @mikeziminio
    Contributor

    @jaraco Jason, I have changed the code of importlib/resources/__init__.py to match the documentation (Anchor import).

    Is my PR correct or is the bug not in the code, but in the documentation and should we replace Anchor with Package in the documentation?

  3. added a commit that references this issue on Jan 7, 2024
  4. jaraco commented on Jan 7, 2024

    @jaraco
    Member

    My intention was that Anchor (or Package) would not need to be exported, that it would serve as documentation but not be part of the API (and thus not depended upon by callers).

    I'm not particularly experienced with type annotations in APIs, so I'm not sure what the best practice is here.

    I'd be interested to know more about the caller's use case. Why is the caller passing an Anchor and not simply one of the specific types? What should happen should this package wish to evolve the meaning of Anchor; should the caller's type evolve with it automatically or should the caller maintain their own type?

  5. nsoranzo commented on Jan 8, 2024

    @nsoranzo
    ContributorAuthor

    Thanks both for looking into this!

    My intention was that Anchor (or Package) would not need to be exported, that it would serve as documentation but not be part of the API (and thus not depended upon by callers).

    I'm not particularly experienced with type annotations in APIs, so I'm not sure what the best practice is here.

    I'd say Anchor is already part of the API since it's used in the type annotation of public methods (e.g. https://github.com/python/importlib_resources/blob/main/importlib_resources/_common.py#L54 ).

    I'd be interested to know more about the caller's use case. Why is the caller passing an Anchor and not simply one of the specific types? What should happen should this package wish to evolve the meaning of Anchor; should the caller's type evolve with it automatically or should the caller maintain their own type?

    Our use case is to type annotate a couple of wrapper methods around importlib.resources.files(), see https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy/util/resources.py .
    Given these are wrappers around the standard library, if something changes we will update them to shield the rest of the application from the changes (it's what they are there for).

  6. FFY00 commented on Jan 16, 2024

    @FFY00
    Member

    My intention was that Anchor (or Package) would not need to be exported, that it would serve as documentation but not be part of the API (and thus not depended upon by callers).

    I'm not particularly experienced with type annotations in APIs, so I'm not sure what the best practice is here.

    Since it's documented as a specific type, it should be exposed. If we don't want to expose a new type, we should probably just use Union[str, ModuleType] directly in the documentation.

    I agree that the current documentation reads like Anchor is a specific type exposed by the module.

  7. added a commit that references this issue on Jan 16, 2024
  8. added a commit that references this issue on Jan 22, 2024
  9. added a commit that references this issue on Feb 11, 2024
  10. erlend-aasland commented on Mar 12, 2024

    @erlend-aasland
    Contributor

    Can this be closed, or are there more actionable items?

  11. jaraco commented on Mar 13, 2024

    @jaraco
    Member

    I believe it can be closed. Comment or re-open if I've missed something.

  12. added a commit that references this issue on Sep 2, 2024
  13. added a commit that references this issue on Feb 24, 2025
  14. added a commit that references this issue on Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions