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