prefer-typing-namedtuple / R6105ΒΆ

Message emitted:

Prefer 'typing.NamedTuple' over 'collections.namedtuple'

Description:

'typing.NamedTuple' uses the well-known 'class' keyword with type-hints for readability (it's also faster as it avoids an internal exec call). Disabled by default!

Caution

This message is disabled by default. To enable it, add prefer-typing-namedtuple to the enable option.

Problematic code:

from collections import namedtuple

Philosophy = namedtuple(  # [prefer-typing-namedtuple]
    "Philosophy", ("goodness", "truth", "beauty")
)

Correct code:

from typing import NamedTuple


class Philosophy(NamedTuple):
    goodness: str
    truth: bool
    beauty: float

Configuration file:

[MAIN]
load-plugins = pylint.extensions.code_style

Related links:

Note

This message is emitted by the optional 'code_style' checker, which requires the pylint.extensions.code_style plugin to be loaded.

Created by the code_style checker.