Python 3.9 使用 OR | 运算符表示联合类型吗?

huangapple go评论154阅读模式
英文:

Python 3.9 use OR | operator for Union types?

问题

自Python版本3.10开始,可以使用X | Y表示联合类型,与Union[X, Y]等效。在Python 3.9上是否有一种简便的方法或解决方法来使用(或忽略)X | Y语法?

我有一个较大的Python软件包,其中广泛使用了X | Y语法进行类型注释。最初它是为Python 3.10+开发的。

现在有一个新的要求,要求软件能在树莓派上运行,但最新的树莓派OS版本(bullseye)只集成了Python 3.9.2,不支持这种语法。

Debian Bookworm最近发布了,它集成了Python 3.11。我预计在接下来的几个月内将会发布基于Bookworm的树莓派OS。因此,实际上只有一个很小的时间窗口,我真的需要Python 3.9的支持。

编辑
我已经添加了from __future__ import annotations导入语句。
该软件使用了pydantic包,异常发生在pydantic尝试解析类型注释时。

英文:

Since Python version 3.10, Unions can be writte as X | Y which is equivalent to Union[X, Y]. Is there some way/workaround to easily use (or just ignore) the X | Y syntax on Python 3.9?

I have a bigger Python package that is making exentsie use of the X | Y syntax for type annotations. It was originally developed for Python 3.10+.

Now there is a new requirement for the software to run on Raspberry Pi, but the latest Raspberry Pi OS release (bullseye) has only Python 3.9.2 integrated which does not support this syntax.

Debian Bookworm has been released recently. It has Python 3.11 integrated. I expect the release of a Bookworm based Raspberry Pi OS within the next few month. So there's really just a small time window where I would really need Python 3.9 support.

EDIT:
I have already added the from __future__ import annotations import.
The software uses the pydantic package and the exception occurs when pydantic is trying to parse the type annotations:

Traceback (most recent call last):
  File "/home/michael/margintest/./margin.py", line 6, in <module>
    from vtcontrol.apps.margintest.app import App
  File "/home/michael/margintest/vtcontrol/apps/margintest/app.py", line 16, in <module>
    from .widgets.steps import StepsDetailView, TestProgressbar
  File "/home/michael/margintest/vtcontrol/apps/margintest/widgets/steps.py", line 6, in <module>
    from ..runner import Runner
  File "/home/michael/margintest/vtcontrol/apps/margintest/runner.py", line 11, in <module>
    from .models.config import Config
  File "/home/michael/margintest/vtcontrol/apps/margintest/models/config.py", line 76, in <module>
    class StepConfig(BaseModel):
  File "/home/michael/margintest/venv/lib/python3.9/site-packages/pydantic/main.py", line 178, in __new__
    annotations = resolve_annotations(namespace.get('__annotations__', {}), namespace.get('__module__', None))
  File "/home/michael/margintest/venv/lib/python3.9/site-packages/pydantic/typing.py", line 400, in resolve_annotations
    value = _eval_type(value, base_globals, None)
  File "/usr/lib/python3.9/typing.py", line 283, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/usr/lib/python3.9/typing.py", line 539, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

答案1

得分: 2

你可以在你的Python文件开头添加以下代码来使用Python 3.9中的新X | Y语法:

from __future__ import annotations

这还会启用其他功能,你可以在PEP 563 – Postponed Evaluation of Annotations中找到更多信息。

英文:

You can add

from __future__ import annotations

at the beginning of your python files to use the new X | Y syntax in Python 3.9. This also enable other features, you can find more information about this in PEP 563 – Postponed Evaluation of Annotations.

huangapple
  • 本文由 发表于 2023年6月29日 15:55:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定