Skip to content
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

inspect.isabstract does not work on abstract base classes during __init_subclass__ #74008

Closed
Soares mannequin opened this issue Mar 15, 2017 · 8 comments
Closed

inspect.isabstract does not work on abstract base classes during __init_subclass__ #74008

Soares mannequin opened this issue Mar 15, 2017 · 8 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Soares
Copy link
Mannequin

Soares mannequin commented Mar 15, 2017

BPO 29822
Nosy @ncoghlan, @serhiy-storchaka, @1st1, @ilevkivskyi, @Soares
PRs
  • bpo-29822: make inspect.isabstract() work during __init_subclass__ #678
  • [3.6] bpo-29822: make inspect.isabstract() work during __init_subclass__ #1979
  • 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 = <Date 2017-06-07.04:22:59.935>
    created_at = <Date 2017-03-15.17:06:09.728>
    labels = ['3.7', 'type-bug', 'library']
    title = 'inspect.isabstract does not work on abstract base classes during __init_subclass__'
    updated_at = <Date 2017-06-07.04:22:59.934>
    user = 'https://github.com/Soares'

    bugs.python.org fields:

    activity = <Date 2017-06-07.04:22:59.934>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-06-07.04:22:59.935>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2017-03-15.17:06:09.728>
    creator = 'So8res'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29822
    keywords = []
    message_count = 8.0
    messages = ['289682', '289788', '289793', '289794', '289823', '292231', '295313', '295314']
    nosy_count = 5.0
    nosy_names = ['ncoghlan', 'serhiy.storchaka', 'yselivanov', 'levkivskyi', 'So8res']
    pr_nums = ['678', '1979']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue29822'
    versions = ['Python 3.6', 'Python 3.7']

    @Soares
    Copy link
    Mannequin Author

    Soares mannequin commented Mar 15, 2017

    Here's an example test that fails:

    def test_isabstract_during_init_subclass(self):
        from abc import ABCMeta, abstractmethod
    
        isabstract_checks = []
    
        class AbstractChecker(metaclass=ABCMeta):
            def __init_subclass__(cls):
                abstract_checks.append(inspect.isabstract(cls))
    
        class AbstractClassExample(AbstractChecker):
    
            @abstractmethod
            def foo(self):
                pass
    
        class ClassExample(AbstractClassExample):
            def foo(self):
                pass
    
        self.assertEqual(isabstract_checks, [True, False])

    To run the test, you'll need to be on a version of python where bpo-29581 is fixed (e.g., a cpython branch with #527 merged) in order for __init_subclass__ to work with ABCMeta at all in the first place. I have a simple patch to inspect.isabstract that fixes this, and will make a PR shortly.

    @Soares Soares mannequin added the 3.7 (EOL) end of life label Mar 15, 2017
    @ilevkivskyi
    Copy link
    Member

    Serhiy, sorry for a distraction, but it looks like here is one more situation where inspect.isabstract is problematic, similar to what was discussed in http://bugs.python.org/issue29638 recently.

    @serhiy-storchaka
    Copy link
    Member

    Is this an alternate fix of bpo-29638?

    @serhiy-storchaka serhiy-storchaka added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 18, 2017
    @Soares
    Copy link
    Mannequin Author

    Soares mannequin commented Mar 18, 2017

    I didn't know about bpo-29638, and I'm not sure whether my PR fixes it. Looking at that bug, I don't think that my PR would fix it, because I still trust TPFLAGS_IS_ABSTRACT when __abstractmethods__ exists. That said, I'm not clear on how the cache works, so it's possible that my PR would fix 29638.

    My issue appears when one uses py3.6's new __init_subclass__ hook with an ABC. __init_subclass__ is run by type.__new__, which means that, as of py3.6, users can (in a natural/reasonable way) inspect ABCMeta classes before ABCMeta.__new__ finishes executing. I didn't see any reasonable way to have ABCMeta.__new__ finish setting up the ABC before calling super().__new__, so I figured the fix should go into inspect.isabstract. But there may be better solutions I just didn't think of.

    @serhiy-storchaka
    Copy link
    Member

    LGTM, but would be nice if Yury and Nick take a look.

    @serhiy-storchaka
    Copy link
    Member

    New changeset fcfe80e by Serhiy Storchaka (Nate) in branch 'master':
    bpo-29822: Make inspect.isabstract() work during __init_subclass__. (#678)
    fcfe80e

    @serhiy-storchaka
    Copy link
    Member

    New changeset 09b6c0c by Serhiy Storchaka (Nate) in branch '3.6':
    [3.6] bpo-29822: make inspect.isabstract() work during __init_subclass__ (bpo-1979)
    09b6c0c

    @serhiy-storchaka
    Copy link
    Member

    Thank you for your contribution Nate.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants