New submission from Erez Zinman <erezinman.program...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue44057>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to