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

False positive F821 (Undefined name) for NamedTuple #8803

Closed
cbeytas opened this issue Nov 21, 2023 · 1 comment · Fixed by #8810
Closed

False positive F821 (Undefined name) for NamedTuple #8803

cbeytas opened this issue Nov 21, 2023 · 1 comment · Fixed by #8810
Assignees
Labels
bug Something isn't working

Comments

@cbeytas
Copy link

cbeytas commented Nov 21, 2023

ruff 0.1.6, Python 3.11

import typing
User = typing.NamedTuple('User', **{'name': str, 'password': bytes})
User = typing.NamedTuple('User', name=str, password=bytes)
User = typing.NamedTuple('User', [('name', str), ('password', bytes)])

Those namedtuples should all be functionally equivalent. Why is F821 flagged on the first one?

ruff test.py --isolated
test.py:2:38: F821 Undefined name `name`
test.py:2:51: F821 Undefined name `password`
Found 2 errors.
@charliermarsh charliermarsh added the bug Something isn't working label Nov 21, 2023
@charliermarsh charliermarsh self-assigned this Nov 21, 2023
@tmke8
Copy link
Contributor

tmke8 commented Nov 21, 2023

Worth noting maybe that the syntax used in the first two lines has been deprecated in Python 3.13:

Deprecated since version 3.13, will be removed in version 3.15: The undocumented keyword argument syntax for creating NamedTuple classes (NT = NamedTuple("NT", x=int)) is deprecated, and will be disallowed in 3.15. Use the class-based syntax or the functional syntax instead.

https://docs.python.org/3.13/library/typing.html#typing.NamedTuple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants