This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Base class of generic type has wrong `cls` argument in classmethods
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, kflu, levkivskyi
Priority: normal Keywords:

Created on 2019-09-27 18:26 by kflu, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg353386 - (view) Author: Kefei Lu (kflu) Date: 2019-09-27 18:26
This is a new regression in Python3.7.


Create the following file as `test.py`

```
# test.py

import typing as t


T = t.TypeVar("T")


class BaseOfGeneric:

    @classmethod
    def f(cls):
        # when called from an instantiated generic type, e.g.,
        # `MyList[int]`, expect `cls` to be the GenericAlias with its type
        # argument already insteantiated
        print(f"current class is {cls}")
        print(f"current class's type: {type(cls)}")


class MyList(t.List[T], BaseOfGeneric):
    pass


MyIntList = MyList[int]

MyIntList.f()
```

Run with Python3.6:

    >>> python3.6 ./test.py 
    current class is __main__.MyList[int]
                                     ^^^ as expected

    current class's type: <class 'typing.GenericMeta'>

EXPECTED BEHAVIOR: The outcome is expected: `cls` of `BaseOfGeneric.f` should be `MyList` **with** type argument `int`


However this is wrong in Python3.7:

    >>> python3.7 ./test.py 
    current class is <class '__main__.MyList'>
                             ^^^^^^^^^^^^^^^ type argument is LOST !!!

    current class's type: <class 'type'>

Note that `cls` of `BaseOfGeneric.f` has lost the type argument `int`! It is not expected.
msg353429 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-09-28 00:07
For performance reasons the implementation of generics changed completely in 3.7. I don't think we can restore the behavior you observed in 3.6.
msg353473 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-09-28 19:51
Yes, it is unfortunately hard to support with the new design. Also note that this was previously discussed at https://github.com/python/typing/issues/629. I think we can close this, since the other issue has more context.
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82479
2019-09-28 19:51:26levkivskyisetstatus: open -> closed
resolution: duplicate
messages: + msg353473

stage: resolved
2019-09-28 00:07:52gvanrossumsetmessages: + msg353429
2019-09-27 21:58:17ned.deilysetnosy: + gvanrossum, levkivskyi
2019-09-27 18:26:36kflucreate