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

Convert RankedValue int ranks to Enum. #9526

Merged
merged 1 commit into from
Apr 13, 2020

Conversation

jsirois
Copy link
Contributor

@jsirois jsirois commented Apr 13, 2020

[ci skip-rust-tests]
[ci skip-jvm-tests]

@jsirois
Copy link
Contributor Author

jsirois commented Apr 13, 2020

Depends on #9525 being merged to pass lint shard.


_rank: int

def __new__(cls, rank: int, display: str) -> "Rank":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is your thinking that the complication of providing the display string explicitly is worth it so we can separate the symbolic value from the rank?

Copy link
Contributor Author

@jsirois jsirois Apr 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real thinking. I was working backwards from IntEnum not being a solution since options parsing expects Enum values to be strings. We can't simply switch that code to use Enum names (I naively tried) since we have Enum options that have names different from values. Since having a decoupling between Enum name and the value used in user-facing choices and display seems reasonable, I worked with the status quo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on removing the display name from the value, if possible.

You could get the same display name by calling rank.name. rank.value will give you the int code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my explanation to Benjy.

Ah, my GitHub UI was showing the original unedited comment without the full explanation when I wrote this. The explanation sounds reasonable. Thanks!

Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better! Thank you!

from typing import Any, Dict, Iterator, List, Union


@total_ordering
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With @total_ordering, you should also define __eq__: https://docs.python.org/3/library/functools.html#functools.total_ordering

Are you relying on the superclass's implementation? If so, consider adding this:

def __eq__(other):
  return super().__eq__(other)

While repetitive, it does make it much more explicit that you're meeting the two requirements of @total_ordering.

Copy link
Contributor Author

@jsirois jsirois Apr 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @dataclass is explicit enough, don't you?

Copy link
Contributor

@Eric-Arellano Eric-Arellano Apr 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow. You're not using @dataclass here and are using Enum.

If we were using dataclass, the solution would be @dataclass(order=True).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yeah. But the exact same point applies to an Enum more strongly. By the very nature of a sealed immutable type equal, etc must work right. This is a language independent observation, you don't even need to know Python to assume it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I don't see the value in explicit redundant boilerplate.


_rank: int

def __new__(cls, rank: int, display: str) -> "Rank":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on removing the display name from the value, if possible.

You could get the same display name by calling rank.name. rank.value will give you the int code.

@jsirois
Copy link
Contributor Author

jsirois commented Apr 13, 2020

+1 on removing the display name from the value, if possible.

See my explanation to Benjy. It's not possible without changing generic option parsing code. Further, we do not follow this advice for our "normal" enums where we should be using enum.auto() if you follow this advice consistently.

[ci skip-rust-tests]
[ci skip-jvm-tests]
@jsirois jsirois force-pushed the options/rank/enum branch from c1c6a2a to dade270 Compare April 13, 2020 18:46
@jsirois
Copy link
Contributor Author

jsirois commented Apr 13, 2020

Re-based to pick up TotalOrderingPlugin to fix lint shard.

@benjyw
Copy link
Contributor

benjyw commented Apr 13, 2020

+1 on removing the display name from the value, if possible.

See my explanation to Benjy. It's not possible without changing generic option parsing code. Further, we do not follow this advice for our "normal" enums where we should be using enum.auto() if you follow this advice consistently.

My comment was actually in favor of what this change does. It seems important to distinguish the symbolic value from the rank (which I see has auxiliary information attached to the value). For example, you could imagine changing the rank if we decided for some reason that env vars should be higher in rank than flags. We definitely wouldn't want to change the value in that case. Granted, not a likely case, but it's helpful for working through the design.

@jsirois jsirois merged commit 2f17315 into pantsbuild:master Apr 13, 2020
@jsirois jsirois deleted the options/rank/enum branch April 13, 2020 23:43
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.

3 participants