python / python/mypy

Mypy rejects valid nested class inside of a NamedTuple class definition

Open
#5,362 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug false-positive priority-2-low topic-named-tuple
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I think I've found a bug in NamedTuple handling:

from enum import Enum
from typing import NamedTuple

class T(NamedTuple):
  class State(Enum):
    A =1
  state:State

print(T.State.A)
print(T(state=T.State.A))

This runs fine in Python3.7 but mypy master head issues the following error:

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

setup.cfg:

[mypy]
python_version = 3.6
cache_dir = _build/mypy_cache
mypy_path = ~/work/pithy:./lambda
check_untyped_defs = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_untyped_defs = False
ignore_missing_imports = False
show_column_numbers = True
show_none_errors = True
strict_boolean = False
strict_optional = True
warn_incomplete_stub = True
warn_no_return = True
warn_redundant_casts = True
warn_return_any = True
warn_unused_configs = True
warn_unused_ignores = True

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

Start by running the minimal Python reproducer against mypy and trace the NamedTuple handling that emits the invalid-statement diagnostic. Confirm that a nested Enum class is accepted inside the NamedTuple definition and that the example type-checks without this error; add regression coverage where the existing NamedTuple tests are located.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.