Skip to content

Stubgen generates "<ERROR>" if namedtuple's fields are declared in a tuple #4406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hoefling opened this issue Dec 24, 2017 · 2 comments
Closed

Comments

@hoefling
Copy link
Contributor

Contents of spam.py:

from collections import namedtuple

FieldsStr = namedtuple('FieldsStr', 'spam, eggs')
FieldsList = namedtuple('FieldsList', ['spam', 'eggs'])
FieldsTuple = namedtuple('FieldsTuple', ('spam', 'eggs'))

$ stubgen -o . spam yields spam.pyi:

# Stubs for spam (Python 3.6)
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.

from collections import namedtuple

FieldsStr = namedtuple('FieldsStr', 'spam, eggs')

FieldsList = namedtuple('FieldsList', ['spam', 'eggs'])

FieldsTuple = namedtuple('FieldsTuple', <ERROR>)

Is this deliberate?

@gvanrossum
Copy link
Member

gvanrossum commented Dec 24, 2017 via email

@hoefling
Copy link
Contributor Author

hoefling commented Dec 24, 2017

Looks like it's just a case that was forgotten to be handled. In mypy/stubgen.py, only field names in a string or a list are being processed:

def process_namedtuple(self, lvalue: NameExpr, rvalue: CallExpr) -> None:
    ...
    if isinstance(rvalue.args[1], StrExpr):
        items = repr(rvalue.args[1].value)
    elif isinstance(rvalue.args[1], ListExpr):
        list_items = cast(List[StrExpr], rvalue.args[1].items)
        items = '[%s]' % ', '.join(repr(item.value) for item in list_items)
    else:
        items = '<ERROR>'

Seems to be an easy fix even I could implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants