Skip to content

Convert importlib.abc to use typing.Protocol #82963

Open

Description

@brettcannon
BPO 38782
Nosy @warsaw, @brettcannon, @ilevkivskyi, @ZackerySpytz, @nanjekyejoannah
PRs
  • bpo-38782: Use typing.Protocol in the importlib.abc module #21245
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-11-12.22:47:40.242>
    labels = ['type-feature', 'library', '3.9']
    title = 'Convert importlib.abc to use typing.Protocol'
    updated_at = <Date 2021-10-20.21:07:21.258>
    user = 'https://github.com/brettcannon'

    bugs.python.org fields:

    activity = <Date 2021-10-20.21:07:21.258>
    actor = 'barry'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2019-11-12.22:47:40.242>
    creator = 'brett.cannon'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38782
    keywords = ['patch']
    message_count = 1.0
    messages = ['356501']
    nosy_count = 5.0
    nosy_names = ['barry', 'brett.cannon', 'levkivskyi', 'ZackerySpytz', 'nanjekyejoannah']
    pr_nums = ['21245']
    priority = 'low'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue38782'
    versions = ['Python 3.9']

    Activity

    1. brettcannon commented on Nov 12, 2019

      @brettcannon
      MemberAuthor

      Now that typing.Protocol exists we should convert the ABCs in importlib over to protocols as the import system functions off of structural typing and not nominal typing.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Nov 12, 2019
    3. transferred this issue fromon Apr 10, 2022
    4. JelleZijlstra commented on Apr 30, 2022

      @JelleZijlstra
      Member

      Protocols are a better conceptual fit for these classes, but wouldn't this change break backwards compatibility? For example, protocols don't support .register().

    5. brettcannon commented on May 1, 2022

      @brettcannon
      MemberAuthor

      Probably, which is why I figured the protocols would be separate since the ABCs also have default implementations that the protocols wouldn't have (I just forgot to update the issue with my latest thinking 😅). I had not decided if they should be in a separate module or just stick them in importlib.abc.

    6. self-assigned this
      on Sep 26, 2024
    7. brettcannon commented on Jun 6, 2025

      @brettcannon
      MemberAuthor

      protocols don't support .register()

      Doesn't using ABCMeta get Protocol subclasses access to register()?

      class Protocol(Generic, metaclass=_ProtocolMeta):

      class _ProtocolMeta(ABCMeta):

    8. ilevkivskyi commented on Jun 12, 2025

      @ilevkivskyi
      Member

      Yes, IIRC protocols a supposed to be "drop in" replacement for ABCs, in particular .register() support is tested here

      def test_protocols_support_register(self):
      That said however, most type-checkers don't support .register() well (or at all).

    9. brettcannon commented on Jun 12, 2025

      @brettcannon
      MemberAuthor

      That said however, most type-checkers don't support .register() well (or at all).

      It would just be for the legacy ABC support. My expectation is the current ABCs will inherit from the protocols, but that most code will type to the protocols themselves and the type checkers will rely on that. But I would still have to either leave the abstract methods that are already there on the ABCs or use @abc.abstractmethod on the protocol classes themselves. Otherwise the protocols might as well be kept separate, in which case it's a question of where to put them in the importlib namespace.

      Since I have not written it down, the protocols/types will be for:

      • sys.meta_path finders
      • sys.path finders
      • Loaders
      • sys.path_hook callables
    10. added
      type-refactorCode refactoring (with no changes in behavior)
      and removed
      type-featureA feature request or enhancement
      on Jun 13, 2025
    11. FFY00 commented on Feb 25, 2026

      @FFY00
      Member

      So, for the protocols, do we keep the ABC names? It would be a bit weird to have two different types with the same name, but coming up with new names for the protocols seems silly.

      Here are the ABC types that would need a protocol:

      • MetaPathFinder
      • PathEntryFinder
      • Loader
      • ResourceLoader
      • InspectLoader
      • ExecutionLoader
      • FileLoader
      • SourceLoader

      Here are the new protocols we would need for the rest of the import machinery:

      • Importer (MetaPathFinder + Loader)
      • PathHook (Callable[[str], Finder])

      That said, ResourceLoader is currently deprecated in favor of TraversableResources. Should we be creating a new protocol for ResourceLoader? If we don't, its subtypes probably also need to be omitted.

      ResourceLoader and its subtypes implement a couple APIs that overlap with the new abstractions:

      • ResourceLoader.get_data -> Traversable
      • InspectLoader.get_source -> Traversable
      • InspectLoader.is_package -> Traversable.is_dir
      • ExecutionLoader.get_filename -> Traversable
      • SourceLoader.set_data -> Traversable

      They could be replaced with a single loader protocol implementing:

      • InspectLoader.get_code
      • TraversableResources.files
      • source_to_code (is static, could be provided standalone)

      And extending Traversable with the following APIs:

      • SourceLoader.path_stats

      I guess this issue converges towards GH-89710.

      With the introduction of Traversable, most of the old APIs became redundant.
      If we are introducing protocols, I am not sure if it makes sense to keep the old PEP 302 APIs.

    12. brettcannon commented on Feb 25, 2026

      @brettcannon
      MemberAuthor

      I think protocols like FinderLike and LoaderLike makes sense. There can be a type for path hooks if we want. So I agree on new names and I'm personally fond of the "*Like" style of naming (hence os.PathLike 😁).

      As for all the PEP 302 protocols, I say skip them.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-refactorCode refactoring (with no changes in behavior)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions