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

namedtuple: Expected string literal for tuple entry name #1832

Closed
laipz8200 opened this issue Sep 18, 2021 · 2 comments
Closed

namedtuple: Expected string literal for tuple entry name #1832

laipz8200 opened this issue Sep 18, 2021 · 2 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@laipz8200
Copy link

Environment data

  • Language Server version: 2021.9.2
  • OS and version: linux x64
  • Python version (and distribution if applicable, e.g. Anaconda): 3.7.3 64-bit
  • python.analysis.indexing: undefined
  • python.analysis.typeCheckingMode: off

Expected behaviour

namedtuple should receive any Iterable[str] type data, but it prompts a problem.

The code runs correctly. In fact, it comes from this website. Is this a recommended practice?

Actual behaviour

Expected string literal for tuple entry name

20210918111446

Logs

Python Language Server Log

[Info  - 10:11:47 AM] Pylance language server 2021.9.2 (pyright eca6f1a5) starting
[Info  - 10:11:47 AM] Server root directory: /home/xxxxxx/.vscode/extensions/ms-python.vscode-pylance-2021.9.2/dist

Code Snippet / Additional information

def namedtuplefetchall(cursor):
    "Return all rows from a cursor as a namedtuple"
    desc = cursor.description
    nt_result = namedtuple('Result', [col[0] for col in desc])
    nt_result = namedtuple('Result', (col[0] for col in desc))
    return [nt_result(*row) for row in cursor.fetchall()]
@erictraut
Copy link
Contributor

You're right, this should not generate an error. If you dynamically specify the entries of a namedtuple, the static type checker won't be able to reason about the names and types of the entries, but it's a legitimate use of namedtuple.

I've updated the code to eliminate the false positive error. It will be fixed in the next release.

@erictraut erictraut added fixed in next version (main) A fix has been implemented and will appear in an upcoming version bug Something isn't working and removed triage labels Sep 18, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.9.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202193-22-september-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 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

3 participants