• typing: property/setter and lists?

    From Paulo da Silva@21:1/5 to All on Thu Nov 3 03:24:19 2022
    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?

    Thanks.
    Paulo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paulo da Silva@21:1/5 to All on Thu Nov 3 05:32:18 2022
    À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]]:
    fixes the problem.
    Not so elegant, however!

    Regards.
    Paulo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to Paulo da Silva on Thu Nov 3 20:55:43 2022
    On 03/11/2022 16.24, Paulo da Silva wrote:
    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]")


    To help us to help you please copy-paste the *exact* message -
    especially which line is in-question.


    The above code passes without complaint in PyCharm, and executes.


    However, the general rule?convention would be to establish type at the
    first mention of the identifier, eg

    def __init__(self):
    self.__foos:list[int] = 5 * [ 0 ]
    # or [ 0, 0, 0, 0, 0, ]


    Why the "__i", and not "i", or "_"?
    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter Otten@21:1/5 to Paulo da Silva on Thu Nov 3 09:53:20 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.
    """

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paulo da Silva@21:1/5 to All on Thu Nov 3 18:37:43 2022
    Às 18:16 de 03/11/22, Chris Angelico escreveu:
    On Fri, 4 Nov 2022 at 05:03, Paulo da Silva <p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt> wrote:
    Changing def foos(self) -> list[int]: to
    def foos(self) -> Union[list[int]]:
    fixes the problem.
    Not so elegant, however!

    Wait, what?!

    Union[X, Y] means "X or Y"
    Union[X] means "X, but don't complain if it's a @property".

    Is that how it goes?
    I'm sorry. A bad transposition of the text. I meant
    def foos(self) -> Union[list[int],int]:

    Regards.
    Paulo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to p_d_a_s_i_l_v_a_ns@nonetnoaddress.p on Fri Nov 4 05:16:59 2022
    On Fri, 4 Nov 2022 at 05:03, Paulo da Silva <p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt> wrote:
    Changing def foos(self) -> list[int]: to
    def foos(self) -> Union[list[int]]:
    fixes the problem.
    Not so elegant, however!

    Wait, what?!

    Union[X, Y] means "X or Y"
    Union[X] means "X, but don't complain if it's a @property".

    Is that how it goes?

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paulo da Silva@21:1/5 to All on Thu Nov 3 18:35:21 2022
    À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]:

    Sorry.
    Paulo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paulo da Silva@21:1/5 to All on Thu Nov 3 18:42:35 2022
    Às 07:55 de 03/11/22, dn escreveu:
    On 03/11/2022 16.24, Paulo da Silva wrote:
    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]")


    To help us to help you please copy-paste the *exact* message -
    especially which line is in-question.


    The above code passes without complaint in PyCharm, and executes.


    However, the general rule?convention would be to establish type at the
    first mention of the identifier, eg

    def __init__(self):
        self.__foos:list[int] = 5 * [ 0 ]
    # or [ 0, 0, 0, 0, 0, ]


    Why the "__i", and not "i", or "_"?
    Just because of my personal taste.
    I know that __ means (for me) a "not used anymore" var and i is an indexer/counter/...

    Regards.
    Paulo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to p_d_a_s_i_l_v_a_ns@nonetnoaddress.p on Fri Nov 4 05:50:49 2022
    On Fri, 4 Nov 2022 at 05:48, Paulo da Silva <p_d_a_s_i_l_v_a_ns@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to Chris Angelico on Fri Nov 4 20:52:24 2022
    On 04/11/2022 07.50, Chris Angelico wrote:
    On Fri, 4 Nov 2022 at 05:48, Paulo da Silva <p_d_a_s_i_l_v_a_ns@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...


    Yes, these personal styles (?quirks) are off-putting to others.

    Plus "_" means (more or less) "not used anymore"
    and for most of us, a weak-identifier name such as "i" is indeed "an indexer/counter/... "
    Accordingly, the question becomes: why not follow the crowd - unless you
    tell me that this is a team/company convention?

    ...and whilst I'm griping,
    "To help us to help you please copy-paste the *exact* message"
    has been followed by:
    "I'm sorry. A bad transposition of the text."

    copy-paste for the win!
    (and to keep others happy to spend their voluntary time helping you -
    more working-with-the-team thinking to consider - please)
    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paulo da Silva@21:1/5 to All on Fri Nov 4 16:33:58 2022
    Às 07:52 de 04/11/22, dn escreveu:
    On 04/11/2022 07.50, Chris Angelico wrote:
    On Fri, 4 Nov 2022 at 05:48, Paulo da Silva
    <p_d_a_s_i_l_v_a_ns@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...


    Yes, these personal styles (?quirks) are off-putting to others.

    Plus "_" means (more or less) "not used anymore"
    and for most of us, a weak-identifier name such as "i" is indeed "an indexer/counter/... "
    Thank you for the suggestions.
    BTW, I am not a python pro programmer. I use it as a tool as many other
    tools and some other (few) languages.

    ..
    ...and whilst I'm griping,
    "To help us to help you please copy-paste the *exact* message"
    has been followed by:
    "I'm sorry. A bad transposition of the text."

    copy-paste for the win!
    (and to keep others happy to spend their voluntary time helping you -
    more working-with-the-team thinking to consider - please)
    The full original message was there. Seemed to me that that was obvious considering the simplicity of the subject and the illustrative toy example. Anyway, I'm sorry.

    Thank you.
    Paulo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)