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

chore: make ignore reason human readable in text output #1031

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gg-mmill
Copy link
Contributor

Context

Following #1024 , small fix to make ignore reasons human readable.

What has been done

Validation

PR check list

  • As much as possible, the changes include tests (unit and/or functional)
  • If the changes affect the end user (new feature, behavior change, bug fix) then the PR has a changelog entry (see doc/dev/getting-started.md). If the changes do not affect the end user, then the skip-changelog label has been added to the PR.

@gg-mmill gg-mmill requested a review from a team as a code owner December 10, 2024 17:44
@gg-mmill gg-mmill marked this pull request as draft December 11, 2024 08:01
@gg-mmill gg-mmill force-pushed the mmillet/-/fix_displayed_ignore_reason branch from 948cf80 to aa8b108 Compare December 11, 2024 08:21
Copy link

codecov bot commented Dec 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.05%. Comparing base (6d0d8b8) to head (aa8b108).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1031   +/-   ##
=======================================
  Coverage   92.05%   92.05%           
=======================================
  Files         181      181           
  Lines        7726     7728    +2     
=======================================
+ Hits         7112     7114    +2     
  Misses        614      614           
Flag Coverage Δ
unittests 92.05% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gg-mmill gg-mmill marked this pull request as ready for review December 11, 2024 08:24
Copy link
Collaborator

@agateau-gg agateau-gg left a comment

Choose a reason for hiding this comment

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

This change makes it hard to get a machine-friendly constant, which would be handy for machine-friendly outputs like JSON.

At the minimum I would remove the __str__() override: code that wants the human-readable version can call .value to get it.

But even this way using human-friendly values makes recreating the enum from a string more difficult:

class MyEnum(str, Enum):
    FOO = "This is foo"
    BAR = "This is bar"

# Fails
MyEnum("FOO")

# Fails
MyEnum("MyEnum.FOO")

# Works
MyEnum("This is foo")

That's why I would advocate for something like this instead:

class MyEnum(str, Enum):
    FOO = "FOO"
    BAR = "BAR"

    @property
    def display_value(self) -> str:
        # Or use a dict
        if self == self.FOO:
            return "This is foo"
        elif self == self.BAR:
            return "This is bar"
        raise ValueError(self)

With this we can do:

>>> MyEnum("FOO")
<MyEnum.FOO: 'FOO'>
>>> MyEnum.FOO.value
'FOO'
>>> MyEnum.FOO.display_value
'This is foo'

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

Successfully merging this pull request may close these issues.

2 participants