admin管理员组文章数量:1313163
I posted a question earlier asking how to overload Python methods using generics.
The solution provided is shown below.
from __future__ import annotations
from typing import Literal, overload
class WoodData: ...
class ConcreteData: ...
class Foo[T: Literal["wood", "concrete"]]:
def __init__(self, data_type: T) -> None:
self.data_type = data_type
@overload
def get_data(self: Foo[Literal["wood"]]) -> WoodData: ...
@overload
def get_data(self: Foo[Literal["concrete"]]) -> ConcreteData: ...
def get_data(self):
if self.data_type == "wood":
return WoodData()
return ConcreteData()
foo = Foo("concrete")
x = foo.get_data()
This example works as expected, but the problem arises when we try to call get_data
from a sibling class method.
For example, I add the bar
method below.
from __future__ import annotations
from typing import Literal, overload
class WoodData: ...
class ConcreteData: ...
class Foo[T: Literal["wood", "concrete"]]:
def __init__(self, data_type: T) -> None:
self.data_type = data_type
@overload
def get_data(self: Foo[Literal["wood"]]) -> WoodData: ...
@overload
def get_data(self: Foo[Literal["concrete"]]) -> ConcreteData: ...
def get_data(self):
if self.data_type == "wood":
return WoodData()
return ConcreteData()
def bar(self):
self.get_data()
This gives the typing error:
Cannot access attribute "get_data" for class "Foo[T@Foo]*"
Could not bind method "get_data" because "Self@Foo[T@Foo]" is not assignable to parameter "self"
"Foo[T@Foo]*" is not assignable to "Foo[Literal['wood']]"
Type parameter "T@Foo" is covariant, but "T@Foo" is not a subtype of "Literal['wood']"
Type "Literal['wood', 'concrete']" is not assignable to type "Literal['wood']"
"Literal['concrete']" is not assignable to type "Literal['wood']"
Could not bind method "get_data" because "Self@Foo[T@Foo]" is not assignable to parameter "self"
"Foo[T@Foo]*" is not assignable to "Foo[Literal['concrete']]"
Type parameter "T@Foo" is covariant, but "T@Foo" is not a subtype of "Literal['concrete']"
How can I access the get_data
function from another method without getting a typing error?
I posted a question earlier asking how to overload Python methods using generics.
The solution provided is shown below.
from __future__ import annotations
from typing import Literal, overload
class WoodData: ...
class ConcreteData: ...
class Foo[T: Literal["wood", "concrete"]]:
def __init__(self, data_type: T) -> None:
self.data_type = data_type
@overload
def get_data(self: Foo[Literal["wood"]]) -> WoodData: ...
@overload
def get_data(self: Foo[Literal["concrete"]]) -> ConcreteData: ...
def get_data(self):
if self.data_type == "wood":
return WoodData()
return ConcreteData()
foo = Foo("concrete")
x = foo.get_data()
This example works as expected, but the problem arises when we try to call get_data
from a sibling class method.
For example, I add the bar
method below.
from __future__ import annotations
from typing import Literal, overload
class WoodData: ...
class ConcreteData: ...
class Foo[T: Literal["wood", "concrete"]]:
def __init__(self, data_type: T) -> None:
self.data_type = data_type
@overload
def get_data(self: Foo[Literal["wood"]]) -> WoodData: ...
@overload
def get_data(self: Foo[Literal["concrete"]]) -> ConcreteData: ...
def get_data(self):
if self.data_type == "wood":
return WoodData()
return ConcreteData()
def bar(self):
self.get_data()
This gives the typing error:
Cannot access attribute "get_data" for class "Foo[T@Foo]*"
Could not bind method "get_data" because "Self@Foo[T@Foo]" is not assignable to parameter "self"
"Foo[T@Foo]*" is not assignable to "Foo[Literal['wood']]"
Type parameter "T@Foo" is covariant, but "T@Foo" is not a subtype of "Literal['wood']"
Type "Literal['wood', 'concrete']" is not assignable to type "Literal['wood']"
"Literal['concrete']" is not assignable to type "Literal['wood']"
Could not bind method "get_data" because "Self@Foo[T@Foo]" is not assignable to parameter "self"
"Foo[T@Foo]*" is not assignable to "Foo[Literal['concrete']]"
Type parameter "T@Foo" is covariant, but "T@Foo" is not a subtype of "Literal['concrete']"
How can I access the get_data
function from another method without getting a typing error?
1 Answer
Reset to default 1As @InSync mentioned in the comments we can solve this by adding a third overload.
from __future__ import annotations
from typing import Literal, overload
class WoodData: ...
class ConcreteData: ...
class Foo[T: Literal["wood", "concrete"]]:
def __init__(self, data_type: T) -> None:
self.data_type = data_type
@overload
def get_data(self: Foo[Literal["wood"]]) -> WoodData: ...
@overload
def get_data(self: Foo[Literal["concrete"]]) -> ConcreteData: ...
@overload
def get_data(self) -> WooData | ConcreteData: ...
def get_data(self):
if self.data_type == "wood":
return WoodData()
return ConcreteData()
def bar(self):
self.get_data()
本文标签: pythonOverloaded method invocation causes type check errorStack Overflow
版权声明:本文标题:python - Overloaded method invocation causes type check error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741931153a2405580.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
T: Literal["wood", "concrete"]
rather thanT: (Literal["wood"], Literal["concrete"])
). The comment suggests adding a third overload:@overload
def get_data(self) -> WoodData | ConcreteData: ...
. – InSync Commented Jan 31 at 16:09