Skip to content

Typed NamedTuple with member function generating another instance of class fails at runtime with NameError #485

@lwolfsonkin

Description

@lwolfsonkin

I simplified the code just for proof of concept:

class Hypothesis(NamedTuple):
    logprob: float
    def attach(self) -> Hypothesis:
        return Hypothesis(logprob=4)

While mypy doesn't complain at this, the python interpreter crashes with a NameError saying:

Traceback (most recent call last):
  File "./decode.py", line 31, in <module>
    class Hypothesis(NamedTuple):
  File "./decode.py", line 34, in Hypothesis
    def attach(self) -> Hypothesis:
NameError: name 'Hypothesis' is not defined

Unfortunately, I dropped the return type annotation as a result (which mypy didn't complain about) in order to make it runnable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions