Issue 29822: inspect.isabstract does not work on abstract base classes during init_subclass (original) (raw)

Created on 2017-03-15 17:06 by So8res, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)

msg289682 - (view)

Author: Nate Soares (So8res) *

Date: 2017-03-15 17:06

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 https://github.com/python/cpython/pull/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.

msg289788 - (view)

Author: Ivan Levkivskyi (levkivskyi) * (Python committer)

Date: 2017-03-18 01:09

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.

msg289793 - (view)

Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer)

Date: 2017-03-18 05:46

Is this an alternate fix of ?

msg289794 - (view)

Author: Nate Soares (So8res) *

Date: 2017-03-18 07:04

I didn't know about , 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.

msg289823 - (view)

Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer)

Date: 2017-03-18 17:52

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

msg292231 - (view)

Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer)

Date: 2017-04-24 17:06

New changeset fcfe80ec2592fed8b3941c79056a8737abef7d3b by Serhiy Storchaka (Nate) in branch 'master': bpo-29822: Make inspect.isabstract() work during init_subclass. (#678) https://github.com/python/cpython/commit/fcfe80ec2592fed8b3941c79056a8737abef7d3b

msg295313 - (view)

Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer)

Date: 2017-06-07 04:21

New changeset 09b6c0c71ea944f7e8b46998f3ebaf5b9fbe15f6 by Serhiy Storchaka (Nate) in branch '3.6': [3.6] bpo-29822: make inspect.isabstract() work during init_subclass (#1979) https://github.com/python/cpython/commit/09b6c0c71ea944f7e8b46998f3ebaf5b9fbe15f6

msg295314 - (view)

Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer)

Date: 2017-06-07 04:22

Thank you for your contribution Nate.

History

Date

User

Action

Args

2022-04-11 14:58:44

admin

set

github: 74008

2017-06-07 04:22:59

serhiy.storchaka

set

status: open -> closed
resolution: fixed
messages: +

stage: backport needed -> resolved

2017-06-07 04:21:36

serhiy.storchaka

set

messages: +

2017-06-07 00:19:20

So8res

set

pull_requests: + <pull%5Frequest2045>

2017-04-24 17:09:07

serhiy.storchaka

set

stage: patch review -> backport needed
versions: - Python 3.5

2017-04-24 17:06:19

serhiy.storchaka

set

messages: +

2017-03-18 17:52:51

serhiy.storchaka

set

messages: +

2017-03-18 13:57:08

serhiy.storchaka

set

nosy: + ncoghlan

2017-03-18 07:04:13

So8res

set

messages: +

2017-03-18 05:46:49

serhiy.storchaka

set

type: behavior
components: + Library (Lib)
versions: + Python 3.5, Python 3.6
nosy: + yselivanov

messages: +
stage: patch review

2017-03-18 01:09:13

levkivskyi

set

nosy: + serhiy.storchaka, levkivskyi
messages: +

2017-03-15 18:50:05

So8res

set

pull_requests: + <pull%5Frequest556>

2017-03-15 17:06:09

So8res

create