I have a problem in Python I can work around, but I'd really like to understand why it doesn't work the way I think it should.
I have a class A
which has nested typing.NamedTuple
s B and C. So far so good. The problem arises when the nested named tuple C
is supposed to have an attribute of type A.B
, i.e. be of the type of the first named tuple, like so:
import typing
class A:
class B(typing.NamedTuple):
foo: int
class C(typing.NamedTuple):
bar: B
So using B
does not work with the following error message NameError: name 'B' is not defined
, although following working code suggests it should:
import typing
class A:
class B(typing.NamedTuple):
foo: int
foobar = B(3)
(This code also checks out using mypy, btw)
So I tried:
class C(typing.NamedTuple):
bar: A.B
which checks out in mypy but fails when run with NameError: name 'A' is not defined
So my question is can I even have an attribute of class C be typed to A.B? If so, how do I do that?
Thanks!
PS: The stuff works of course without type hints using classes/collections.NamedTuple, etc.