Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

PEP544, inheriting Protocol show error: Inheriting 'Protocol', which is not a class. #2021

Closed
potens1 opened this issue May 4, 2020 · 7 comments

Comments

@potens1
Copy link

potens1 commented May 4, 2020

Environment data

  • VS Code version: 1.44.2
  • Extension version (available under the Extensions sidebar): 2020.4.76186
  • OS and version: Arch
  • Python version (& distribution if applicable, e.g. Anaconda): python 3.8 (but should produce the same if typing_extensions are installed)
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): pipenv
  • Relevant/affected Python packages and their versions: typing, mypy, typing_extensions
  • Relevant/affected Python-related VS Code extensions and their versions: XXX
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info How to update the language server to the latest stable version vscode-python#3977): same result, tried both
  • Value of the python.languageServer setting: Microsoft

Expected behaviour

No error should be raised since it is defined in PEP Protocol must be used this way

Actual behaviour

error in Problems view:
Inheriting 'Protocol', which is not a class.

Steps to reproduce:

from PEP544

from typing import Protocol

class SupportsClose(Protocol):
    def close(self) -> None:
        ...

[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Nothing relevant


Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)

Hemmm, coredump opening the devtool under arch, too new kernel or glibc, have to investigate if it's electron side or vscode one (but I guess nothing relevant there either)

Please, if you correct the issue, could you do it that even on python version before 3.8, if using typing_extensions it work also ? (typing extensions were used by mypy before 3.8, I think: https://mypy.readthedocs.io/en/latest/protocols.html#simple-user-defined-protocols)

@karrtikr
Copy link

karrtikr commented May 4, 2020

Those errors you see comes directly from the linters. Please paste the Python output panel mentioned in the issue template. What type of linter are you using?
The output panel should contain the commands linters run that results in those erros.

@potens1
Copy link
Author

potens1 commented May 5, 2020

Hi, I use flake8 and mypy, but the error is given by the plugin, I think (no linter named)

Screenshot from 2020-05-05 08-02-56

I'm sorry but it seems the code snippet I gave was wrong:
On python 3.8 this has no error:

try:
    from typing import Protocol
except ImportError:
    pass
    # from typing_extensions import Protocol  # to remove for python3.8


class SupportsClose(Protocol):
    def close(self) -> None:
        ...

This has the errors I gave in the screenshot:

try:
    from typing import Protocol
except ImportError:
    from typing_extensions import Protocol  # to remove for python3.8


class SupportsClose(Protocol):
    def close(self) -> None:
        ...

on python 3.7 with typing_extensions (which predates PEP544, so not strictly to be supported since it requires add of typing_extensions package)
Screenshot from 2020-05-05 08-21-17

Screenshot from 2020-05-05 08-21-31

So, at the end, the bad behavior is only present when the typing_extensions is in the source code (! not always imported, on python3.8, it is not imported)

If the error come from external linter, sorry for the false report, but I was thinking errors and warning coming with "Python(something)" are from the vscode extension.

@potens1
Copy link
Author

potens1 commented May 5, 2020

For the records, remember that Protocols can not be instantiated, their only reason to be there is to provide to code analyzers and IDE clue about what a class must provide to work with duck typing, if you try to create an instance of it, mypy will complain with:
Screenshot from 2020-05-05 08-45-03
and running code will crash with:

Traceback (most recent call last):
  File "bad_behaviour.py", line 10, in <module>
    foo = SupportsClose()
  File "/tmp/test2/lib/python3.7/site-packages/typing_extensions.py", line 1545, in _no_init
    raise TypeError('Protocols cannot be instantiated')
TypeError: Protocols cannot be instantiated

@karrtikr karrtikr transferred this issue from microsoft/vscode-python May 5, 2020
@karrtikr karrtikr removed their assignment May 5, 2020
@karrtikr
Copy link

karrtikr commented May 5, 2020

Looks like it's coming from the Python Language server.

@potens1
Copy link
Author

potens1 commented May 29, 2020

Hello,
This is not to bump the issue, but, since it's coming from the language server, should not it be marked as "bug" ?

@MikhailArkhipov
Copy link

typing is implemented in code rather than actual parsing of the module so this is #535. Bug is when something is supposed to work but doesn't. In this case typing support is not fully implemented by (current) design.

@MikhailArkhipov
Copy link

#535.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants