typing: property/setter and lists? [RESOLVED ERRATA]

Chris Angelico rosuav at gmail.com
Thu Nov 3 14:50:49 EDT 2022


On Fri, 4 Nov 2022 at 05:48, Paulo da Silva
<p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt> wrote:
>
> Às 05:32 de 03/11/22, Paulo da Silva escreveu:
> > Às 03:24 de 03/11/22, Paulo da Silva escreveu:
> >> 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?
> >>
> > Changing def foos(self) -> list[int]:  to
> >   def foos(self) -> Union[list[int]]:
> I meant, of course,
> def foos(self) -> Union[list[int],int]:
>

Ohhh! I thought this was triggering a strange quirk of the checker in
some way...

ChrisA


More information about the Python-list mailing list