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

Detect invalid attribute assignment for NamedTuple #1976

Closed
cdce8p opened this issue Oct 24, 2021 · 3 comments
Closed

Detect invalid attribute assignment for NamedTuple #1976

cdce8p opened this issue Oct 24, 2021 · 3 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

@cdce8p
Copy link

cdce8p commented Oct 24, 2021

from typing import NamedTuple

class Car(NamedTuple):
    color: str
    mileage: int
    automatic: bool

car1 = Car('red', 0, True)
car1.mileage = 42  # error

This will result in AttributeError: can't set attribute during runtime. I think it would be nice if pylance / pyright could detect it.

This already happens for __setitem__ calls (although this would raise a TypeError).

car1[1] = 42
"__setitem__" method not defined on type "Car"
@erictraut
Copy link
Contributor

Thanks for the the enhancement request. I've created a tracking bug in the pyright issue tracker.

@erictraut
Copy link
Contributor

This will be included in the next release.

@erictraut erictraut added enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Oct 25, 2021
@bschnurr
Copy link
Member

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

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

3 participants