如何使用 TypeGuard 缩小 Python 中多个对象字段的类型?
How can I use TypeGuards to narrow types for multiple object fields in Python?
假设我有一个 Foo
对象,它有多个字段,可以是 None
或其他类型。该字段是否None
关系到其他字段是否None
,所以通过检查一个字段,我可以立即知道其他字段是否None
。
我知道这是糟糕的 class 设计,但我无法修改它,因为我正在注释的是其他人的代码。
看起来 PEP 647 中引入的 TypeGuard
是我添加此功能的最佳选择,但我不知道如何将它们专门应用于这种情况。我附上了我用 subclasses 所做的尝试,但它在 MyPy 和 Pyright 中都失败了。
from typing import Optional
from typing_extensions import TypeGuard
class Foo:
"""A class containing the parameters `value`, `values` and `other`. If
`values` is `None` then both the others are not, and if the others are not
then `values` is.
"""
def __init__(self, value: 'int | list[int]', other: Optional[int]) -> None:
is_singular = isinstance(value, int)
self.value = value if is_singular else None
self.values = None if is_singular else value
if is_singular:
assert other is not None
else:
assert other is None
self.other = other
class SingularFoo(Foo):
"""A subclass for an instance of `Foo` where `values` is `None`
"""
def __init__(self, value: int, other: int) -> None:
super().__init__(value, other)
class MultiFoo(Foo):
"""A subclass for an instance of `Foo` where `values` is not `None`
"""
def __init__(self, value: list[int]) -> None:
super().__init__(value, None)
def isFooSingular(f: Foo) -> TypeGuard[SingularFoo]:
"""A type guard that returns whether `f` is singular (meaning that `values`
is `None` and `value` and `other` are not)
"""
return f.value is not None
# Create a singular `Foo`
my_foo = Foo(1, 2)
# Type guard
assert isFooSingular(my_foo)
# After the type guard, both should be considered as `int`
#
# Errors from MyPy:
# * Unsupported operand types for + ("int" and "None")
# * Unsupported operand types for + ("List[int]" and "int")
# * Unsupported operand types for + ("List[int]" and "None")
# * Unsupported operand types for + ("None" and "int")
# * Unsupported left operand type for + ("None")
print(my_foo.value + my_foo.other)
如何修改此代码,以便类型保护执行所需的类型缩小操作。
如果您明确说明 SingularFoo
中的类型怎么办?这似乎让 mypy
高兴:
class SingularFoo(Foo):
"""A subclass for an instance of `Foo` where `values` is `None`"""
value: int
other: int
def __init__(self, value: int, other: int) -> None:
super().__init__(value, other)
假设我有一个 Foo
对象,它有多个字段,可以是 None
或其他类型。该字段是否None
关系到其他字段是否None
,所以通过检查一个字段,我可以立即知道其他字段是否None
。
我知道这是糟糕的 class 设计,但我无法修改它,因为我正在注释的是其他人的代码。
看起来 PEP 647 中引入的 TypeGuard
是我添加此功能的最佳选择,但我不知道如何将它们专门应用于这种情况。我附上了我用 subclasses 所做的尝试,但它在 MyPy 和 Pyright 中都失败了。
from typing import Optional
from typing_extensions import TypeGuard
class Foo:
"""A class containing the parameters `value`, `values` and `other`. If
`values` is `None` then both the others are not, and if the others are not
then `values` is.
"""
def __init__(self, value: 'int | list[int]', other: Optional[int]) -> None:
is_singular = isinstance(value, int)
self.value = value if is_singular else None
self.values = None if is_singular else value
if is_singular:
assert other is not None
else:
assert other is None
self.other = other
class SingularFoo(Foo):
"""A subclass for an instance of `Foo` where `values` is `None`
"""
def __init__(self, value: int, other: int) -> None:
super().__init__(value, other)
class MultiFoo(Foo):
"""A subclass for an instance of `Foo` where `values` is not `None`
"""
def __init__(self, value: list[int]) -> None:
super().__init__(value, None)
def isFooSingular(f: Foo) -> TypeGuard[SingularFoo]:
"""A type guard that returns whether `f` is singular (meaning that `values`
is `None` and `value` and `other` are not)
"""
return f.value is not None
# Create a singular `Foo`
my_foo = Foo(1, 2)
# Type guard
assert isFooSingular(my_foo)
# After the type guard, both should be considered as `int`
#
# Errors from MyPy:
# * Unsupported operand types for + ("int" and "None")
# * Unsupported operand types for + ("List[int]" and "int")
# * Unsupported operand types for + ("List[int]" and "None")
# * Unsupported operand types for + ("None" and "int")
# * Unsupported left operand type for + ("None")
print(my_foo.value + my_foo.other)
如何修改此代码,以便类型保护执行所需的类型缩小操作。
如果您明确说明 SingularFoo
中的类型怎么办?这似乎让 mypy
高兴:
class SingularFoo(Foo):
"""A subclass for an instance of `Foo` where `values` is `None`"""
value: int
other: int
def __init__(self, value: int, other: int) -> None:
super().__init__(value, other)