Skip to content

chore: convert namedtuple to dataclass#534

Merged
mjs merged 1 commit intomjs:masterfrom
JohnVillalovos:jlvillal/dataclasses
Sep 10, 2023
Merged

chore: convert namedtuple to dataclass#534
mjs merged 1 commit intomjs:masterfrom
JohnVillalovos:jlvillal/dataclasses

Conversation

@JohnVillalovos
Copy link
Contributor

A dataclass is nicer than a namedtuple. Makes it is easy to add type-hints and also they have the added flexibility to customize things if desired.

@JohnVillalovos
Copy link
Contributor Author

JohnVillalovos commented Aug 28, 2023

The one possible issue I see with this is if people are using these as tuples and using indexing (example variable[1]) instead of dot attributes (example variable.attribute).

A work around for that would be to add a method like this. But not sure...

def __getitem__(self, index):
    return dataclasses.astuple(self)[index]

If we did that I would think to add a deprecation warning to tell people to use attribute access.

@mjs
Copy link
Owner

mjs commented Aug 28, 2023

The one possible issue I see with this is if people are using these as tuples and using indexing (example variable[1]) instead of dot attributes (example variable.attribute).

A work around for that would be to add a method like this. But not sure...

def __getitem__(self, index):
    return dataclasses.astuple(self)[index]

If we did that I would think to add a deprecation warning to tell people to use attribute access.

I wouldn't bother with this because:

  1. I very much doubt that anyone is accessing attributes positionally
  2. The major version number will be incremented for the next release so we're allowed to break compatibility :)

Let's not clutter things up if we don't need to.

@mjs
Copy link
Owner

mjs commented Aug 28, 2023

I'm ok with this PR as it is although it has made me notice all the if TYPE_CHECKING parts which make the code harder to read. Do we really need to leave those in?

@mjs
Copy link
Owner

mjs commented Aug 28, 2023

I should have also said that I like the overall change to dataclass. It's much more readable.

A `dataclass` is nicer than a `namedtuple`. Makes it is easy to add
type-hints and also they have the added flexibility to customize
things if desired.
@JohnVillalovos
Copy link
Contributor Author

I'm ok with this PR as it is although it has made me notice all the if TYPE_CHECKING parts which make the code harder to read. Do we really need to leave those in?

I cleaned those up just now to make less of them. But they are needed unless want the assert() calls to run in live code.

@JohnVillalovos
Copy link
Contributor Author

@mjs Let me know if there is anything you want me to change. Thanks.

@mjs
Copy link
Owner

mjs commented Sep 10, 2023

Thanks for this. Merging...

@mjs mjs merged commit 332935d into mjs:master Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants