Author farcat
Recipients Jelle Zijlstra, farcat, gvanrossum, kj, lars2
Date 2021-06-30.14:35:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1625063726.4.0.841323476202.issue44524@roundup.psfhosted.org>
In-reply-to
Content
I have been doing some research, but note that I don't have much experience with the typing module. That said, there seem to be 2 main cases:

 - '_SpecialForm': with instances Any, Union, etc.
 - '_BaseGenericAlias'/'_SpecialGenericAlias': base classes collections classes.

I think '_SpecialForm' can be enhanced to have '__name__' by replacing the '_name' attribute with '__name__'. Maybe add '__qualname__' as well. I cannot say whether there are many more attributes that could be implemented to have the same meaning as in 'type'. The meaning of attributes like '__mro__' seem difficult to define.
Alternatively '__getattr__' could be added (but that might be too much):

def __getattr__(self, attr):
    return getattr(self._getitem, attr)

'_BaseGenericAlias''_SpecialGenericAlias' the '__getattr__' method could perhaps be adapted (or overridden in '_SpecialGenericAlias') as follows, from:

def __getattr__(self, attr):
    # We are careful for copy and pickle.
    # Also for simplicity we just don't relay all dunder names
    if '__origin__' in self.__dict__ and not _is_dunder(attr):
        return getattr(self.__origin__, attr)
    raise AttributeError(attr)

to:

def __getattr__(self, attr):
    if '__origin__' in self.__dict__:
        return getattr(self.__origin__, attr)
    raise AttributeError(attr)

or perhaps:

def __getattr__(self, attr):
    if '__origin__' in self.__dict__ and hasattr(type, attr):
        return getattr(self.__origin__, attr)
    raise AttributeError(attr)

to forward unresolved attribute names to the original class.

I have written some tools and tested some with the above solutions and this seems to solve the missing '__name__' issue and make the typing abc's much more in line with the collection abc's. However I did not do any unit/regression testing (pull the repo, etc.)

tools are attached.
History
Date User Action Args
2021-06-30 14:35:26farcatsetrecipients: + farcat, gvanrossum, Jelle Zijlstra, kj, lars2
2021-06-30 14:35:26farcatsetmessageid: <1625063726.4.0.841323476202.issue44524@roundup.psfhosted.org>
2021-06-30 14:35:26farcatlinkissue44524 messages
2021-06-30 14:35:26farcatcreate