python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.22k stars 2.78k forks source link

inner class of NamedTuple triggers "Invalid statement" #10337

Open Lambda-Logan opened 3 years ago

Lambda-Logan commented 3 years ago

This will show an error.

from typing import NamedTuple

class Name(NamedTuple):
    name: str = ""

    class Inner:
        pass

>>> error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"

However, this works fine:

class Name:
    name: str = ""

    class Inner:
        pass

mypy 0.761 Python 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0] on linux

Akuli commented 3 years ago

Are you sure this is a bug? The NamedTuple docs say "docstrings and methods", without a mention of nested classes.

JelleZijlstra commented 3 years ago

At runtime it does work in 3.9, but in 3.6 the inner class just disappears. I'm not sure that's on purpose. I added runtime support for NamedTuple methods in 3.6 I believe, but perhaps a change since then made nested classes also work.