Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: descriptor '__subclasses__' of 'type' object needs an argument #266

Closed
gvanrossum opened this issue Aug 24, 2016 · 8 comments
Closed

Comments

@gvanrossum
Copy link
Member

Behold:

ExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]]

produces a traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "typing.py", line 656, in __getitem__
    return Union[arg, type(None)]
  File "typing.py", line 556, in __getitem__
    dict(self.__dict__), parameters)
  File "typing.py", line 516, in __new__
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
  File "typing.py", line 516, in <genexpr>
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
  File "typing.py", line 1090, in __subclasscheck__
    if super(GenericMeta, self).__subclasscheck__(cls):
  File "/Users/guido/v/lib/python2.7/abc.py", line 179, in __subclasscheck__
    for scls in cls.__subclasses__():
TypeError: descriptor '__subclasses__' of 'type' object needs an argument
@gvanrossum
Copy link
Member Author

FWIW a sufficient repro is

Optional[Type[BaseException]]

I also have a tentative fix -- simply don't inherit Type from type. That makes this problem go away and still passes the test. But I'm not sure how comfortable I am with that. Then again, I don't think subclassing Type[] is valid anyways, nor would using it in an isinstance() call. And I'm willing to give up subclass() calls too.

@ilevkivskyi What do you think?

@ilevkivskyi
Copy link
Member

@gvanrossum I am fine with your fix.

I would say that for every class from collections.abc that also appears in typing it should be possible to use issubclass and probably also isinstance provided it is used without type parameters (the logic here is that Any declares a fallback to dynamic typing and, e.g., Container without a type parameter means Container[Any]). So that one can use issubclass(cls, typing.Container) instead of issubclass(cls, collections.abc.Container). However, the same logic does not necessarily apply to built-in classes. I think, e.g., List[Any] could be treated as list, but equivalently we could just prohibit using List or List[Any] - what is the point of using those while there is much shorter and well known equivalent list.

In any case, we could fine-tune some details later (if necessary) in the process of resolution of #136

@gvanrossum
Copy link
Member Author

OK, that's sound reasoning. I will merge the fix.

gvanrossum added a commit that referenced this issue Aug 24, 2016
jskinn added a commit to jskinn/robot-vision-experiment-framework that referenced this issue Dec 11, 2017
PCManticore added a commit to pylint-dev/pylint that referenced this issue Sep 19, 2018
Due to unforeseen circumstances, readthedocs cannot render our documentation because we
use a certain typing construction that errors on the Python 3.5 version that ReadTheDocs
uses. We need to import pylint in order to construct the dynamic list of features, so the only
choice right now is to remove these annotations from the codebase, although it would be great
if ReadTheDocs would use a more recent version of Python:

python/typing#266
jstasiak added a commit to python-zeroconf/python-zeroconf that referenced this issue Sep 19, 2018
Older typing versions don't allow what we did[1]. We don't really need
to be that precise here anyway.

The error:

    $ python
    Python 3.5.2 (default, Nov 23 2017, 16:37:01)
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import zeroconf
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/scraper/venv/lib/python3.5/site-packages/zeroconf.py", line 320, in <module>
        OptionalExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]]
      File "/usr/lib/python3.5/typing.py", line 649, in __getitem__
        return Union[arg, type(None)]
      File "/usr/lib/python3.5/typing.py", line 552, in __getitem__
        dict(self.__dict__), parameters, _root=True)
      File "/usr/lib/python3.5/typing.py", line 512, in __new__
        for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
      File "/usr/lib/python3.5/typing.py", line 512, in <genexpr>
        for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
      File "/usr/lib/python3.5/typing.py", line 1077, in __subclasscheck__
        if super().__subclasscheck__(cls):
      File "/usr/lib/python3.5/abc.py", line 225, in __subclasscheck__
        for scls in cls.__subclasses__():
    TypeError: descriptor '__subclasses__' of 'type' object needs an argument

Closes #141
Fixes: 1f33c4f ("Introduce some static type analysis to the codebase")

[1] python/typing#266
@devxpy
Copy link

devxpy commented Nov 7, 2018

This is still an issue on Python 3.5, mypy 0.641.

@srittau
Copy link
Collaborator

srittau commented Nov 7, 2018

@devxpy: Do you have an example of code that fails. At least the second examples by @gvanrossum above pass fine for me with Python 3.5.6. What version of Python 3.5 were you using?

@devxpy
Copy link

devxpy commented Nov 7, 2018

@srittau Sorry for not providing this earlier

$ python
Python 3.5.2 (default, Sep 26 2018, 15:59:34) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Optional, Type
>>> Optional[Type[BaseException]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dev/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 649, in __getitem__
    return Union[arg, type(None)]
  File "/home/dev/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 552, in __getitem__
    dict(self.__dict__), parameters, _root=True)
  File "/home/dev/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 512, in __new__
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
  File "/home/dev/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 512, in <genexpr>
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
  File "/home/dev/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 1077, in __subclasscheck__
    if super().__subclasscheck__(cls):
  File "/home/dev/.local/share/virtualenvs/zproc-eGzboTzC/lib/python3.5/abc.py", line 225, in __subclasscheck__
    for scls in cls.__subclasses__():
TypeError: descriptor '__subclasses__' of 'type' object needs an argument
>>> 

Will update to 3.5.6 now..

@emmatyping
Copy link
Contributor

@devxpy, typing will only get upgraded with Python if you are on Python 3.5+. Since this issue was fixed in August 2016, I believe any 3.5 release above 3.5.3 should work.

@twitu
Copy link

twitu commented Jul 11, 2019

I am on an Ubuntu 16.04.6 system, and I am getting this error for python 3.5.6
I compiled python 3.5.6 from https://www.python.org/ftp/python/3.5.6/Python-3.5.6.tgz

andersk added a commit to andersk/PyAPNs2 that referenced this issue Oct 4, 2019
On Ubuntu 16.04, which has Python 3.5.2, this raised `TypeError:
descriptor '__subclasses__' of 'type' object needs an
argument` (python/typing#266).

Signed-off-by: Anders Kaseorg <[email protected]>
Pr0Ger pushed a commit to Pr0Ger/PyAPNs2 that referenced this issue Oct 8, 2019
On Ubuntu 16.04, which has Python 3.5.2, this raised `TypeError:
descriptor '__subclasses__' of 'type' object needs an
argument` (python/typing#266).

Signed-off-by: Anders Kaseorg <[email protected]>
The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue Nov 2, 2019
With Python 3.5.2 (which is shipped with Ubuntu 16.04), placing a typing.Type
into Union/Optional causes:

     TypeError: descriptor '__subclasses__' of 'type' object needs an argument

This was fixed in Python 3.5.3 via:
python/typing#266

Fixes #5099
PythonCoderAS added a commit to praw-dev/praw that referenced this issue Apr 26, 2020
Per python/typing#266, the issue was fixed in versions > 3.5.3.
FFY00 added a commit to FFY00/python-build that referenced this issue May 19, 2021
Bump typing dependency because of python/typing#266.

Signed-off-by: Filipe Laíns <[email protected]>
FFY00 added a commit to FFY00/python-build that referenced this issue May 19, 2021
Bump typing dependency because of python/typing#266.

Signed-off-by: Filipe Laíns <[email protected]>
FFY00 added a commit to pypa/build that referenced this issue May 19, 2021
Bump typing dependency because of python/typing#266.

Signed-off-by: Filipe Laíns <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants