Skip to content

literal NamedTuple as a type #10294

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

Closed
belm0 opened this issue Apr 8, 2021 · 7 comments
Closed

literal NamedTuple as a type #10294

belm0 opened this issue Apr 8, 2021 · 7 comments

Comments

@belm0
Copy link

belm0 commented Apr 8, 2021

Pitch

I have an API that accepts dynamically created NamedTuple types. I'd at least like to specify NamedTuple as a type hint, and have mypy allow any attribute access on top of Tuple operations. The infinitely wider scope of Any isn't too attractive. (Is there something else suitable from typing that I'm missing?)

error: "NamedTuple" has no attribute "x"  [attr-defined]
@belm0 belm0 added the feature label Apr 8, 2021
@JelleZijlstra
Copy link
Member

You could create a Protocol that has the tuple operations that you need plus a def __getattr__(self, attr: str) -> Any:. NamedTuple by itself doesn't make sense as a type because it's not actually a type.

@belm0
Copy link
Author

belm0 commented Apr 8, 2021

Thank you

I found some related issues with info:

@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 4, 2022

I think @JelleZijlstra's suggestion is the best realistic solution here :/

@JelleZijlstra
Copy link
Member

Actually, we made a change a while ago that now allows NamedTuple as a type.

@AlexWaygood
Copy link
Member

Actually, we made a change a while ago that now allows NamedTuple as a type.

Sorry, yes, you're right. This now works — hooray!

from typing import NamedTuple

def func(x: NamedTuple) -> None: ...

class MyTuple(NamedTuple):
    baz: int

func(MyTuple(5))

@belm0
Copy link
Author

belm0 commented Apr 4, 2022

It's not working as I expect. Since the specific NamedTuple type is unknown, I don't expect attr-defined errors from mypy on attribute access.

def func(x: NamedTuple) -> None:
    y = x.foo  # error: "NamedTuple" has no attribute "foo"

class MyTuple(NamedTuple):
    baz: int

func(MyTuple(5))

Rather, I think what has been implemented so far is attribute checking for known types derived from NamedTuple:

class MyTuple(NamedTuple):
    baz: int

def func(x: MyTuple) -> None:
    y = x.baz
    z = x.bar  # error: "MyTuple" has no attribute "bar"

func(MyTuple(5))

original request:

I'd at least like to specify NamedTuple as a type hint, and have mypy allow any attribute access on top of Tuple operations. The infinitely wider scope of Any isn't too attractive.

@JelleZijlstra
Copy link
Member

As implemented, it's basically a NamedTuple version of object, allowing operations valid on all NamedTuples, while you seem to want a NamedTuple version of Any. That's obviously unsafe and doesn't feel worth an addition to the type system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants