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 — Convert importlib.abc to use typing.Protocol · Issue #82963 · python/cpython

Convert importlib.abc to use typing.Protocol #82963

New issue
Copy link
New issue
Copy link
Open
Open
Convert importlib.abc to use typing.Protocol#82963
Copy link
Assignees
brettcannon
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-importlibtype-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)

Description

@brettcannon
brettcannon
opened on Nov 12, 2019
Issue body actions
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
      brettcannon
      on Nov 12, 2019
      MemberAuthor
      More actions

      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. brettcannon
      added
      3.9 (EOL)end of lifeend of life
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancementA feature request or enhancement
      on Nov 12, 2019
    3. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    4. JelleZijlstra commented on Apr 30, 2022

      @JelleZijlstra
      JelleZijlstra
      on Apr 30, 2022
      Member
      More actions

      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
      brettcannon
      on May 1, 2022
      MemberAuthor
      More actions

      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. hauntsaninja
      added
      topic-importlib
      on May 3, 2022
    7. brettcannon
      self-assigned this
      on Sep 26, 2024
    8. brettcannon commented on Jun 6, 2025

      @brettcannon
      brettcannon
      on Jun 6, 2025
      MemberAuthor
      More actions

      protocols don't support .register()

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

      cpython/Lib/typing.py

      Line 2043 in f00512d

      class Protocol(Generic, metaclass=_ProtocolMeta):

      cpython/Lib/typing.py

      Line 1926 in f00512d

      class _ProtocolMeta(ABCMeta):

    9. ilevkivskyi commented on Jun 12, 2025

      @ilevkivskyi
      ilevkivskyi
      on Jun 12, 2025
      Member
      More actions

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

      cpython/Lib/test/test_typing.py

      Line 3765 in f00512d

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

      Reacted by Brett Cannon
    10. brettcannon commented on Jun 12, 2025

      @brettcannon
      brettcannon
      on Jun 12, 2025
      MemberAuthor
      More actions

      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
    11. picnixz
      added
      type-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)
      and removed
      type-featureA feature request or enhancementA feature request or enhancement
      3.9 (EOL)end of lifeend of life
      on Jun 13, 2025
    12. FFY00 commented on Feb 25, 2026

      @FFY00
      FFY00
      on Feb 25, 2026
      Member
      More actions

      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.

    13. brettcannon commented on Feb 25, 2026

      @brettcannon
      brettcannon
      on Feb 25, 2026
      MemberAuthor
      More actions

      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.

      Reacted by Filipe Laíns
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    • @brettcannon
      brettcannon

    Labels

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — 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.