[New-bugs-announce] [issue44057] Inconsitencies in `__init_subclass__` in a generic class
Erez Zinman
report at bugs.python.org
Thu May 6 07:48:19 EDT 2021
New submission from Erez Zinman <erezinman.programmer at gmail.com>:
The following behavior was witnessed in v3.6 & v3.8.
When deriving from a Generic base class, there's an inconsistency in the order of operation within the `__new__()` function between the case of deriving WITH generic-argument specification and WITHOUT.
It might be best explained in the following example:
```
import typing
T = typing.TypeVar('T')
class Base(typing.Generic[T]):
some_attribute: typing.Any
def __init_subclass__(cls, **kwargs):
assert hasattr(cls, 'some_attribute')
class Class1(Base): # OK
some_attribute = 123
class Class2(Base[int]): # AssertionError
some_attribute = 123
```
In this examples, the base class implements `__init_subclass__` to ensure that sublclasses define an attribute. In the case of `Class1`, the class derives without specifying the type-arguments for the class. In that case, the `__init_subclass__` is called after the `some_attribute` is defined. In the second case, however, because I pass the `int` type-argument to the base-class, for some reason `__init_subclass__` is called BEFORE the class' definition.
----------
components: Interpreter Core, Library (Lib)
messages: 393085
nosy: erezinman
priority: normal
severity: normal
status: open
title: Inconsitencies in `__init_subclass__` in a generic class
versions: Python 3.6, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44057>
_______________________________________
More information about the New-bugs-announce
mailing list