Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于类型注解和支持的Python版本 about type hint and support version of Python #573

Open
1 of 4 tasks
frostylight opened this issue Aug 9, 2024 · 4 comments
Open
1 of 4 tasks
Labels
API design How to design an API or why design such an API bug Something isn't working

Comments

@frostylight
Copy link
Contributor

Issue type

  • Bug Report
  • Feature Request
  • Help wanted
  • Other

Description
I noticed that the type hints in spikingjelly do not support Python3.6 in fact.
This commit 880f589 added type hints like list[nn.Module], which are fully supported from Python3.9 according to PEP-585.
In Python3.7 to 3.8, you should use them after from __future__ import annotations.
And in 3.6, there should be typing.List[nn.Module].

我注意到,目前spikingjelly的类型标注实际上已经不再支持Python3.6
880f589这个commit中,代码就使用了形如list[nn.Module]的类型标注
根据PEP-585,这种用法在Python3.7往后版本需要from __future__ import annotations才能使用,从3.9开始才被完全支持,事实上在3.6应当使用typing.List[nn.Module]


Solution
Aliases like typing.List were deprecated since Python3.9.

PEP-585: Removal will occur no sooner than Python 3.9’s end of life, scheduled for October 2025.

So type hints in code should vary from versions.

  • For Python3.6, should use aliases from module typing
  • From 3.9, should use builtin type
  • For 3.7 and 3.8, can be one of the following:
    • the same as 3.6
    • from __future__ import annotations, then the same as 3.9

同样根据PEP-585,typing.Listtyping.Tuple等自3.9版本开始被弃用 ,而移除不会早于Python3.9结束生命周期,也就是25年10月
因此代码中应该根据Python版本选择使用的类型注解

  • 对于Python3.6,应当使用来自typing模块的别名
  • 对于Python3.9及之后的版本,应当使用内置类型
  • 介于两者之间的版本
    • 可以与3.6相同
    • 也可以from __future__ import annotations,然后与3.9相同

Discussion

According to Python, version 3.6 and 3.7 is unsupported and end-of-life, and 3.8 is supported until October 2024.
There should be nobody using new version spikingjelly with old version Python.
So new version spikingjelly can stop supporting Python before 3.7 or 3.8.

根据Python官方,3.6以及3.7版本已经结束生命周期,3.8也将在24年10月结束生命周期。
理论上应该没有新项目在使用新版spikingjelly的同时使用Python3.7及之前的版本,3.8也类似
因此新版spikingjelly可以放弃对旧版本Python(3.7或3.8之前的版本)的支持

@fangwei123456 fangwei123456 added bug Something isn't working API design How to design an API or why design such an API labels Aug 9, 2024
@fangwei123456
Copy link
Owner

py版本其实主要取决于pytorch,一般是先装pytorch再装sj框架

目前为了省事,直接把依赖的版本要求给去掉了
https://github.com/fangwei123456/spikingjelly/commits/master/requirements.txt

@frostylight
Copy link
Contributor Author

看了下Python3.6的最后Pytorch版本是1.10.2,3.7是1.13.1
Pytorch实际上直到最新版本还在使用typing.List之类的,所以为了兼容或许需要把现有的类型注解全改成旧式
float | int | None这种写法实际上Python3.10才支持,所以也只能用typing.Uniontyping.Optional

@fangwei123456
Copy link
Owner

也可以不改 等用户报错了再说😅

@frostylight
Copy link
Contributor Author

合理的,那我先改monitor的类型注解
instance的类型标Any实际上等于没标,用isinstance实际上应该标type | tuple[type, ...]之类的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API design How to design an API or why design such an API bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants