Skip to content

typing.Protocol is not supported in Python < 3.8 #916

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
skylander86 opened this issue Apr 9, 2022 · 1 comment · Fixed by #925
Closed

typing.Protocol is not supported in Python < 3.8 #916

skylander86 opened this issue Apr 9, 2022 · 1 comment · Fixed by #925
Labels
bug Something isn't working

Comments

@skylander86
Copy link

Bug report

What's wrong

On Python 3.7, importing django_stubs_ext causes ImportError: cannot import name 'Protocol' from 'typing' (/usr/local/lib/python3.7/typing.py).

This issue only surfaced in django-stubs 1.10.0 and django-stubs-ext 0.4.0.

The workaround in the meantime is to stay on 1.9.0 and 0.3.0.

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/check.py", line 69, in handle
    databases=options['databases'],
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 423, in check
    databases=databases,
  File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 76, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/usr/local/lib/python3.7/site-packages/django/core/checks/templates.py", line 29, in check_string_if_invalid_is_string
    for conf in settings.TEMPLATES:
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/testsite/testsite/settings.py", line 4, in <module>
    import django_stubs_ext
  File "/usr/local/lib/python3.7/site-packages/django_stubs_ext/__init__.py", line 5, in <module>
    from .types import AnyAttrAllowed as AnyAttrAllowed
  File "/usr/local/lib/python3.7/site-packages/django_stubs_ext/types.py", line 1, in <module>
    from typing import Any, Protocol
ImportError: cannot import name 'Protocol' from 'typing' (/usr/local/lib/python3.7/typing.py)

How is that should be

If Python 3.7 compatibility is desired, we shuld use Protocol from typing_extensions instead.

System information

  • OS:
  • python version: 3.7
  • django version: 3.2
  • mypy version: 0.910
  • django-stubs version: 1.10.0
  • django-stubs-ext version: 0.4.0
@skylander86 skylander86 added the bug Something isn't working label Apr 9, 2022
@sobolevn
Copy link
Member

sobolevn commented Apr 9, 2022

Please, send a PR!

sterliakov added a commit to sterliakov/django-stubs that referenced this issue Apr 17, 2022
sobolevn pushed a commit that referenced this issue Apr 28, 2022
* Fix stubs related to `(Async)RequestFactory` and `(Async)Client`

* Revert incorrect removal.

* Allow set as `unique_together`, use shared type alias.

* Revert `Q.__init__` to use only `*args, **kwargs` to remove false-positive with `Q(**{...})`

* Add abstract methods to `HttpResponseBase` to create common interface.

* Remove monkey-patched attributes from `HttpResponseBase` subclasses.

* Add QueryDict mutability checks (+ plugin support)

* Fix lint

* Return back GenericForeignKey to `Options.get_fields`

* Minor fixup

* Make plugin code typecheck with `--warn-unreachable`, minor performance increase.

* Better types for `{unique, index}_together` and Options.

* Fix odd type of `URLResolver.urlconf_name` which isn't a str actually.

* Better types for field migration operations.

* Revert form.files to `MultiValueDict[str, UploadedFile]`

* Compatibility fix (#916)

* Do not assume that `Annotated` is always related to django-stubs (fixes #893)

* Restrict `FormView.get_form` return type to `_FormT` (class type argument). Now it is resolved to `form_class` argument if present, but also errors if it is not subclass of _FormT

* Fix CI (make test runnable on 3.8)

* Fix CI (make test runnable on 3.8 _again_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants