-
Notifications
You must be signed in to change notification settings - Fork 767
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
Comments
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. |
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 |
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 |
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 |
This does not work, it still reports 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. |
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 |
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. |
Is there any particular reason type annotations for anonymous tuples are not supported? (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) |
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 |
So what about Python 2.x and very early 3.x that only basically support |
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. |
Another approach would be to specify mypy as the linter. |
Currently when a variable is unpacked from a typed
NamedTuple
, it's type isAny
.It would be nice if the type could be detected as it happens using the dot notation.
Example:
The text was updated successfully, but these errors were encountered: