简体   繁体   English

查找 package 的最低支持 Python 版本

[英]Finding the minimum supported Python version for a package

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.我刚刚制作了一个 Python package 并帮助我管理它,我在 Python 中使用了一些相对较新的功能来进行打字。 Given that this is just typings, will my package work for lower versions?鉴于这只是打字,我的 package 是否适用于较低版本?

Here's an example of the package:这是 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.据我了解,将chainable设置为ExampleClass的返回类型是 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.还有一些其他的输入内容,我不是 100% 确定它们被添加到哪个版本中,但我认为使用 3.6 版的人不会访问它。

Considering these are just typing these, can I include versions >=3.6 or do I have to remove all the typings?考虑到这些只是输入这些,我可以包含版本 >=3.6 还是我必须删除所有输入?

annotations was introduced in __future__ in Python 3.7 for PEP 563 .PEP 563的 Python 3.7 中的__future__中引入了annotations

PEP 563 postpones the evaluation of type annotations so they do not happen during defition. PEP 563 推迟了类型注释的评估,因此它们不会在定义期间发生。 This in turns allows later annotations on earlier python version.这反过来又允许在早期的 python 版本上进行后续注释。

So you only need to enforce python >= 3.7 .所以你只需要强制执行python >= 3.7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM