3

I'm working on a project where I was asked to code some validations using Chain of Responsibility. I am currently using python 3.9.2 on my machine, but the project on docker is on 3.6.5

This piece of code works nice on my machine, but it breaks on Docker:

from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any, Optional


class Handler(ABC):
    """
    The Handler interface declares a method for building the chain of handlers.
    It also declares a method for executing a request.
    """

    @abstractmethod
    def set_next(self, handler: Handler) -> Handler:
        pass

    @abstractmethod
    def handle(self, request) -> Optional[str]:
        pass

The error that shows is the following:

 from __future__ import annotations
django_1    |     ^
django_1    | SyntaxError: future feature annotations is not defined

Is there a way to make the code work on python 3.6.5?

  • 3
    [`__annotations__` was introduced in Python 3.7](https://www.python.org/dev/peps/pep-0563/): "This change is going to be introduced gradually, starting with a new __future__ import in Python 3.7". Also see https://docs.python.org/3/library/__future__.html – ForceBru Mar 15 '21 at 14:48
  • 2
    *"I am currently using python 3.9.2 on my machine, but the project on docker is on 3.6.5"* - why not make these consistent? Otherwise you're going to hit no end of issues. – jonrsharpe Mar 15 '21 at 14:49
  • @jonrsharpe the code from refactoring.guru is the fragment I posted, is not my own, I'm asking for a way to do that thing on 3.6.5 –  Mar 15 '21 at 14:55
  • 3
    You can read https://www.python.org/dev/peps/pep-0563/ to see what that import would add, and therefore how to work around it (*"when a type hint contains names that have not been defined yet, that definition needs to be expressed as a string literal"*). But you'll likely hit other issues, the point of using Docker is for a more consistent environment not less! – jonrsharpe Mar 15 '21 at 14:58

1 Answers1

1

I've found this answer on github and I'm reposting it in case anyone ends up here like me.

As pointed out by @ForceBru annotations as part of __future__ was introduced with Python 3.7+, so you'll need to add de dependency manually (or move up the python version, which I would recommend)

To add the dependency do the following:

git clone -b legacy_py3.6 https://github.com/QUVA-Lab/e2cnn.git
cd e2cnn
python setup.py install

Adrià
  • 356
  • 2
  • 7