Use annotations on Pytnon 3.6.5

7.8k views Asked by At

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?

1

There are 1 answers

1
AdriĆ  On

UPDATE:

TLDR: Python 3.6+ reached end of cicle, update to Python >=3.7

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)

A (deprecated) package called future-annotations may be a solution to allow old systems to keep working. Following PyPi instructions:

pip install future-annotations