Skip to content

KW_ONLY is only recognized when assigned to _ #11451

Closed

Description

Description

When creating a KW_ONLY field in a dataclass, the typical convention is to attach it to _. However, as Python documentation states, this is simply a convention, and the name is ignored regardless. At runtime, the field effectively doesn't exist.

However, Pyright will not recognize it under anything else, as the KW_ONLY check is locked behind a field specifically being _. This goes against official Python behavior and documentation.

// Is this a KW_ONLY separator introduced in Python 3.10?
if (!isNamedTuple && statement.d.valueExpr.d.value === '_') {
const annotatedType = variableTypeEvaluator();
if (isClassInstance(annotatedType) && ClassType.isBuiltIn(annotatedType, 'KW_ONLY')) {
sawKeywordOnlySeparator = true;
variableNameNode = undefined;
typeAnnotationNode = undefined;
variableTypeEvaluator = undefined;
}
}

As such, it will not recognize which fields should be keyword-only. In some cases, Pyright will not report anything, making the issue invisible. However, it will raise an false positive if KW_ONLY is after fields with defaults, as seen below.

Code example

The real world example that inspired this issue. Another common convention with _ is to assign gettext to it. However, when assigning translatable text into a dataclass field's default, it will conflict with _: KW_ONLY, as now _ has been reassigned (to nothing) in the class scope.

The simplest solution, which seems to generally satisfy linters, is to assign KW_ONLY to __ instead. However, Pyright will complain about this too, stating "Fields without default values cannot appear after fields with default values". This error is reported as simply "reportGeneralTypeIssues", meaning it cannot be selectively silenced. Any call to the class constructor will also now be flagged, as Pyright believes there is a required (ironically positional) argument that is missing.

The issue can currently be silenced by assigning field(default_factory=KW_ONLY) to the KW_ONLY typed field, but this is rather abnormal and not very Pythonic.

from dataclasses import KW_ONLY, dataclass, field
from gettext import gettext

_ = gettext

@dataclass
class PartiallyKWONLY:
    im_positional: int
    __: KW_ONLY  # no error
    im_kwonly: str

@dataclass
class PartiallyKWONLYandDefaults:
    im_positional: int = 67
    __: KW_ONLY  # error: reportGeneralTypeIssues
    im_kwonly: str = _("with a default")

@dataclass
class PartiallyKWONLYbutFullyDefaults:
    im_positional: int = 67
    __: KW_ONLY = field(default_factory=KW_ONLY)  # no error
    im_kwonly: str = _("with a default")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions