Skip to content

NamedTuple no longer includes ellipsis for fields with default values #15638

Closed

Description

@ericstarr

Bug Report

In the past, stubgen would indicate NamedTuple fields with default values using an ellipsis. Now it does not indicate which fields have default values in any way. I don't know exactly what version I used to generate my old stubs, but it was about 2 years ago.

To Reproduce

Run stubgen on a python file containing the following

from typing import NamedTuple

class Spam(NamedTuple):
    a: int
    b: int = 1

Expected Behavior

The generated stub should look like

from typing import NamedTuple

class Spam(NamedTuple):
    a: int
    b: int = ...

Actual Behavior

The generated stub looks like

from typing import NamedTuple

class Spam(NamedTuple):
    a: int
    b: int

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9

Activity

  1. sobolevn commented on Jul 15, 2023

    @sobolevn
    Member

    This was not specific for named tuples, it was a general logic for all class level assigns: https://github.com/python/mypy/blob/v0.750/mypy/stubgen.py#L981-L982

    But, I agree that it makes the difference for named tuples:

    from typing import NamedTuple
    
    class A(NamedTuple):
        x: int = 1
    class B(NamedTuple):
        x: int
    
    A()
    B()  # E: Missing positional argument "x" in call to "B"
  2. self-assigned this
    on Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions