On 22Oct2023 17:50, Antoon Pardon <antoon.par...@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 <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to