Skip to content

Convert importlib.abc to use typing.Protocol #82963

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

Open
brettcannon opened this issue Nov 12, 2019 · 6 comments
Open

Convert importlib.abc to use typing.Protocol #82963

brettcannon opened this issue Nov 12, 2019 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir topic-importlib type-refactor Code refactoring (with no changes in behavior)

Comments

@brettcannon
Copy link
Member

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']

    @brettcannon
    Copy link
    Member Author

    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.

    @brettcannon brettcannon added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Nov 12, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @JelleZijlstra
    Copy link
    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().

    @brettcannon
    Copy link
    Member Author

    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.

    @brettcannon
    Copy link
    Member Author

    protocols don't support .register()

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

    class Protocol(Generic, metaclass=_ProtocolMeta):

    class _ProtocolMeta(ABCMeta):

    @ilevkivskyi
    Copy link
    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).

    @brettcannon
    Copy link
    Member Author

    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

    @picnixz picnixz added type-refactor Code refactoring (with no changes in behavior) and removed type-feature A feature request or enhancement 3.9 only security fixes labels Jun 13, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir topic-importlib type-refactor Code refactoring (with no changes in behavior)
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants