Skip to content
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

Support typing for NamedTuple unpacked variables #251

Closed
oidualc opened this issue Aug 17, 2020 · 13 comments
Closed

Support typing for NamedTuple unpacked variables #251

oidualc opened this issue Aug 17, 2020 · 13 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@oidualc
Copy link

oidualc commented Aug 17, 2020

Currently when a variable is unpacked from a typed NamedTuple, it's type is Any.
It would be nice if the type could be detected as it happens using the dot notation.
Example:

from typing import NamedTuple

class Employee(NamedTuple):
    name: str
    id: int

employee = Employee("Guido", 1)

employee.name  # (variable) name: str
employee.id  # (variable) id: int

(name, id) = employee  # (variable) name: Any, (variable) id: Any
@erictraut
Copy link
Contributor

Thanks for the suggestion. It turns out that I implemented this support last week, and it will be included in the next version of Pylance. See microsoft/pyright#907 for more details.

@erictraut erictraut added the enhancement New feature or request label Aug 17, 2020
@github-actions github-actions bot removed the triage label Aug 17, 2020
@jakebailey jakebailey added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Aug 19, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.8.2, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202082-20-august-2020

@Kein
Copy link

Kein commented Dec 8, 2020

Is it possible to implement type check for short syntax?

Entry = NamedTuple("Entry", size=int, offset=int, numSubs=int, subs=int, hasValue=bool)

This will report Entry.size as type: Any

@erictraut
Copy link
Contributor

Pylance supports this documented form:

Entry = NamedTuple(
    "Entry",
    {
        ("size", int),
        ("offset", int),
        ("numSubs", int),
        ("subs", int),
        ("hasValue", bool),
    },
)

Or the variant documented in PEP 526:

class Entry(NamedTuple):
    size: int
    offset: int
    numSubs: int
    subs: int
    hasValue: bool

@Kein
Copy link

Kein commented Dec 8, 2020

Pylance supports this documented form:

Entry = NamedTuple(
    "Entry",
    {
        ("size", int),
        ("offset", int),
        ("numSubs", int),
        ("subs", int),
        ("hasValue", bool),
    },
)

This does not work, it still reports size as any.

The class approach works but it is too verbose and OOP for 3 lines of code and the type that isnt reusable outside of that context.

@erictraut
Copy link
Contributor

Apologies. It's supposed to be a list, not a dictionary.

Entry = NamedTuple(
    "Entry",
    [
        ("size", int),
        ("offset", int),
        ("numSubs", int),
        ("subs", int),
        ("hasValue", bool),
    ],
)

And you need to instantiate it:

a = Entry(3, 4, 5, 6, True)
a.size

@Kein
Copy link

Kein commented Dec 8, 2020

And you need to instantiate it:

Ah, I see, so that's why it failed, I was populating it a bit differently. A warning for that when type checking enabled would be nice but I believe it is out of scope for Pylance. Thanks.

@Kein
Copy link

Kein commented Dec 8, 2020

Is there any particular reason type annotations for anonymous tuples are not supported?
I would rather prefer:

(var, var2, var) = struct.unpack('<3I', data) # type: (int, int int)

then

var1 = 0 # type: int
var2 = 0 # type: int
var3 = 0 # type: int
(var, var2, var3) = struct.unpack('<3I', data)

@erictraut
Copy link
Contributor

Old-style type annotations in comments were never documented in any PEP or the Python specification. They grew up organically and were eventually replaced by language syntax in Python 3.3 and 3.6. Pylance does support a few simple forms of these old-time comment forms, but we don't plan to add any more at this point. You should use the official language syntax for type annotations.

@gvanrossum
Copy link

Old-style type annotations in comments were never documented in any PEP or the Python specification. They grew up organically and were eventually replaced by language syntax in Python 3.3 and 3.6. Pylance does support a few simple forms of these old-time comment forms, but we don't plan to add any more at this point. You should use the official language syntax for type annotations.

Well, there's this: https://www.python.org/dev/peps/pep-0484/#id41

But I agree that we should encourage annotations for everything except # type: ignore.

@Kein
Copy link

Kein commented Dec 8, 2020

Old-style type annotations in comments were never documented in any PEP or the Python specification. They grew up organically and were eventually replaced by language syntax in Python 3.3 and 3.6. Pylance does support a few simple forms of these old-time comment forms, but we don't plan to add any more at this point. You should use the official language syntax for type annotations.

So what about Python 2.x and very early 3.x that only basically support #type: T->T hints? I understand you dont want to actively support these, but is there any reason to completely abandon backwards compatibility with 2.x?

@erictraut
Copy link
Contributor

Pylance doesn't support Python 2.x. If you need to maintain Python 2.x compatibility in your source base, the JEDI language server is available for your use.

@gvanrossum
Copy link

Another approach would be to specify mypy as the linter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

5 participants