1

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.NamedTuples 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.

GoDontKnow
  • 13
  • 3
  • Executing your code gives the error `NameError: name 'typing' is not defined` –  Feb 01 '22 at 11:51
  • Sorry, I was missing one space on the import statement and it did not make into the code block. Fixed. – GoDontKnow Feb 01 '22 at 12:05

1 Answers1

0

Related to : How do I type hint a method with the type of the enclosing class?

Here you have a slight variant where the type of your C.bar is of a nested class of the parent of C, but it still ends the same way : Python gets lost.

You can add double-quotes to your type-hint (bar: "B") so that it will not cause a NameError. It would have to be bar: "A.B" for mypy to pass.
Or you can add at the very top of your file from __future__ import annotations.

Lenormju
  • 4,078
  • 2
  • 8
  • 22