如果我将默认设置为 None,我可以省略 Optional 吗?
Can I omit Optional if I set default to None?
例如:
def foo(bar: int = None):
pass
当我检查 bar
的 type/annotation 时,pycharm 告诉我它是 Optional[int]
。
bar: int = None
看起来比 bar: Optional[int] = None
干净得多,尤其是当您有 10 个以上参数时。
那么我可以简单地省略 Optional
吗?像 mypy 或其他 linters 这样的工具会将这种情况突出显示为 en 错误吗?
看起来 python 本身不喜欢这个想法:
In [1]: from typing import Optional
In [2]: from inspect import signature
In [3]: def foo(a: int = None): pass
In [4]: def bar(a: Optional[int] = None): pass
In [5]: signature(foo).parameters['a'].annotation
Out[5]: int
In [6]: signature(bar).parameters['a'].annotation
Out[6]: typing.Union[int, NoneType]
没有。以前允许省略 Optional
,但后来被删除了。
A past version of this PEP allowed type checkers to assume an optional type when the default value is None [...]
This is no longer the recommended behavior. Type checkers should move towards requiring the optional type to be made explicit.
某些工具可能仍会为旧版支持提供旧行为。即使是这样,也不要指望它在未来得到支持。
具体而言,mypy 默认仍然支持隐式 Optional
,但明确指出这可能会在未来发生变化:
Optional types and the None type (mypy v0.782)
[...] You can use the --no-implicit-optional
command-line option to stop treating arguments with a None default value as having an implicit Optional[...] type. It’s possible that this will become the default behavior in the future.
中跟踪此行为的弃用
PEP 484 中定义的标准的先前版本允许这样做。
但是,最新版本在 Union section 中声明如下:
A past version of this PEP allowed type checkers to assume an optional
type when the default value is None, as in this code:
def handle_employee(e: Employee = None): ...
This would have been treated as equivalent to:
def handle_employee(e: Optional[Employee] = None) -> None: ...
This is no longer the recommended behavior. Type checkers should move
towards requiring the optional type to be made explicit.
对此我很高兴。在我看来,它实际上看起来很刺耳。
例如:
def foo(bar: int = None):
pass
当我检查 bar
的 type/annotation 时,pycharm 告诉我它是 Optional[int]
。
bar: int = None
看起来比 bar: Optional[int] = None
干净得多,尤其是当您有 10 个以上参数时。
那么我可以简单地省略 Optional
吗?像 mypy 或其他 linters 这样的工具会将这种情况突出显示为 en 错误吗?
看起来 python 本身不喜欢这个想法:
In [1]: from typing import Optional
In [2]: from inspect import signature
In [3]: def foo(a: int = None): pass
In [4]: def bar(a: Optional[int] = None): pass
In [5]: signature(foo).parameters['a'].annotation
Out[5]: int
In [6]: signature(bar).parameters['a'].annotation
Out[6]: typing.Union[int, NoneType]
没有。以前允许省略 Optional
,但后来被删除了。
A past version of this PEP allowed type checkers to assume an optional type when the default value is None [...]
This is no longer the recommended behavior. Type checkers should move towards requiring the optional type to be made explicit.
某些工具可能仍会为旧版支持提供旧行为。即使是这样,也不要指望它在未来得到支持。
具体而言,mypy 默认仍然支持隐式 Optional
,但明确指出这可能会在未来发生变化:
中跟踪此行为的弃用Optional types and the None type (mypy v0.782)
[...] You can use the
--no-implicit-optional
command-line option to stop treating arguments with a None default value as having an implicit Optional[...] type. It’s possible that this will become the default behavior in the future.
PEP 484 中定义的标准的先前版本允许这样做。
但是,最新版本在 Union section 中声明如下:
A past version of this PEP allowed type checkers to assume an optional type when the default value is None, as in this code:
def handle_employee(e: Employee = None): ...
This would have been treated as equivalent to:
def handle_employee(e: Optional[Employee] = None) -> None: ...
This is no longer the recommended behavior. Type checkers should move towards requiring the optional type to be made explicit.
对此我很高兴。在我看来,它实际上看起来很刺耳。