Skip to content

Issue using Protocol as a base class with Python 3.5.2 #734

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

Closed
Slonegg opened this issue Jun 24, 2020 · 3 comments
Closed

Issue using Protocol as a base class with Python 3.5.2 #734

Slonegg opened this issue Jun 24, 2020 · 3 comments

Comments

@Slonegg
Copy link

Slonegg commented Jun 24, 2020

Python: 3.5.2
OS: macOS Mojave

Seems that _generic_new implementation from typing_extensions module is outdated or incorrect. When typing_extensions relies on its own implementation of _generic_new I get the following error.

Script to reproduce:

import abc
from typing_extensions import Protocol, runtime_checkable


@runtime_checkable
class Proto(Protocol):
    @abc.abstractmethod
    def foo(self):
        pass


class SomeClass(Proto):
    def __init__(self, some_arg):
        pass

    def foo(self):
        return 5


x = SomeClass(3)

Traceback:

Traceback (most recent call last):
  File "/Users/dikobraz/repro.py", line 20, in <module>
    x = SomeClass(3)
  File "/Users/dikobraz/env-py-3.5-tf-2/lib/python3.5/site-packages/typing_extensions.py", line 1357, in __new__
    return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
  File "/Users/dikobraz/env-py-3.5-tf-2/lib/python3.5/site-packages/typing_extensions.py", line 87, in _generic_new
    return base_cls.__new__(cls, *args, **kwargs)
TypeError: object() takes no parameters

In debugger:
Screen Shot 2020-06-23 at 6 47 37 PM

@gvanrossum
Copy link
Member

Could you try this with a newer version? 3.5 is nearing its end of life, and the latest version is 3.5.9. The latest version for which I can find downloadable binaries is 3.5.4: https://www.python.org/downloads/release/python-354/

If your problem is specifically with 3.5.2, I'm not sure that we can do much for you unless you can come up with a fix (I don't even have 3.5 installed on my machine any more).

@Slonegg
Copy link
Author

Slonegg commented Jun 24, 2020

Thank you @gvanrossum for the quick reply. I've verified that it works with 3.5.9 because it comes with newer typing module and typing_extensions module is just using _generic_new from typing.

Maybe we can just use _generic_new from more recent version of typing module before it was removed completely in this PR (python/cpython#4906):

def _generic_new(base_cls, cls, *args, **kwargs):
    if cls.__origin__ is None:
        return base_cls.__new__(cls)
    else:
        origin = cls._gorg
        obj = base_cls.__new__(origin)
        obj.__init__(*args, **kwargs)
        return obj

However, since Python 3.5 nearing its end of life maybe it's not worth it. We just have 3.5.2 baked in our Docker images. What is your advise? Submit a PR for typing_extensions and see if it works or drop it? Worth noting though that Python 3.5.2 is default for Ubuntu 16.04 which is still widely used for deployment.

@gvanrossum
Copy link
Member

I strongly advise you to upgrade your Python version.

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

2 participants