Skip to content

TYP use typed version of namedtuple #40531

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
MarcoGorelli opened this issue Mar 20, 2021 · 7 comments · Fixed by #43107
Closed

TYP use typed version of namedtuple #40531

MarcoGorelli opened this issue Mar 20, 2021 · 7 comments · Fixed by #43107
Assignees
Labels
good first issue Typing type annotations, mypy/pyright type checking

Comments

@MarcoGorelli
Copy link
Member

MarcoGorelli commented Mar 20, 2021

Instead of

OutputKey = collections.namedtuple("OutputKey", ["label", "position"])

we can do

from typing import NamedTuple

class OutputKey(NamedTuple)
    label
    position

The advantage is that like this we can add type hints to the various elements, such as

from typing import NamedTuple

class OutputKey(NamedTuple)
    label: Hashable
    position: int

See https://docs.python.org/3/library/typing.html#typing.NamedTuple for reference

@MarcoGorelli MarcoGorelli added the Typing type annotations, mypy/pyright type checking label Mar 20, 2021
@jreback
Copy link
Contributor

jreback commented Mar 20, 2021

better to use a dataclass

@MarcoGorelli
Copy link
Member Author

Is that always true? Immutability is a nice property, I'd have thought we wouldn't want to lose that

@attack68
Copy link
Contributor

Im also waiting for pandas to adopt TypedDict, for similar reasons. Similar to a data class but also with user input args where a dict is expected and a dataclass wouldn't work becuase the user input cant be a data class.

@MarcoGorelli
Copy link
Member Author

MarcoGorelli commented Jul 16, 2021

Is that always true? Immutability is a nice property, I'd have thought we wouldn't want to lose that

at the time of writing, I wasn't aware that dataclasses could use frozen=True, so that'd do

@franslarsson
Copy link
Contributor

take

@MarcoGorelli
Copy link
Member Author

take

Hi @franslarsson - still working on this?

@franslarsson
Copy link
Contributor

take

Hi @franslarsson - still working on this?

Hi @MarcoGorelli
Sorry for the delay, I'll open a PR for it tomorrow, Wednesday.

franslarsson added a commit to franslarsson/pandas that referenced this issue Aug 19, 2021
MarcoGorelli added a commit that referenced this issue Aug 19, 2021
* TYP: Replace namedtuple with dataclass #40531

* Fixes from pre-commit [automated commit]

* noop

Co-authored-by: MarcoGorelli <[email protected]>
feefladder pushed a commit to feefladder/pandas that referenced this issue Sep 7, 2021
…3107)

* TYP: Replace namedtuple with dataclass pandas-dev#40531

* Fixes from pre-commit [automated commit]

* noop

Co-authored-by: MarcoGorelli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants