Skip to content

problem with ABCMeta.__prepare__ when called after types.new_class #77371

Open

Description

@Ricyteach
mannequin
BPO 33190
Nosy @ilevkivskyi, @Ricyteach
PRs
  • bpo-33190: elucidated types.new_class docstring #6317
  • Files
  • abcmeta_prepare.py: demo of issue
  • 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 2018-03-30.20:43:02.783>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'problem with ABCMeta.__prepare__ when called after types.new_class'
    updated_at = <Date 2018-03-31.02:58:22.949>
    user = 'https://github.com/Ricyteach'

    bugs.python.org fields:

    activity = <Date 2018-03-31.02:58:22.949>
    actor = 'Ricyteach'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2018-03-30.20:43:02.783>
    creator = 'Ricyteach'
    dependencies = []
    files = ['47509']
    hgrepos = []
    issue_num = 33190
    keywords = ['patch']
    message_count = 5.0
    messages = ['314714', '314716', '314717', '314718', '314731']
    nosy_count = 2.0
    nosy_names = ['levkivskyi', 'Ricyteach']
    pr_nums = ['6317']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33190'
    versions = ['Python 3.7', 'Python 3.8']

    Activity

    1. Ricyteach commented on Mar 30, 2018

      Ricyteachmannequin
      MannequinAuthor

      I am pretty sure this is a bug. If not I apologize.

      Say I want to dynamically create a new C class, with base class MyABC (and dynamically assigned abstract method m). This works fine if I use type, but if I use new_class, the keyword argument to the m method implementation gets lost somewhere in the call to ABCMeta.__prepare___.

      I've attached a file to demo. Thanks.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Mar 30, 2018
    3. ilevkivskyi commented on Mar 30, 2018

      @ilevkivskyi
      Member

      This is not a bug, but a misunderstanding:

      • First, ABCMeta doesn't have __prepare__, it is just type.__prepare__
      • Second, the third argument to types.new_class is called kwds for a reason. It is not a namespace like in type but the set of keywords in the equivalent class definition. For example:

      types.new_class('Test', (A, B), {'metaclass': Meta, 'foo': 42})

      id equivalent to

      class Test(A, B, metaclass=Meta, foo=42):
          pass

      If you want to populate the namespace, then you should use the fourth argument exec_body which should be a callable that takes newly created dictionary and populates it with items. For your case it should be:

      C = new_class("C", (MyABC,), {}, exec_body=lambda ns: ns.update(namespace))

      If you want to clarify the corresponding docstring, then please open a PR. Otherwise you can close the issue.

    4. Ricyteach commented on Mar 30, 2018

      Ricyteachmannequin
      MannequinAuthor

      Excellent; thank you very much for the explanation.

      I have never done a PR on a project this size but I'll give it a try.

    5. ilevkivskyi commented on Mar 30, 2018

      @ilevkivskyi
      Member
    6. Ricyteach commented on Mar 31, 2018

      Ricyteachmannequin
      MannequinAuthor

      Thank you; I gave it a go. Hopefully didn't cause too much additional work for someone.

    7. transferred this issue fromon Apr 10, 2022
    8. furkanonder commented on May 13, 2023

      @furkanonder
      Contributor

      @ilevkivskyi I think we can close the problem because the issue is not a bug, it is a misunderstanding.

    9. ilevkivskyi commented on May 13, 2023

      @ilevkivskyi
      Member

      There is still an option to improve the docstring for new_class() but if no-one wants to do this, then we can close.

    10. added
      docsDocumentation in the Doc dir
      pendingThe issue will be closed if no feedback is provided
      and removed
      type-bugAn unexpected behavior, bug, or error
      on Oct 26, 2026
    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

      3.7 (EOL)end of life3.8 (EOL)end of lifedocsDocumentation in the Doc direasypendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directory

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions