python / python/mypy

inner class of NamedTuple triggers "Invalid statement"

Open
#10,337 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-named-tuple
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported case using the NamedTuple definition with a nested Inner class, then trace the NamedTuple definition handling that emits the "Invalid statement" diagnostic. Compare it with the ordinary class example; done means the nested class is accepted without that diagnostic while existing field validation remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.