Skip to content

Latest commit

 

History

History
51 lines (34 loc) · 2.34 KB

File metadata and controls

51 lines (34 loc) · 2.34 KB

Overview

reportUntypedNamedTuple flags cases where a NamedTuple is defined without explicit type annotations for its fields. This diagnostic helps ensure that all fields in a NamedTuple are properly typed, improving static analysis and code reliability.

Representative Issues

  • #4163: Ensure consistency in the use of type stubs between Pyright's CLI and Pylance settings.
  • #5200: Provide a configuration setting to allow users to customize diagnostic rule severities based on the type checking mode.
  • #6300: Exclude unnecessary folders like .venv to improve performance.
  • #715: Prefer the typing.Dict syntax over the older dict[t, t] syntax for compatibility across Python versions.
  • #4367: Ensure that comments in TOML files use correct line endings and do not contain unsupported control characters.

Examples

Error:

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])  # Fields have no type annotations

Fix — use typing.NamedTuple with explicit types:

from typing import NamedTuple

class Point(NamedTuple):
    x: float
    y: float

Or use the functional form with types:

from typing import NamedTuple

Point = NamedTuple("Point", [("x", float), ("y", float)])

Common Fixes & Workarounds

  1. Add explicit type annotations to all fields in your NamedTuple definitions.
  2. Use the typing.NamedTuple class with type annotations for better static analysis.
  3. Exclude unnecessary folders from analysis to improve performance.
  4. Review the Pyright configuration documentation for details on configuring or disabling this diagnostic.

See Also