Mailing List Archive

return type same as class gives NameError.
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?
--
https://mail.python.org/mailman/listinfo/python-list
Re: return type same as class gives NameError. [ In reply to ]
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: return type same as class gives NameError. [ In reply to ]
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>
--
https://mail.python.org/mailman/listinfo/python-list