What does this expression mean?

Asked

Viewed 45 times

-1

I’m trying to install and use Minio: https://github.com/minio/minio, and installed the https://github.com/py-pa/django-minio-storage for user as Storage in my djando project. But when saved an image is giving me the following error:

File "/home/developer/.virtualenvs/store/lib/python3.5/site-packages/django/core/files/storage.py", line 358, in get_storage_class
    return import_string(import_path or settings.DEFAULT_FILE_STORAGE)
  File "/home/developer/.virtualenvs/store/lib/python3.5/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/home/developer/.virtualenvs/store/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 661, in exec_module
  File "<frozen importlib._bootstrap_external>", line 767, in get_code
  File "<frozen importlib._bootstrap_external>", line 727, in source_to_code
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/developer/.virtualenvs/store/lib/python3.5/site-packages/minio_storage/storage.py", line 52
    **kwargs,
            ^
SyntaxError: invalid syntax

Which is in that part of the code:

37     def __init__(                                                               
 38         self,                                                                   
 39         minio_client: minio.Minio,                                              
 40         bucket_name: str,                                                       
 41         *,                                                                      
 42         base_url: T.Optional[str] = None,                                       
 43         file_class=None,                                                        
 44         auto_create_bucket: bool = False,                                       
 45         presign_urls: bool = False,                                             
 46         auto_create_policy: bool = False,                                       
 47         policy_type: T.Optional[Policy] = None,                                 
 48         object_metadata: T.Optional[T.Dict[str, str]] = None,                   
 49         backup_format: T.Optional[str] = None,                                  
 50         backup_bucket: T.Optional[str] = None,                                  
 51         assume_bucket_exists: bool = False,                                     
 52         **kwargs,                                                      
 53     ):                       

Then I removed the comma and the error stopped falling there, but it happened in other places:

raise OSError(f"The bucket {self.bucket_name} does not exist")

Here I removed the f before the string and stopped giving error.

But it began to give also in this line:

File "/home/developer/.virtualenvs/store/lib/python3.5/site-packages/minio_storage/storage.py", line 209
    dirs: T.List[str] = []

I’ve never come across such mistakes, my python is in summer 3.5 and the compatibility of the library is Python 3.4-3.6. Are these syntax errors real? And what to say this expression if it exists? dirs: T.List[str] = []

1 answer

3


Although the library compatibility is marked as "from 3.4", this code is using Python 3.6-specific syntax up.

Looking at the project page, in Pypi.python.org, they actually list themselves as "Python 3.4 compatible" - but this is a package maintenance error.

So as for the first error - I didn’t even know it had changed - but I tested it here and it’s a fact: although Python accepts an extra "," whenever it has a comma-separated structure, in the specific context of defining functions, it makes no sense to have any parameter after a parameter marked with ** - precisely because this is the "joker" that will absorb all other arguments passed in a call. Then until version 3.5, this comma after the parameter started with "**" generated a syntax error. I tested here in version 3.6, and the syntax is valid (You will make a mistake if you put anything after the comma - they only allowed the comma to drop, because it turned out to be an exception compared to all the other points in the language in which commas are used as separators).

This code, and the other errors you are getting, has to do with the syntax of "Annotations". Since Python 3.0 it is possible to put the ":" def funcao(nome: Tipo=valor_padrao) -> tipo_de_retorno: in a Python function definition. These values are Python objects that are placed in a special dictionary in the attribute __annotations__ of a function, and do nothing directly in the program, but can be used as documentation, and by external variable type verification tools.

From version 3.6 it is also possible to annotate with types the variables in the class and function field - such as the syntax where the next error occurred. The expression dirs: T.List[str] = [] creates a variable dirs with an empty list inside - []. And along with this, creates for this variable the annotation T.List[str] - that Convention was adopted on the basis of PEP 484, and indicates that this variable will contain a "List" whose elements must all be "str". If the program actually does this, as stated above, it can be checked by an external tool - "mypy". Without this tool, the annotation serves only as a hint of use of the variable: you hit the eye and already see that it should be a list, and the elements should be string. (Note that in this specific case, the author of the code used import typing as T at the beginning of the file, in order to use T.List, T.Optional - instead of having to import all names individually from typing import List, Optional, Union, ... - I adopt this practice as well and find the best solution for the use of annotations)

But all this is only valid from Python 3.6

Better than looking for an old version of the "Minio" library that works with Python 3.5, is you upgrade your project to use a newer Python, like version 3.8 - stable since November 2019, or 3.7, released almost two years ago already. The Python that accompanies the operating system is the of the system, and cannot be updated without breaking everything - but it does not have to limit its project options: you can install newer versions of Python as a user, and use virtual environments for each project. One of the best options for choosing the language version is the Pyenv, but there are others like "Snaps" and etc... (if you are on Windows, simply install a newer Python).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.