Skip to content

Allow class-based NamedTuple syntax in semantic analyzer for stubs in pre-3.6 mode #3464

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
gvanrossum opened this issue May 26, 2017 · 5 comments · Fixed by #7646
Closed

Comments

@gvanrossum
Copy link
Member

See python/typeshed#1336 (comment)

Not very important, but would be nice if stubs could use

class A(NamedTuple):
    x: int
    y: str

instead if

A = NamedTuple('A', [('x', int), ('y', str)])
@srittau
Copy link
Contributor

srittau commented Oct 7, 2019

Had this come up in python/typeshed#3278.

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 7, 2019

This seems to be the relevant code (in mypy/semanal_namedtuple.py):

    def check_namedtuple_classdef(
            self, defn: ClassDef) -> Optional[Tuple[List[str], List[Type], Dict[str, Expression]]]:
        """Parse and validate fields in named tuple class definition.

        Return a three tuple:
          * field names
          * field types
          * field default values
        or None, if any of the types are not ready.
        """
        if self.options.python_version < (3, 6):
            self.fail('NamedTuple class syntax is only supported in Python 3.6', defn)
            return [], [], {}
...

Also, there is is_typeshed_stub_file() in SemanticAnalyzerInterface which isn't quite what we need. A similar property could be added for is_stub_file(), and then the rest should be easy.

Anybody want to write a PR?

srittau added a commit to srittau/mypy that referenced this issue Oct 7, 2019
Previously, it was not supported in stubs when checking in Python 2.7 or
3.5 mode.

Closes: python#3464
@srittau
Copy link
Contributor

srittau commented Oct 7, 2019

PR in #7646.

ilevkivskyi pushed a commit that referenced this issue Oct 7, 2019
Previously, it was not supported in stubs when checking in Python 2.7 or
3.5 mode.

Closes: #3464
@gvanrossum
Copy link
Member Author

Do we know if this is supported by type checkers other than mypy?

@srittau
Copy link
Contributor

srittau commented Oct 30, 2019

We checked this in python/typeshed#3321 and pytype, pyre, and pyright, PyCharm, and the Microsoft Language Server for Python all support this.

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

Successfully merging a pull request may close this issue.

3 participants