Finding the minimum supported Python version for a package

Question:

I’ve just made a Python package and to help me in managing it, I’m using some relatively new features in Python for typings. Given that this is just typings, will my package work for lower versions?

Here’s an example of the package:

from __future__ import annotations
from typing import List, TypeVar, Callable, Union, overload, Optional, Any
from .another_module import SomeClass

T = TypeVar("T")

class ExampleClass:
    def __init__(self, arr: List[T] = ()):
        self.arr = arr

    @overload
    def find(self, predicate: Callable[[T, Optional[int], Optional[List[T]]], bool]) -> T | None:
        ...

    @overload
    def find(self, predicate: SomeClass) -> T | None:
        ...

    def find(self, predicate: Union[SomeClass, Callable[[T, Optional[int], Optional[List[T]]], bool]]) -> T | None:
        # code goes here
    
    def chainable(self) -> ExampleClass:
        return modified(self)

From what I understand, setting ExampleClass as the return type on chainable is something from version 3.11. There are some other typing things that I’m not 100% sure what version they were added in but I don’t think someone using version 3.6 for example would have access to it.

Considering these are just typing these, can I include versions >=3.6 or do I have to remove all the typings?

Asked By: Spedwards

||

Answers:

annotations was introduced in __future__ in Python 3.7 for PEP 563.

PEP 563 postpones the evaluation of type annotations so they do not happen during definition. This in turns allows later annotations on earlier python version.

So you only need to enforce python >= 3.7.

Answered By: ljmc
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.