typing: property/setter and lists?
Peter Otten
__peter__ at web.de
Thu Nov 3 04:53:20 EDT 2022
On 03/11/2022 04:24, Paulo da Silva wrote:
> Hi!
>
> And a typing problem again!!!
> _______________________________________
> class C:
> def __init__(self):
> self.__foos=5*[0]
>
> @property
> def foos(self) -> list[int]:
> return self.__foos
>
> @foos.setter
> def foos(self,v: int):
> self.__foos=[v for __i in self.__foos]
>
> c=C()
> c.foos=5
> print(c.foos)
> _______________________________________
>
> mypy gives the following error:
> error: Incompatible types in assignment (expression has type "int",
> variable has type "List[int]")
>
> How do I turn around this?
Quoting https://github.com/python/mypy/issues/3004:
"""
gvanrossum commented on Mar 15, 2017
IIRC we debated a similar issues when descriptors were added and decided
that it's a perverse style that we just won't support. If you really
have this you can add # type: ignore.
"""
More information about the Python-list
mailing list