Reputation: 4492
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?
Upvotes: 0
Views: 159
Reputation: 5315
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
.
Upvotes: 1