-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Comments
You could create a Protocol that has the tuple operations that you need plus a |
Thank you I found some related issues with info: |
|
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)) |
It's not working as I expect. Since the specific NamedTuple type is unknown, I don't expect 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:
|
As implemented, it's basically a NamedTuple version of |
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 ofAny
isn't too attractive. (Is there something else suitable fromtyping
that I'm missing?)The text was updated successfully, but these errors were encountered: