• return type same as class gives NameError.

    From Antoon Pardon@21:1/5 to All on Sun Oct 22 17:50:34 2023
    I have the following small module:

    =-=-=-=-=-=-=-=-=-=-=-= 8< =-=-=-=-=-=-=-=-=-=-=-=-=

    from typing import NamedTuple, TypeAlias, Union
    from collections.abc import Sequence

    PNT: TypeAlias = tuple[float, float]

    class Pnt (NamedTuple):
    x: float
    y: float

    def __add__(self, other: PNT) -> Pnt:
    return Pnt(self[0] + other[0], self[1] + other[1])

    =-=-=-=-=-=-=-=-=-=-=-= >8 =-=-=-=-=-=-=-=-=-=-=-=-=

    But when I import this, I get the following diagnostic:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/sisc/projecten/iudex/problem.py", line 10, in <module>
    class Pnt (NamedTuple):
    File "/home/sisc/projecten/iudex/problem.py", line 14, in Pnt
    def __add__(self, other: PNT) -> Pnt:
    ^^^
    NameError: name 'Pnt' is not defined. Did you mean: 'PNT'?


    Can someone explain what I am doing wrong?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to Antoon Pardon via Python-list on Mon Oct 23 05:54:50 2023
    On 23/10/2023 04.50, Antoon Pardon via Python-list wrote:
    I have the following small module:

    =-=-=-=-=-=-=-=-=-=-=-= 8< =-=-=-=-=-=-=-=-=-=-=-=-=

    from typing import NamedTuple, TypeAlias, Union
    from collections.abc import Sequence

    PNT: TypeAlias = tuple[float, float]

    class Pnt (NamedTuple):
        x: float
        y: float

        def __add__(self, other: PNT) -> Pnt:
            return Pnt(self[0] + other[0], self[1] + other[1])

    =-=-=-=-=-=-=-=-=-=-=-= >8 =-=-=-=-=-=-=-=-=-=-=-=-=

    But when I import this, I get the following diagnostic:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/sisc/projecten/iudex/problem.py", line 10, in <module>
        class Pnt (NamedTuple):
      File "/home/sisc/projecten/iudex/problem.py", line 14, in Pnt
        def __add__(self, other: PNT) -> Pnt:
                                         ^^^ NameError: name 'Pnt' is not defined. Did you mean: 'PNT'?


    Can someone explain what I am doing wrong?

    What happens when the advice is followed?

    Not sure why declare type-alias and then don't use it, but if insist on
    using class-name will be making a "forward-reference" (because class has
    not yet been fully defined) - see https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#forward-references

    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Antoon Pardon on Mon Oct 23 09:06:54 2023
    On 22Oct2023 17:50, Antoon Pardon <antoon.pardon@vub.be> wrote:
    I have the following small module:
    =-=-=-=-=-=-=-=-=-=-=-= 8< =-=-=-=-=-=-=-=-=-=-=-=-=
    class Pnt (NamedTuple):
    x: float
    y: float

    def __add__(self, other: PNT) -> Pnt:
    return Pnt(self[0] + other[0], self[1] + other[1])

    When this function is defined, the class "Pnt" has not yet been defined.
    That happens afterwards.

    You want a forward reference, eg:

    def __add__(self, other: PNT) -> "Pnt":

    A type checker will resolve this after the fact, when it encounters the
    string in the type annotation.

    This message:

    NameError: name 'Pnt' is not defined. Did you mean: 'PNT'?

    is unfortunate, because you have a very similar "PNT" name in scope. But
    it isn't what you want.

    Cheers,
    Cameron Simpson <cs@cskk.id.au>

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